phpunit SomeClassTest.php
Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/bin/phpunit on line 38
pear config-set auto_discover 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pear install pear.phpunit.de/PHP_CodeCoverage
pecl install xdebug # required for code coverage, it will display the path to xdebug.so
find / -iname 'php.ini'; # discover where the php.ini file is and edit it
php --ini # discover which php.ini is being used/loading modules
e.g. echo 'zend_extension="/usr/lib/php5/20090626/xdebug.so"' >> /etc/php5/cli/php.ini
php -m # list the modules being used
phpunit --coverage-html ./report SomeClassNameTest.php
browser open file:///home/ubuntu/Desktop/repos/web/tests/report/index.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
phpunit SomeClassTest.php
Fatal error: Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/bin/phpunit on line 39
sudo pear install --alldeps phpunit/PHPUnit # OR sudo apt-get install phpunit
Attempting to discover channel "pear.symfony.com"...
downloading channel.xml ...
Starting to download channel.xml (811 bytes)
....done: 811 bytes
Auto-discovered channel "pear.symfony.com", alias "symfony2", adding to registry
downloading PHPUnit-3.7.28.tgz ...
Starting to download PHPUnit-3.7.28.tgz (116,279 bytes)
...done: 116,279 bytes
downloading PHP_Timer-1.0.5.tgz ...
Starting to download PHP_Timer-1.0.5.tgz (3,597 bytes)
...done: 3,597 bytes
downloading PHPUnit_MockObject-1.2.3.tgz ...
Starting to download PHPUnit_MockObject-1.2.3.tgz (20,390 bytes)
...done: 20,390 bytes
downloading Yaml-2.3.6.tgz ...
Starting to download Yaml-2.3.6.tgz (40,482 bytes)
...done: 40,482 bytes
downloading PHP_Invoker-1.1.3.tgz ...
Starting to download PHP_Invoker-1.1.3.tgz (3,734 bytes)
...done: 3,734 bytes
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.5
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.2.3
install ok: channel://pear.symfony.com/Yaml-2.3.6
install ok: channel://pear.phpunit.de/PHPUnit-3.7.28
install ok: channel://pear.phpunit.de/PHP_Invoker-1.1.3
phpunit --colors --verbose --debug --filter test_prefix_desired SomeClassTest.php
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//Working with a Class with Static Methods (but at least one public method), Private Static class variable
$myClassReflection = new ReflectionClass('MixpanelProxy');
print_r($myClassReflection->getStaticProperties());
$myClassReflectionProperty = $myClassReflection->getProperty('instance');
$myClassReflectionProperty->setAccessible(true);
$stub = $this->getMockBuilder('MixpanelProxy')->disableOriginalConstructor()->getMock();
$stub->expects($this->any())->method('send')->will($this->returnValue('bar'));
$myClassReflectionProperty = $myClassReflectionProperty->setValue($stub);
MixpanelProxy::event('foo'); //static event() depends on public send()
// $myClassReflection->setStaticPropertyValue('instance', $stub); //works if it was public static
// ReflectionException: Class MixpanelProxy does not have a property named instance
// http://www.php.net/manual/en/reflectionclass.setstaticpropertyvalue.php
NOTE: depends on dependency injection and not using static methods! (only static methods in the same class can sorta mock)
// http://phpunit.de/manual/3.8/en/test-doubles.html#test-doubles.mock-objects
// https://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/
// Completely artificial class
$stub = $this->getMockBuilder('SomeClass')->disableOriginalConstructor()->getMock();
$stub->expects($this->any())->method('doSomething')->will($this->returnValue('foo'));
$this->assertEquals('foo', $stub->doSomething());
// reuses the constructor, private variables, etc.
$addons_model = $this->getMock('Addons_Model'); //all mocked methods return null by default
print_r(get_class_methods($addons_model));
$groupid=0;
var_dump($addons_model->destroy_by_group($groupid)); // NULL
$addons_model->expects($this->any())
->method('destroy_by_group')
->will($this->returnValue('hi'));
var_dump($addons_model->destroy_by_group($groupid)); // NULL
getMock( arguments )
String – Required – the class name to mock
Array – Optional – the methods to mock
Array – Optional – arguments to pass to the mock’s constructor
String – Optional – a class name for the mock
Boolean – Optional – disable the call to the original class’ constructor
Boolean – Optional – disable the call to the original object’s clone method
Boolean – Optional – disable autoload during the mock object creation
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
You cannot mock a private method as getMock uses the reference class definition,
it cannot use a Reflection as a source =(
http://phpunit.de/manual/current/en/test-doubles.html # Limitations!
http://www.mikeyd.com.au/2011/01/20/how-to-use-phps-reflectionclass-to-test-private-methods-and-properties-with-phpunit/