dev-master
9999999-devQuickly test your entities
MIT
The Requires
- php >=5.3.3
- phpunit/phpunit ~4.8
by Jean-Noël Germon
Quickly test your entities
You can write very quickly unit tests for the accessors, adders and removers of your entities., (*1)
composer install jngermon/phpunit-entity-tester
This is an example of unit test file:, (*2)
<?php namespace Demo\Tests; use PhpUnitEntityTester\AccessorTester; use PhpUnitEntityTester\AccessorCollectionTester; class MyEntityTest extends \PHPUnit_Framework_TestCase { public function simpleTest() { $entity = new MyEntity(); // The entity that you want to test // Test 'setName' and 'getName' new AccessorTester($entity, 'name')->test('John Doe'); // Test 'addFruit', 'removeFruit' and 'getFruits' new AccessorCollectionTester($entity, 'fruits')->test('apple', 'pear'); } }
use PhpUnitEntityTester\AccessorTester;
$tester = new AccessortTester($entity, 'attribute');
Where $entity
is the entity to test and 'attribute'
the base name of setter and getter methods., (*3)
In this case, the tester will test the methods 'setAttribute' and 'getAttribute'., (*4)
Your can change the setter and getter methods like this :, (*5)
$tester->setterMethod('setFoo') //fluent method ->getteMethod('getBar'); // fluent method
You can remove the fluent constaint for the setter (set to true
by default), (*6)
$tester->fluent(false);
Simply use the test
method :, (*7)
$tester->test('the value');
In this case the tester : 1. calls the setter with 'the value' 2. tests the fluent constraint 3. tests that the getter return 'the value', (*8)
If the value returned by the getter have to be different to the value used with the setter
use the second argument of the test
method like this :, (*9)
$tester->test('value for setter', 'value that the getter have to return');
You can also only test the setter method like that :, (*10)
$tester->testSetter('value');
This method calls the setter method and tests the fluent constraint, (*11)
You can also only test the setter method like that :, (*12)
$tester->testGetter('value');
This method calls the getter method and tests the return with value in parameter, (*13)
use PhpUnitEntityTester\AccessorCollectionTester;
$tester = new AccessorCollectionTester($entity, 'items');
Where $entity
is the entity to test and 'items'
the base name for adder, remover and getter methods. For the adder and remover methods, the final 's' of the base name will be removed., (*14)
In this case, ther tester will test the methods 'addItem', 'removeItem' and 'getItems'., (*15)
You can change the adder, remover and getter methods like this :, (*16)
$tester->addMethod('addCustomItem') // fluent method ->removeMethod('popItem') // fluent method ->getMethod('getAllItems'); // fluent method
You can remove the fluent constraint for the adder and remover methods (set to true
by default), (*17)
$tester->fluent(false);
By default, the tester considere that the collection respect the unicity of its items. You can force the tester to considere thaht the collection don't respect the unicity like that :, (*18)
$tester->unique(false);
Simply use the test
method :, (*19)
$tester->test('first value', 'second value');
This method needs two arguments to work. In this case the tester : 1. calls and tests the adder method with the first value 2. try to remove the second value (that is not in collection yet) 3. calls and tests the adder method with the second value 4. try to add again the first value (to tetunique constraint) 5. try to remove the first value 6. add again the first value (to obtain a collection with the first and the second value in it), (*20)
This method uses the following three methods that you can also use separatly., (*21)
You can also only test the adder method like that :, (*22)
$tester->testAdd('value');
This method calls the adder method and tests : 1. the fluent constraint 2. if the value is in collection (added) 3. the unicity constraint, (*23)
You can also only test the remover method like that :, (*24)
$tester->testRemove('value');
This method calls the remover method and tests : 1. the fluent constraint 2. if the value is not in colleciton (removed) 3. if the remover removed the good number of items (all that match value but not more), (*25)
You can also only test the getter method like that :, (*26)
$tester->testGet();
This method tests if the returned value of the getter method is :
1. (if return is an object) implement Countable
interface
2. (if return is an object) implement Traversable
interface
3. (if return is not an object) is an array
or null
, (*27)
To test entity quicker, use dataProvider
with tester.
1. Create the test method, (*28)
/** * @dataProvider providerTestAccessor */ public function testAccessor($attribute, $setValue, $getValue = AccessorTester::USE_SET_DATA) { $entity = new YourEntityClass(); $tester = new AccessorTester($entity, $attribute); $tester->test($setValue, $getValue); }
public function providerTestAccessor() { return [ ['name', 'John Doe'], ['surname', 'JD'], ['astro', 'lion'], ['age', 12], ['age', -5, 0], ['color', 'red', '#ff0000'], ['createdAt', '2015-12-01', new \Datetime('2015-12-01')], // ... // one line here, generate complete test of your accessor ]; }
You can also use the process for AccessorCollectionTester
by customize the test method., (*29)
Special thanks for gerg0ire who encourage me to do that library, (*30)
Quickly test your entities
MIT