Created
May 23, 2012 03:21
-
-
Save colindecarlo/2773091 to your computer and use it in GitHub Desktop.
testing protected methods via a 'Testable' subclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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