Integration tests

Since your project uses the TechDivision\DynamicsBcApiClient\Api\RequestInterface, by default, integration tests will try to call the actual REST API, which will fail but is also not desired.

To avoid this, this module provides a mocked client factory (see TechDivision\DynamicsBcApiClient\Test\Integration\MockedClientFactory).

  • Replacing the client factory for each integration test is painful and error-prone, so consider globally registering the mocked client factory as the default for the integration tests

  • The registration can be done by adding a preference file to dev/tests/integration/etc/di/preferences/, e.g. dev/tests/integration/etc/di/preferences/project.php

  • The project uses the TechDivision\DynamicsBcApiClient\Api\RequestInterface

  • The integration tests will attempt to call the actual REST API by default.

  • To prevent failed tests, this module provides a mocked client factory

    • see \TechDivision\DynamicsBcApiClient\Test\Integration\MockedClientFactory

  • The client factory gets pre-registered by default for your project’s integration tests to replace the client factory in each integration test

  • Registration gets achieved by adding a preference file including file headers and copyright notice dev/tests/integration/etc/di/preferences/.

    Add the following code snippet to the file, including the file headers and copyright notice:
return [
    \GuzzleHttp\ClientInterfaceFactory::class => \TechDivision\DynamicsBcApiClient\Test\Integration\MockedClientFactory::class,
];

Be sure to clear the integration test sandbox afterward to enforce this new setting throughout the test suite

If you are writing integration tests after this, and they rely on data returned from a REST API, use the 'test class on \TechDivision\DynamicsBcApiClient\Test\Integration\BaseRequestUsageTestCase.

This class applies a \GuzzleHttp\Handler\MockHandler to the mocked client factory and allows predictable responses to get included in the queue of the mirrored handler
<?php
use \TechDivision\DynamicsBcApiClient\Test\Integration\BaseRequestUsageTestCase;

class MyTest extends BaseRequestUsageTestCase
{
    public function testSomething(): void
    {
        //enqueue three responses of which the last simulates a 403 status code
        $this->enqueueResponse(
            $this->buildResponse(200, '{"foo": "bar"}')
        );
        $this->enqueueResponse(
            $this->buildResponse(200, '{"foo": "bar"}')
        );
        $this->enqueueResponse(
            $this->buildResponse(403, 'Forbidden - You are not allowed to access this resource', 'text/plain;charset=UTF-8')
        );

        //do something that relies on the responses
    }
}