Skip to content

Instantly share code, notes, and snippets.

@ericmann
Created December 2, 2013 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmann/7745729 to your computer and use it in GitHub Desktop.
Save ericmann/7745729 to your computer and use it in GitHub Desktop.
PHPUnit Protected accessibility example
<?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