Created
December 2, 2013 06:00
-
-
Save ericmann/7745729 to your computer and use it in GitHub Desktop.
PHPUnit Protected accessibility example
This file contains 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 | |
/** | |
* This class contains one protected method we wish to test. | |
* In reality, it would also contain several public methods, | |
* but that's immaterial to our discussion. | |
* | |
* @pacakge Tutorials | |
*/ | |
class ClassToTest { | |
/** | |
* Does something cool | |
* | |
* @returns string | |
*/ | |
protected function someFunction() { | |
return 'expected'; | |
} | |
} | |
/** | |
* Arbitrary subclass of the class we wish to test. Any methods of | |
* this class are just passthroughs for the protected functions | |
* defined by the parent. Any public methods on the parent can also | |
* be tested through the subclass since they're not overridden and | |
* will be inherited as usual. | |
* | |
* @package Tutorials | |
* @subpackage Tests | |
*/ | |
class test_ClassToTest extends ClassToTest { | |
/** | |
* Override for parent method. Just passes the result through. | |
*/ | |
public function someFunction() { | |
return parent::someFunction(); | |
} | |
} | |
/** | |
* The test class is using vanilla PHPUnit code. | |
* | |
* @package Tutorials | |
* @subpackage Tests | |
*/ | |
class TestCase extends PHPUnit_Framework_TestCase { | |
public function test_someFunction() { | |
// Remember, the subclass is functionally identical to its parent, | |
// The only difference is method visibility. | |
$instance = new test_ClassToTest(); | |
$result = $instance->someFunction(); | |
$this->assertEquals( 'expected', $result ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment