Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
.. index::
2
    single: Mockery; Configuration
3
 
4
Mockery Global Configuration
5
============================
6
 
7
To allow for a degree of fine-tuning, Mockery utilises a singleton
8
configuration object to store a small subset of core behaviours. The three
9
currently present include:
10
 
11
* Option to allow/disallow the mocking of methods which do not actually exist
12
  fulfilled (i.e. unused)
13
* Setter/Getter for added a parameter map for internal PHP class methods
14
  (``Reflection`` cannot detect these automatically)
15
* Option to drive if quick definitions should define a stub or a mock with
16
  an 'at least once' expectation.
17
 
18
By default, the first behaviour is enabled. Of course, there are
19
situations where this can lead to unintended consequences. The mocking of
20
non-existent methods may allow mocks based on real classes/objects to fall out
21
of sync with the actual implementations, especially when some degree of
22
integration testing (testing of object wiring) is not being performed.
23
 
24
You may allow or disallow this behaviour (whether for whole test suites or
25
just select tests) by using the following call:
26
 
27
.. code-block:: php
28
 
29
    \Mockery::getConfiguration()->allowMockingNonExistentMethods(bool);
30
 
31
Passing a true allows the behaviour, false disallows it. It takes effect
32
immediately until switched back. If the behaviour is detected when not allowed,
33
it will result in an Exception being thrown at that point. Note that disallowing
34
this behaviour should be carefully considered since it necessarily removes at
35
least some of Mockery's flexibility.
36
 
37
The other two methods are:
38
 
39
.. code-block:: php
40
 
41
    \Mockery::getConfiguration()->setInternalClassMethodParamMap($class, $method, array $paramMap)
42
    \Mockery::getConfiguration()->getInternalClassMethodParamMap($class, $method)
43
 
44
These are used to define parameters (i.e. the signature string of each) for the
45
methods of internal PHP classes (e.g. SPL, or PECL extension classes like
46
ext/mongo's MongoCollection. Reflection cannot analyse the parameters of internal
47
classes. Most of the time, you never need to do this. It's mainly needed where an
48
internal class method uses pass-by-reference for a parameter - you MUST in such
49
cases ensure the parameter signature includes the ``&`` symbol correctly as Mockery
50
won't correctly add it automatically for internal classes. Note that internal class
51
parameter overriding is not available in PHP 8. This is because incompatible
52
signatures have been reclassified as fatal errors.
53
 
54
Finally there is the possibility to change what a quick definition produces.
55
By default quick definitions create stubs but you can change this behaviour
56
by asking Mockery to use 'at least once' expectations.
57
 
58
.. code-block:: php
59
 
60
    \Mockery::getConfiguration()->getQuickDefinitions()->shouldBeCalledAtLeastOnce(bool)
61
 
62
Passing a true allows the behaviour, false disallows it. It takes effect
63
immediately until switched back. By doing so you can avoid the proliferating of
64
quick definitions that accumulate overtime in your code since the test would
65
fail in case the 'at least once' expectation is not fulfilled.
66
 
67
Disabling reflection caching
68
----------------------------
69
 
70
Mockery heavily uses `"reflection" <https://secure.php.net/manual/en/book.reflection.php>`_
71
to do it's job. To speed up things, Mockery caches internally the information it
72
gathers via reflection. In some cases, this caching can cause problems.
73
 
74
The **only** known situation when this occurs is when PHPUnit's ``--static-backup`` option
75
is used. If you use ``--static-backup`` and you get an error that looks like the
76
following:
77
 
78
.. code-block:: php
79
 
80
    Error: Internal error: Failed to retrieve the reflection object
81
 
82
We suggest turning off the reflection cache as so:
83
 
84
.. code-block:: php
85
 
86
    \Mockery::getConfiguration()->disableReflectionCache();
87
 
88
Turning it back on can be done like so:
89
 
90
.. code-block:: php
91
 
92
    \Mockery::getConfiguration()->enableReflectionCache();
93
 
94
In no other situation should you be required turn this reflection cache off.