Created
June 4, 2015 14:11
-
-
Save Exadra37/19e4681e02f11e9a55db to your computer and use it in GitHub Desktop.
More robust testing for the methods we are testing
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 | |
/** | |
* @author Paulo Silva(Exadra37) <exadra37ingmaildotcom> | |
* @since 2015/06/04 | |
*/ | |
abstract class BasicTestCase extends PHPUnit_Framework_TestCase // Orchestra\Testbench\TestCase for Laravel | |
{ | |
/** | |
* Setup the test environment. | |
*/ | |
public function setUp() | |
{ | |
parent::setUp(); | |
} | |
/** | |
* It will assert that the given method for the provided class, still have the same parameters names and same number of parameters, by comparing it against the given fixture. | |
* | |
* @param string $class - class for the method we want to test, like 'Morelytics\Common\Repositories\RollupRepository' | |
* @param string $method - method that we want to ensure no changes have been done to their parameters, like 'getCompareSingle' | |
* @param array $fixture - all parameters names for the method | |
* | |
* @return void | |
*/ | |
protected function assertThatMethodHaveSameParameters($class, $method, $fixture) | |
{ | |
$parameters = array(); | |
$reflector = new ReflectionClass($class); | |
foreach ($reflector->getMethod($method)->getParameters() as $parameter) { | |
$parameters[] = $parameter->name; | |
} | |
$this->assertEquals(count($fixture), count($parameters)); | |
$this->assertEquals($fixture, $parameters); | |
} | |
/** | |
* It will assert that the given method for the provided class, still have the same parameters names and same number of parameters, by comparing it against the given fixture. | |
* | |
* @param string $class - class for the method we want to test, like 'Morelytics\Common\Repositories\RollupRepository' | |
* @param string $method - method that we want to ensure no changes have been done to their parameters, like 'getCompareSingle' | |
* @param string $footprint - the foot print for the method, that can be obtained from $reflector = new ReflectionClass($class); print_r($reflector->getMethod($method)->__toString()) | |
* | |
* @return void | |
*/ | |
protected function assertThatMethodHaveSameFootPrint($class, $method, $footPrint) | |
{ | |
$reflector = new ReflectionClass($class); | |
$this->assertContains($footPrint, $reflector->getMethod($method)->__toString()); | |
} | |
} |
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 | |
/** | |
* @author Paulo Silva(Exadra37) <exadra37ingmaildotcom> | |
* @since 2015/06/04 | |
*/ | |
class TestExample extends BasicTestCase | |
{ | |
/** | |
* ALWAYS USE ONE OF THE BELOW TEST EXAMPLE FOR ANY METHOD OR FUNCTION YOU ARE WRITING TESTS FOR | |
* | |
* THE MORE STRICT ONE IS THE LAST ONE test_method_have_same_foot_print() | |
*/ | |
/** | |
* @test | |
*/ | |
public function test_method_have_same_parameters() | |
{ | |
$fixture = array( | |
'required1', | |
'required2', | |
'optional1', | |
'optional2', | |
'optional3' | |
); | |
$this->assertThatMethodHaveSameParameters('Exadra37\PackageName\Repositories\UsersRepository', 'getUsers', $fixture); | |
} | |
/** | |
* @test | |
*/ | |
public function test_method_have_same_foot_print() | |
{ | |
$footPrint = 'Parameters [5] { | |
Parameter #0 [ <required> $required1 ] | |
Parameter #1 [ <required> $required2 ] | |
Parameter #2 [ <optional> $optional1 = true ] | |
Parameter #3 [ <optional> $optional2 = 0 ] | |
Parameter #4 [ <optional> $optional3 = false ]'; | |
$this->assertThatMethodHaveSameFootPrint('Exadra37\PackageName\Repositories\UsersRepository', 'getUsers', $footPrint); | |
} | |
} |
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 | |
namespace Exadra37\PackageName\Repositories; | |
/** | |
* @author Paulo Silva(Exadra37) <exadra37ingmaildotcom> | |
* @since 2015/06/04 | |
*/ | |
class UsersRepository | |
{ | |
public function getUsers($required1, $required2, $optional1 = true, $optional2 = 0, $optional3 = false) | |
{ | |
// some logic here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment