PHP code example of adgoal / phpunit-entity-tester

1. Go to this page and download the library: Download adgoal/phpunit-entity-tester library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adgoal / phpunit-entity-tester example snippets




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');

$tester->setterMethod('setFoo') //fluent method
    ->getteMethod('getBar'); // fluent method

$tester->fluent(false);

$tester->test('the value');

$tester->test('value for setter', 'value that the getter have to return');

$tester->testSetter('value');

$tester->testGetter('value');

use PhpUnitEntityTester\AccessorCollectionTester;

$tester = new AccessorCollectionTester($entity, 'items');

$tester->addMethod('addCustomItem') // fluent method
    ->removeMethod('popItem') // fluent method
    ->getMethod('getAllItems'); // fluent method

$tester->fluent(false);

$tester->unique(false);

$tester->test('first value', 'second value');

$tester->testAdd('value');

$tester->testRemove('value');

$tester->testGet();

/**
 * @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
    ];
}