2017 © Pedro Peláez
 

library amock

PHPunit mock fixtures

image

thanos-kontos/amock

PHPunit mock fixtures

  • Thursday, November 23, 2017
  • by tkontos
  • Repository
  • 2 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Amock - Organize your stub objects in yaml

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality, (*1)

Release notes

Still not 100% stable. Use with caution, (*2)

At a glance

Quoting from phpunit:, (*3)

Sometimes it is just plain hard to test the system under test (SUT) because it depends on other components that cannot be used in the test environment. This could be because they aren't available, they will not return the results needed for the test or because executing them would have undesirable side effects. In other cases, our test strategy requires us to have more control or visibility of the internal behavior of the SUT., (*4)

When we are writing a test in which we cannot (or chose not to) use a real depended-on component (DOC), we can replace it with a Test Double. The Test Double doesn't have to behave exactly like the real DOC; it merely has to provide the same API as the real one so that the SUT thinks it is the real one!, (*5)

It is almost impossible to write good tests without stubbing objects, because we often need to imitate writing to a database or calling an external service., (*6)

Amock lets you create these stub objects in yaml instead of "polluting" your tests with irrelevant code., (*7)

Instructions

composer require --dev thanos-kontos/amock, (*8)

Add a directory to store your fixtures e.g. tests/mock_fixtures and as as many yaml files as you want., (*9)

# tests/mock_fixtures/repositories.yml

MyProject\Library\UserRepository:
  mockUserRepository:
    disableConstructor: true
    mockMethods:
      insert: '@null'
      update: '@null'
MyProject\Library\ProductRepository:
  mockProductRepository:
    disableConstructor: true
    mockMethods:
      insert: '@null'
      update: '@null'
# tests/mock_fixtures/gateways.yml

MyProject\Library\SomeApiGateway:
  mockSuccessResponse:
    disableConstructor: true
    mockMethods:
      getHelloReponse: '@string:{"hello":"world"}'
  mock404Response:
    disableConstructor: true
    mockMethods:
      getHelloReponse: '@string:{"error": "404","message":"Not found"}'
      sampleSetter: '@self'
      methodThatReturnsDifferentValueOnConsecutiveCalls:
        - '@string:{"hello":"world"}'
        - '@integer:123'
        - '@boolean:false'
      methodThatReturnsArray: '@array:{"111":"aaa","222":"bbb"}'
      methodThatReturnsBoolean: '@boolean:false'
      methodThatReturnsInteger: '@integer:123'
      methodThatReturnsNull: '@null'
  mockExceptionResponse:
    disableConstructor: true
    mockMethods:
      getHelloReponse: '@exception:\MyProject\Library\Exception\ApiException'

On your test setUp method, (*10)

$config = new \Amock\Configuration('yaml', 'dir', '/path/to/mock_fixtures');
$this->amock = \Amock\Amock::create($config, $testCase);

or, (*11)

$config = new \Amock\Configuration('yaml', 'file', '/path/to/mock_fixtures/somefile.yml');
$this->amock = \Amock\Amock::create($config, $testCase);

Then you can use the configured mocks in your tests:, (*12)

$stub = $this->amock->get('mock404Response');

Example

There is an dummy project here that you can use as a reference., (*13)

License

Amock is released under the MIT License., (*14)

The Versions

23/11 2017

dev-master

9999999-dev

PHPunit mock fixtures

  Sources   Download

MIT

The Requires

 

The Development Requires

test mock phpunit fixture

16/11 2017

0.0.3

0.0.3.0

PHPunit mock fixtures

  Sources   Download

MIT

The Requires

 

The Development Requires

test mock phpunit fixture

05/11 2017

0.0.2

0.0.2.0

PHPunit mock fixtures

  Sources   Download

MIT

The Requires

 

The Development Requires

test mock phpunit fixture

31/10 2017

0.0.1

0.0.1.0

  Sources   Download

The Requires