Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
.. index::
2
    single: Mocking; Demeter Chains
3
 
4
Mocking Demeter Chains And Fluent Interfaces
5
============================================
6
 
7
Both of these terms refer to the growing practice of invoking statements
8
similar to:
9
 
10
.. code-block:: php
11
 
12
    $object->foo()->bar()->zebra()->alpha()->selfDestruct();
13
 
14
The long chain of method calls isn't necessarily a bad thing, assuming they
15
each link back to a local object the calling class knows. As a fun example,
16
Mockery's long chains (after the first ``shouldReceive()`` method) all call to
17
the same instance of ``\Mockery\Expectation``. However, sometimes this is not
18
the case and the chain is constantly crossing object boundaries.
19
 
20
In either case, mocking such a chain can be a horrible task. To make it easier
21
Mockery supports demeter chain mocking. Essentially, we shortcut through the
22
chain and return a defined value from the final call. For example, let's
23
assume ``selfDestruct()`` returns the string "Ten!" to $object (an instance of
24
``CaptainsConsole``). Here's how we could mock it.
25
 
26
.. code-block:: php
27
 
28
    $mock = \Mockery::mock('CaptainsConsole');
29
    $mock->shouldReceive('foo->bar->zebra->alpha->selfDestruct')->andReturn('Ten!');
30
 
31
The above expectation can follow any previously seen format or expectation,
32
except that the method name is simply the string of all expected chain calls
33
separated by ``->``. Mockery will automatically setup the chain of expected
34
calls with its final return values, regardless of whatever intermediary object
35
might be used in the real implementation.
36
 
37
Arguments to all members of the chain (except the final call) are ignored in
38
this process.