Skip to content

Commit

Permalink
feature #143 Make polishsymfonycommunity/symfony-mocker-container opt…
Browse files Browse the repository at this point in the history
…ional (, pamil, videni, lchrusciel)

This PR was merged into the 4.0-dev branch.

Discussion
----------

| Q               | A
| --------------- | ---
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | kind of
| Related tickets | Replaces #142, fixes #141 
| License         | MIT


Commits
-------

708f40d make polishsymfonycommunity/symfony-mocker-container optional
36d0fcd Update src/ApiTestCase.php
fae54cc Delete MockContainerInterface.php
edc585c [Composer] Suggest symfony-mocker-container
6215f4f [README] Mention that symfony-mocker-container features are optional
  • Loading branch information
lchrusciel authored Feb 5, 2019
2 parents 990816c + 6215f4f commit db3fdb7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
72 changes: 37 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ ApiTestCase

Thanks to [PHP-Matcher](https://github.com/coduo/php-matcher) you can, according to its readme, "write expected json responses like a gangster". We definitely agree.

[SymfonyMockerContainer](https://github.com/PolishSymfonyCommunity/SymfonyMockerContainer) makes it super easy to mock services, which is great if you work on an application that communicates with other APIs, for example: Google Maps API.

It also uses [Alice](https://github.com/nelmio/alice) for easy Doctrine fixtures loading.

Features:
Expand All @@ -28,23 +26,6 @@ Assuming you already have Composer installed globally:
$ composer require --dev lchrusciel/api-test-case
```

Then you have to slightly change your Kernel logic to support SymfonyMockerContainer:

```php
// app/AppKernel.php

use \PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;

protected function getContainerBaseClass()
{
if ('test' === $this->environment) {
return MockerContainer::class;
}

return parent::getContainerBaseClass();
}
```

And it's done! ApiTestCase is working with the default configuration.

Usage
Expand Down Expand Up @@ -182,22 +163,6 @@ array(
)
```

It is also a really common case to communicate with some external API. But in test environment we want to be sure what we will receive from it. To check behaviour of our app with different responses from external API we can use [SymfonyMockerContainer](https://github.com/PolishSymfonyCommunity/SymfonyMockerContainer). This library allows to mock the third party API response, and asserts number of calls.
Again, this is extra useful when you work with APIs like Google Maps, Stripe etc. You can also mock response from other apps in your SOA project.

```php
public function testGetResponseFromMockedService()
{
$this->client->getContainer()->mock('app.third_party_api_client', 'ApiTestCase\Test\Service\ThirdPartyApiClient')
->shouldReceive('getInventory')
->once()
->andReturn($this->getJsonResponseFixture('third_party_api_inventory'))
;
}
```

From this moment, first `getInventory` method call will return the response defined in `third_party_api_inventory.json` file placed in a ``src/AppBundle/Tests/Responses/Mocked/`` folder, or any other location you have defined in ``phpunit.xml`` file.

### Testing With Database Fixtures

ApiTestCase is integrated with ``nelmio/alice``. Thanks to this nice library you can easily load your fixtures when you need them. You have to define your fixtures and place them in an appropriate directory.
Expand Down Expand Up @@ -274,6 +239,43 @@ Finally, to use these fixtures in a test, just call a proper method:
}
```

### Additional features

You may also add `polishsymfonycommunity/symfony-mocker-container` library to project and take advantage of mocking external API callse.

First, you have to slightly change your Kernel logic to support SymfonyMockerContainer:

```php
// app/AppKernel.php

use \PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;

protected function getContainerBaseClass()
{
if ('test' === $this->environment) {
return MockerContainer::class;
}

return parent::getContainerBaseClass();
}
```

And you are good to test endpoints, which communicate with some external API in a meantime. To check behaviour of our app with different responses from external API we can use [SymfonyMockerContainer](https://github.com/PolishSymfonyCommunity/SymfonyMockerContainer). This library allows to mock the third party API response, and asserts number of calls.
This is extra useful when you work with APIs like Google Maps, Stripe etc. You can also mock response from other apps in your SOA project.

```php
public function testGetResponseFromMockedService()
{
$this->client->getContainer()->mock('app.third_party_api_client', 'ApiTestCase\Test\Service\ThirdPartyApiClient')
->shouldReceive('getInventory')
->once()
->andReturn($this->getJsonResponseFixture('third_party_api_inventory'))
;
}
```

From this moment, first `getInventory` method call will return the response defined in `third_party_api_inventory.json` file placed in a ``src/AppBundle/Tests/Responses/Mocked/`` folder, or any other location you have defined in ``phpunit.xml`` file.

Configuration Reference
-----------------------

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
"nelmio/alice": "^3.1",
"phpspec/php-diff": "^1.1",
"phpunit/phpunit": "^6.0|^7.0|^8.0",
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
"symfony/browser-kit": "^3.4|^4.0",
"symfony/finder": "^3.4|^4.0",
"symfony/framework-bundle": "^3.4|^4.0",
"theofidry/alice-data-fixtures": "^1.0"
},
"require-dev": {
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
"symfony/serializer": "^3.4|^4.0"
},
"conflict": {
Expand All @@ -48,6 +48,9 @@
"replace": {
"lakion/api-test-case": "self.version"
},
"suggest": {
"polishsymfonycommunity/symfony-mocker-container": "For mocking container services and external APIs"
},
"autoload": {
"psr-4": {
"ApiTestCase\\": "src/"
Expand Down
5 changes: 4 additions & 1 deletion src/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public function setUpDatabase()

public function tearDown(): void
{
if (null !== $this->client && null !== $this->client->getContainer()) {
if (null !== $this->client &&
null !== $this->client->getContainer() &&
$this->client->getContainer() instanceof \PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer
) {
foreach ($this->client->getContainer()->getMockedServices() as $id => $service) {
$this->client->getContainer()->unmock($id);
}
Expand Down

0 comments on commit db3fdb7

Please sign in to comment.