404

[ Avaa Bypassed ]




Upload:

Command:

elspacio@3.129.63.214: ~ $
.. index::
    single: Mocking; Demeter Chains

Mocking Demeter Chains And Fluent Interfaces
============================================

Both of these terms refer to the growing practice of invoking statements
similar to:

.. code-block:: php

    $object->foo()->bar()->zebra()->alpha()->selfDestruct();

The long chain of method calls isn't necessarily a bad thing, assuming they
each link back to a local object the calling class knows. As a fun example,
Mockery's long chains (after the first ``shouldReceive()`` method) all call to
the same instance of ``\Mockery\Expectation``. However, sometimes this is not
the case and the chain is constantly crossing object boundaries.

In either case, mocking such a chain can be a horrible task. To make it easier
Mockery supports demeter chain mocking. Essentially, we shortcut through the
chain and return a defined value from the final call. For example, let's
assume ``selfDestruct()`` returns the string "Ten!" to $object (an instance of
``CaptainsConsole``). Here's how we could mock it.

.. code-block:: php

    $mock = \Mockery::mock('CaptainsConsole');
    $mock->shouldReceive('foo->bar->zebra->alpha->selfDestruct')->andReturn('Ten!');

The above expectation can follow any previously seen format or expectation,
except that the method name is simply the string of all expected chain calls
separated by ``->``. Mockery will automatically setup the chain of expected
calls with its final return values, regardless of whatever intermediary object
might be used in the real implementation.

Arguments to all members of the chain (except the final call) are ignored in
this process.

Filemanager

Name Type Size Permission Actions
alternative_should_receive_syntax.rst File 2.33 KB 0644
argument_validation.rst File 10.49 KB 0644
creating_test_doubles.rst File 14.12 KB 0644
demeter_chains.rst File 1.6 KB 0644
expectations.rst File 15.57 KB 0644
final_methods_classes.rst File 1.32 KB 0644
index.rst File 400 B 0644
instance_mocking.rst File 805 B 0644
magic_methods.rst File 687 B 0644
map.rst.inc File 550 B 0644
partial_mocks.rst File 4.19 KB 0644
pass_by_reference_behaviours.rst File 4.22 KB 0644
phpunit_integration.rst File 4.74 KB 0644
protected_methods.rst File 668 B 0644
public_properties.rst File 821 B 0644
public_static_properties.rst File 701 B 0644
spies.rst File 4.63 KB 0644