项目作者: mcn-fredw

项目描述 :
Simple trait to create mcok objects from yaml files for use in PHPUnit.
高级语言: PHP
项目地址: git://github.com/mcn-fredw/mock-from-yaml-php.git
创建时间: 2017-03-04T19:44:47Z
项目社区:https://github.com/mcn-fredw/mock-from-yaml-php

开源协议:BSD 3-Clause "New" or "Revised" License

下载


MockFromYaml

PHP traits to mock objects from arrays and read the data provider arrays from yaml files.

Installation

composer install (WIP)

Depends on:

phpunit/phpunit
symfony/yaml

Usage

Using YAML files for simple test case data (don’t really need this lib for that):

  1. namespace Tests\libs\MyClassTest_NS;
  2. use PHPUnit_Framework_TestCase as TestCase;
  3. use MockFromYaml\YamlTestCasesReaderTrait;
  4. class MyClassTest extends TestCase
  5. {
  6. use YamlTestCasesReaderTrait;
  7. public function someMethodProvider()
  8. {
  9. return static::readYamlTestCases(__DIR__ . '/MyClassTest.someMethodProvider.yml');
  10. }
  11. /**
  12. * @dataProvider someMethodProvider
  13. */
  14. public function testSomeMethod($exception, $in, $expect)
  15. {
  16. if (0 < strlen($exception)) {
  17. $this->expectException($exception);
  18. }
  19. $obj = new MyClass();
  20. $this->assertEquals($expect, $obj->someMethod($in));
  21. }
  22. }

__DIR__ . ‘/MyClassTest.someMethodProvider.yml’

  1. test case 1:
  2. exception: null
  3. in: 0
  4. expect: false
  5. test case 2:
  6. exception: null
  7. in: 1
  8. expect: true

Using YAML files for object mocking (high level usage):

  1. namespace Tests\libs\MyClassTest_NS;
  2. use PHPUnit_Framework_TestCase as TestCase;
  3. use MockFromYaml\MockFromArrayCreatorTrait;
  4. use MockFromYaml\YamlTestCasesReaderTrait;
  5. class MyClassTest extends TestCase
  6. {
  7. use YamlTestCasesReaderTrait;
  8. public function someMethodProvider()
  9. {
  10. return static::readYamlTestCases(__DIR__ . '/MyClassTest.someMethodProvider.yml');
  11. }
  12. /**
  13. * @dataProvider someMethodProvider
  14. */
  15. public function testSomeMethod($exception, $expect, $fixtures)
  16. {
  17. $domain = [ 'someIntitalValueKey' => 'someIntitalValue' ];
  18. $this->createMockFixtures($fixtures, $domain);
  19. if (0 < strlen($exception)) {
  20. $this->expectException($exception);
  21. }
  22. $obj = new MyClass($domain['serviceConnector']);
  23. $this->assertEquals($expected, $obj->someMethod($domain['authBridge']));
  24. }
  25. }

__DIR__ . ‘/MyClassTest.someMethodProvider.yml’

  1. test case 1:
  2. exception: null
  3. expect: false
  4. fixtures:
  5. # service provider mock-up
  6. - class: 'Project\libs\ServiceProvider'
  7. domain: 'serviceProvider'
  8. fixture:
  9. authenticate:
  10. expects: [once]
  11. with: [[equalTo, 'user'], [equalTo, 'token']]
  12. will: [returnValue, false]
  13. # injected -mock-dependency
  14. - class: 'Project\libs\ServiceConnector'
  15. domain: 'serviceConnector'
  16. fixture:
  17. connect:
  18. expects: [once]
  19. with: [equalTo, 'http://my.local']
  20. # reference service provider mock-up
  21. will: [returnValue, '$serviceProvider']
  22. # auth bridge
  23. - class: 'Project\libs\AuthBridge'
  24. domain: 'authBridge'
  25. fixture:
  26. getEntity:
  27. expects: [once]
  28. will: [returnValue, 'user']
  29. getToken:
  30. expects: [once]
  31. will: [returnValue, 'token']

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

Initial version 1.0.0

License

BSD 3-Clause License