/home/mip/mip/public/img/credit/datatables/cookbook.tar
mocking_hard_dependencies.rst000066400000006140151521003710012427 0ustar00.. index::
    single: Cookbook; Mocking Hard Dependencies

Mocking Hard Dependencies (new Keyword)
=======================================

One prerequisite to mock hard dependencies is that the code we are trying to test uses autoloading.

Let's take the following code for an example:

.. code-block:: php

    <?php
    namespace App;
    class Service
    {
        function callExternalService($param)
        {
            $externalService = new Service\External();
            $externalService->sendSomething($param);
            return $externalService->getSomething();
        }
    }

The way we can test this without doing any changes to the code itself is by creating :doc:`instance mocks </reference/instance_mocking>` by using the ``overload`` prefix.

.. code-block:: php

    <?php
    namespace AppTest;
    use Mockery as m;
    class ServiceTest extends \PHPUnit_Framework_TestCase
    {
        public function testCallingExternalService()
        {
            $param = 'Testing';

            $externalMock = m::mock('overload:App\Service\External');
            $externalMock->shouldReceive('sendSomething')
                ->once()
                ->with($param);
            $externalMock->shouldReceive('getSomething')
                ->once()
                ->andReturn('Tested!');

            $service = new \App\Service();

            $result = $service->callExternalService($param);

            $this->assertSame('Tested!', $result);
        }
    }

If we run this test now, it should pass. Mockery does its job and our ``App\Service`` will use the mocked external service instead of the real one.

The problem with this is when we want to, for example, test the ``App\Service\External`` itself, or if we use that class somewhere else in our tests.

When Mockery overloads a class, because of how PHP works with files, that overloaded class file must not be included otherwise Mockery will throw a "class already exists" exception. This is where autoloading kicks in and makes our job a lot easier.

To make this possible, we'll tell PHPUnit to run the tests that have overloaded classes in separate processes and to not preserve global state. That way we'll avoid having the overloaded class included more than once. Of course this has its downsides as these tests will run slower.

Our test example from above now becomes:

.. code-block:: php

    <?php
    namespace AppTest;
    use Mockery as m;
    /**
     * @runTestsInSeparateProcesses
     * @preserveGlobalState disabled
     */
    class ServiceTest extends \PHPUnit_Framework_TestCase
    {
        public function testCallingExternalService()
        {
            $param = 'Testing';

            $externalMock = m::mock('overload:App\Service\External');
            $externalMock->shouldReceive('sendSomething')
                ->once()
                ->with($param);
            $externalMock->shouldReceive('getSomething')
                ->once()
                ->andReturn('Tested!');

            $service = new \App\Service();

            $result = $service->callExternalService($param);

            $this->assertSame('Tested!', $result);
        }
    }
detecting_mock_objects.rst000066400000000612151521003710011762 0ustar00.. index::
    single: Cookbook; Detecting Mock Objects

Detecting Mock Objects
======================

Users may find it useful to check whether a given object is a real object or a
simulated Mock Object. All Mockery mocks implement the
``\Mockery\MockInterface`` interface which can be used in a type check.

.. code-block:: php

    assert($mightBeMocked instanceof \Mockery\MockInterface);
index.rst000066400000000232151521003710006377 0ustar00Cookbook
========

.. toctree::
    :hidden:

    default_expectations
    detecting_mock_objects
    mocking_hard_dependencies

.. include:: map.rst.inc
default_expectations.rst000066400000001365151521003710011512 0ustar00.. index::
    single: Cookbook; Default Mock Expectations

Default Mock Expectations
=========================

Often in unit testing, we end up with sets of tests which use the same object
dependency over and over again. Rather than mocking this class/object within
every single unit test (requiring a mountain of duplicate code), we can
instead define reusable default mocks within the test case's ``setup()``
method. This even works where unit tests use varying expectations on the same
or similar mock object.

How this works, is that you can define mocks with default expectations. Then,
in a later unit test, you can add or fine-tune expectations for that specific
test. Any expectation can be set as a default using the ``byDefault()``
declaration.
map.rst.inc000066400000000177151521003710006625 0ustar00* :doc:`/cookbook/default_expectations`
* :doc:`/cookbook/detecting_mock_objects`
* :doc:`/cookbook/mocking_hard_dependencies`