PHP traits to mock objects from arrays and read the data provider arrays from yaml files.
composer install (WIP)
Depends on:
phpunit/phpunit symfony/yaml
Using YAML files for simple test case data (don't really need this lib for that):
namespace Tests\libs\MyClassTest_NS;
use PHPUnit_Framework_TestCase as TestCase;
use MockFromYaml\YamlTestCasesReaderTrait;
class MyClassTest extends TestCase
{
use YamlTestCasesReaderTrait;
public function someMethodProvider()
{
return static::readYamlTestCases(__DIR__ . '/MyClassTest.someMethodProvider.yml');
}
/**
* @dataProvider someMethodProvider
*/
public function testSomeMethod($exception, $in, $expect)
{
if (0 < strlen($exception)) {
$this->expectException($exception);
}
$obj = new MyClass();
$this->assertEquals($expect, $obj->someMethod($in));
}
}
__DIR__ . '/MyClassTest.someMethodProvider.yml'
test case 1:
exception: null
in: 0
expect: false
test case 2:
exception: null
in: 1
expect: true
Using YAML files for object mocking (high level usage):
namespace Tests\libs\MyClassTest_NS;
use PHPUnit_Framework_TestCase as TestCase;
use MockFromYaml\MockFromArrayCreatorTrait;
use MockFromYaml\YamlTestCasesReaderTrait;
class MyClassTest extends TestCase
{
use YamlTestCasesReaderTrait;
public function someMethodProvider()
{
return static::readYamlTestCases(__DIR__ . '/MyClassTest.someMethodProvider.yml');
}
/**
* @dataProvider someMethodProvider
*/
public function testSomeMethod($exception, $expect, $fixtures)
{
$domain = [ 'someIntitalValueKey' => 'someIntitalValue' ];
$this->createMockFixtures($fixtures, $domain);
if (0 < strlen($exception)) {
$this->expectException($exception);
}
$obj = new MyClass($domain['serviceConnector']);
$this->assertEquals($expected, $obj->someMethod($domain['authBridge']));
}
}
__DIR__ . '/MyClassTest.someMethodProvider.yml'
test case 1:
exception: null
expect: false
fixtures:
# service provider mock-up
- class: 'Project\libs\ServiceProvider'
domain: 'serviceProvider'
fixture:
authenticate:
expects: [once]
with: [[equalTo, 'user'], [equalTo, 'token']]
will: [returnValue, false]
# injected -mock-dependency
- class: 'Project\libs\ServiceConnector'
domain: 'serviceConnector'
fixture:
connect:
expects: [once]
with: [equalTo, 'http://my.local']
# reference service provider mock-up
will: [returnValue, '$serviceProvider']
# auth bridge
- class: 'Project\libs\AuthBridge'
domain: 'authBridge'
fixture:
getEntity:
expects: [once]
will: [returnValue, 'user']
getToken:
expects: [once]
will: [returnValue, 'token']
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Initial version 1.0.0
BSD 3-Clause License