creating_test_doubles.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. .. index::
  2. single: Reference; Creating Test Doubles
  3. Creating Test Doubles
  4. =====================
  5. Mockery's main goal is to help us create test doubles. It can create stubs,
  6. mocks, and spies.
  7. Stubs and mocks are created the same. The difference between the two is that a
  8. stub only returns a preset result when called, while a mock needs to have
  9. expectations set on the method calls it expects to receive.
  10. Spies are a type of test doubles that keep track of the calls they received, and
  11. allow us to inspect these calls after the fact.
  12. When creating a test double object, we can pass in an identifier as a name for
  13. our test double. If we pass it no identifier, the test double name will be
  14. unknown. Furthermore, the identifier does not have to be a class name. It is a
  15. good practice, and our recommendation, to always name the test doubles with the
  16. same name as the underlying class we are creating test doubles for.
  17. If the identifier we use for our test double is a name of an existing class,
  18. the test double will inherit the type of the class (via inheritance), i.e. the
  19. mock object will pass type hints or ``instanceof`` evaluations for the existing
  20. class. This is useful when a test double must be of a specific type, to satisfy
  21. the expectations our code has.
  22. Stubs and mocks
  23. ---------------
  24. Stubs and mocks are created by calling the ``\Mockery::mock()`` method. The
  25. following example shows how to create a stub, or a mock, object named "foo":
  26. .. code-block:: php
  27. $mock = \Mockery::mock('foo');
  28. The mock object created like this is the loosest form of mocks possible, and is
  29. an instance of ``\Mockery\MockInterface``.
  30. .. note::
  31. All test doubles created with Mockery are an instance of
  32. ``\Mockery\MockInterface``, regardless are they a stub, mock or a spy.
  33. To create a stub or a mock object with no name, we can call the ``mock()``
  34. method with no parameters:
  35. .. code-block:: php
  36. $mock = \Mockery::mock();
  37. As we stated earlier, we don't recommend creating stub or mock objects without
  38. a name.
  39. Classes, abstracts, interfaces
  40. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  41. The recommended way to create a stub or a mock object is by using a name of
  42. an existing class we want to create a test double of:
  43. .. code-block:: php
  44. $mock = \Mockery::mock('MyClass');
  45. This stub or mock object will have the type of ``MyClass``, through inheritance.
  46. Stub or mock objects can be based on any concrete class, abstract class or even
  47. an interface. The primary purpose is to ensure the mock object inherits a
  48. specific type for type hinting.
  49. .. code-block:: php
  50. $mock = \Mockery::mock('MyInterface');
  51. This stub or mock object will implement the ``MyInterface`` interface.
  52. .. note::
  53. Classes marked final, or classes that have methods marked final cannot be
  54. mocked fully. Mockery supports creating partial mocks for these cases.
  55. Partial mocks will be explained later in the documentation.
  56. Mockery also supports creating stub or mock objects based on a single existing
  57. class, which must implement one or more interfaces. We can do this by providing
  58. a comma-separated list of the class and interfaces as the first argument to the
  59. ``\Mockery::mock()`` method:
  60. .. code-block:: php
  61. $mock = \Mockery::mock('MyClass, MyInterface, OtherInterface');
  62. This stub or mock object will now be of type ``MyClass`` and implement the
  63. ``MyInterface`` and ``OtherInterface`` interfaces.
  64. .. note::
  65. The class name doesn't need to be the first member of the list but it's a
  66. friendly convention to use for readability.
  67. We can tell a mock to implement the desired interfaces by passing the list of
  68. interfaces as the second argument:
  69. .. code-block:: php
  70. $mock = \Mockery::mock('MyClass', 'MyInterface, OtherInterface');
  71. For all intents and purposes, this is the same as the previous example.
  72. Spies
  73. -----
  74. The third type of test doubles Mockery supports are spies. The main difference
  75. between spies and mock objects is that with spies we verify the calls made
  76. against our test double after the calls were made. We would use a spy when we
  77. don't necessarily care about all of the calls that are going to be made to an
  78. object.
  79. A spy will return ``null`` for all method calls it receives. It is not possible
  80. to tell a spy what will be the return value of a method call. If we do that, then
  81. we would deal with a mock object, and not with a spy.
  82. We create a spy by calling the ``\Mockery::spy()`` method:
  83. .. code-block:: php
  84. $spy = \Mockery::spy('MyClass');
  85. Just as with stubs or mocks, we can tell Mockery to base a spy on any concrete
  86. or abstract class, or to implement any number of interfaces:
  87. .. code-block:: php
  88. $spy = \Mockery::spy('MyClass, MyInterface, OtherInterface');
  89. This spy will now be of type ``MyClass`` and implement the ``MyInterface`` and
  90. ``OtherInterface`` interfaces.
  91. .. note::
  92. The ``\Mockery::spy()`` method call is actually a shorthand for calling
  93. ``\Mockery::mock()->shouldIgnoreMissing()``. The ``shouldIgnoreMissing``
  94. method is a "behaviour modifier". We'll discuss them a bit later.
  95. Mocks vs. Spies
  96. ---------------
  97. Let's try and illustrate the difference between mocks and spies with the
  98. following example:
  99. .. code-block:: php
  100. $mock = \Mockery::mock('MyClass');
  101. $spy = \Mockery::spy('MyClass');
  102. $mock->shouldReceive('foo')->andReturn(42);
  103. $mockResult = $mock->foo();
  104. $spyResult = $spy->foo();
  105. $spy->shouldHaveReceived()->foo();
  106. var_dump($mockResult); // int(42)
  107. var_dump($spyResult); // null
  108. As we can see from this example, with a mock object we set the call expectations
  109. before the call itself, and we get the return result we expect it to return.
  110. With a spy object on the other hand, we verify the call has happened after the
  111. fact. The return result of a method call against a spy is always ``null``.
  112. We also have a dedicated chapter to :doc:`spies` only.
  113. .. _creating-test-doubles-partial-test-doubles:
  114. Partial Test Doubles
  115. --------------------
  116. Partial doubles are useful when we want to stub out, set expectations for, or
  117. spy on *some* methods of a class, but run the actual code for other methods.
  118. We differentiate between three types of partial test doubles:
  119. * runtime partial test doubles,
  120. * generated partial test doubles, and
  121. * proxied partial test doubles.
  122. Runtime partial test doubles
  123. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  124. What we call a runtime partial, involves creating a test double and then telling
  125. it to make itself partial. Any method calls that the double hasn't been told to
  126. allow or expect, will act as they would on a normal instance of the object.
  127. .. code-block:: php
  128. class Foo {
  129. function foo() { return 123; }
  130. function bar() { return $this->foo(); }
  131. }
  132. $foo = mock(Foo::class)->makePartial();
  133. $foo->foo(); // int(123);
  134. We can then tell the test double to allow or expect calls as with any other
  135. Mockery double.
  136. .. code-block:: php
  137. $foo->shouldReceive('foo')->andReturn(456);
  138. $foo->bar(); // int(456)
  139. See the cookbook entry on :doc:`../cookbook/big_parent_class` for an example
  140. usage of runtime partial test doubles.
  141. Generated partial test doubles
  142. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  143. The second type of partial double we can create is what we call a generated
  144. partial. With generated partials, we specifically tell Mockery which methods
  145. we want to be able to allow or expect calls to. All other methods will run the
  146. actual code *directly*, so stubs and expectations on these methods will not
  147. work.
  148. .. code-block:: php
  149. class Foo {
  150. function foo() { return 123; }
  151. function bar() { return $this->foo(); }
  152. }
  153. $foo = mock("Foo[foo]");
  154. $foo->foo(); // error, no expectation set
  155. $foo->shouldReceive('foo')->andReturn(456);
  156. $foo->foo(); // int(456)
  157. // setting an expectation for this has no effect
  158. $foo->shouldReceive('bar')->andReturn(999);
  159. $foo->bar(); // int(456)
  160. It's also possible to specify explicitly which methods to run directly using
  161. the `!method` syntax:
  162. .. code-block:: php
  163. class Foo {
  164. function foo() { return 123; }
  165. function bar() { return $this->foo(); }
  166. }
  167. $foo = mock("Foo[!foo]");
  168. $foo->foo(); // int(123)
  169. $foo->bar(); // error, no expectation set
  170. .. note::
  171. Even though we support generated partial test doubles, we do not recommend
  172. using them.
  173. One of the reasons why is because a generated partial will call the original
  174. constructor of the mocked class. This can have unwanted side-effects during
  175. testing application code.
  176. See :doc:`../cookbook/not_calling_the_constructor` for more details.
  177. Proxied partial test doubles
  178. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  179. A proxied partial mock is a partial of last resort. We may encounter a class
  180. which is simply not capable of being mocked because it has been marked as
  181. final. Similarly, we may find a class with methods marked as final. In such a
  182. scenario, we cannot simply extend the class and override methods to mock - we
  183. need to get creative.
  184. .. code-block:: php
  185. $mock = \Mockery::mock(new MyClass);
  186. Yes, the new mock is a Proxy. It intercepts calls and reroutes them to the
  187. proxied object (which we construct and pass in) for methods which are not
  188. subject to any expectations. Indirectly, this allows us to mock methods
  189. marked final since the Proxy is not subject to those limitations. The tradeoff
  190. should be obvious - a proxied partial will fail any typehint checks for the
  191. class being mocked since it cannot extend that class.
  192. .. _creating-test-doubles-aliasing:
  193. Aliasing
  194. --------
  195. Prefixing the valid name of a class (which is NOT currently loaded) with
  196. "alias:" will generate an "alias mock". Alias mocks create a class alias with
  197. the given classname to stdClass and are generally used to enable the mocking
  198. of public static methods. Expectations set on the new mock object which refer
  199. to static methods will be used by all static calls to this class.
  200. .. code-block:: php
  201. $mock = \Mockery::mock('alias:MyClass');
  202. .. note::
  203. Even though aliasing classes is supported, we do not recommend it.
  204. Overloading
  205. -----------
  206. Prefixing the valid name of a class (which is NOT currently loaded) with
  207. "overload:" will generate an alias mock (as with "alias:") except that created
  208. new instances of that class will import any expectations set on the origin
  209. mock (``$mock``). The origin mock is never verified since it's used an
  210. expectation store for new instances. For this purpose we use the term "instance
  211. mock" to differentiate it from the simpler "alias mock".
  212. In other words, an instance mock will "intercept" when a new instance of the
  213. mocked class is created, then the mock will be used instead. This is useful
  214. especially when mocking hard dependencies which will be discussed later.
  215. .. code-block:: php
  216. $mock = \Mockery::mock('overload:MyClass');
  217. .. note::
  218. Using alias/instance mocks across more than one test will generate a fatal
  219. error since we can't have two classes of the same name. To avoid this,
  220. run each test of this kind in a separate PHP process (which is supported
  221. out of the box by both PHPUnit and PHPT).
  222. .. _creating-test-doubles-named-mocks:
  223. Named Mocks
  224. -----------
  225. The ``namedMock()`` method will generate a class called by the first argument,
  226. so in this example ``MyClassName``. The rest of the arguments are treated in the
  227. same way as the ``mock`` method:
  228. .. code-block:: php
  229. $mock = \Mockery::namedMock('MyClassName', 'DateTime');
  230. This example would create a class called ``MyClassName`` that extends
  231. ``DateTime``.
  232. Named mocks are quite an edge case, but they can be useful when code depends
  233. on the ``__CLASS__`` magic constant, or when we need two derivatives of an
  234. abstract type, that are actually different classes.
  235. See the cookbook entry on :doc:`../cookbook/class_constants` for an example
  236. usage of named mocks.
  237. .. note::
  238. We can only create a named mock once, any subsequent calls to
  239. ``namedMock``, with different arguments are likely to cause exceptions.
  240. .. _creating-test-doubles-constructor-arguments:
  241. Constructor Arguments
  242. ---------------------
  243. Sometimes the mocked class has required constructor arguments. We can pass these
  244. to Mockery as an indexed array, as the 2nd argument:
  245. .. code-block:: php
  246. $mock = \Mockery::mock('MyClass', [$constructorArg1, $constructorArg2]);
  247. or if we need the ``MyClass`` to implement an interface as well, as the 3rd
  248. argument:
  249. .. code-block:: php
  250. $mock = \Mockery::mock('MyClass', 'MyInterface', [$constructorArg1, $constructorArg2]);
  251. Mockery now knows to pass in ``$constructorArg1`` and ``$constructorArg2`` as
  252. arguments to the constructor.
  253. .. _creating-test-doubles-behavior-modifiers:
  254. Behavior Modifiers
  255. ------------------
  256. When creating a mock object, we may wish to use some commonly preferred
  257. behaviours that are not the default in Mockery.
  258. The use of the ``shouldIgnoreMissing()`` behaviour modifier will label this
  259. mock object as a Passive Mock:
  260. .. code-block:: php
  261. \Mockery::mock('MyClass')->shouldIgnoreMissing();
  262. In such a mock object, calls to methods which are not covered by expectations
  263. will return ``null`` instead of the usual error about there being no expectation
  264. matching the call.
  265. On PHP >= 7.0.0, methods with missing expectations that have a return type
  266. will return either a mock of the object (if return type is a class) or a
  267. "falsy" primitive value, e.g. empty string, empty array, zero for ints and
  268. floats, false for bools, or empty closures.
  269. On PHP >= 7.1.0, methods with missing expectations and nullable return type
  270. will return null.
  271. We can optionally prefer to return an object of type ``\Mockery\Undefined``
  272. (i.e. a ``null`` object) (which was the 0.7.2 behaviour) by using an
  273. additional modifier:
  274. .. code-block:: php
  275. \Mockery::mock('MyClass')->shouldIgnoreMissing()->asUndefined();
  276. The returned object is nothing more than a placeholder so if, by some act of
  277. fate, it's erroneously used somewhere it shouldn't, it will likely not pass a
  278. logic check.
  279. We have encountered the ``makePartial()`` method before, as it is the method we
  280. use to create runtime partial test doubles:
  281. .. code-block:: php
  282. \Mockery::mock('MyClass')->makePartial();
  283. This form of mock object will defer all methods not subject to an expectation to
  284. the parent class of the mock, i.e. ``MyClass``. Whereas the previous
  285. ``shouldIgnoreMissing()`` returned ``null``, this behaviour simply calls the
  286. parent's matching method.