Skip to content

Instantly share code, notes, and snippets.

@colindecarlo
Created May 23, 2012 03:21
Show Gist options
  • Save colindecarlo/2773091 to your computer and use it in GitHub Desktop.
Save colindecarlo/2773091 to your computer and use it in GitHub Desktop.
testing protected methods via a 'Testable' subclass
<?php
//interesting bits
public function test_protected_persist_with_testable_subclass_success()
{
$filePersister = new TestableFile($_FILES, $this->logger);
$filePersister->_persistWrapper();
$persistedLocation = \PHPUnit_Framework_Assert::readAttribute($filePersister, '_location');
$this->assertRegExp('/UL-/', $persistedLocation);
$this->assertTrue(file_exists($persistedLocation));
$this->assertEquals("Hello World\n", file_get_contents($persistedLocation));
}
public function test_protected_persist_with_testable_subclass_failure()
{
$filePersisterMock = $this->getMockBuilder('GPUG\Test\Examples\Persister\TestableFile')
->setConstructorArgs(array($_FILES, $this->logger))
->setMethods(array('_moveUploadedFile'))
->getMock();
$filePersisterMock->expects($this->once())
->method('_moveUploadedFile')
->will($this->returnValue(false));
try {
$filePersisterMock->_persistWrapper();
} catch (\Exception $e) {
$this->assertNull(\PHPUnit_Framework_Assert::readAttribute($filePersisterMock, '_location'));
$this->assertEquals('error moving uploaded file', $e->getMessage());
return;
}
$this->fail('Expected exception was not thrown in test_protected_persist_directly_with_reflection_failure');
}
// interesing bits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment