File: mocking_class_within_class.rst

package info (click to toggle)
php-mockery 1.6.12-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,988 kB
  • sloc: php: 12,443; xml: 1,716; makefile: 204; sh: 47; python: 43
file content (146 lines) | stat: -rw-r--r-- 4,413 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
.. index::
    single: Cookbook; Mocking class within class

.. _mocking-class-within-class:

Mocking class within class
==========================

Imagine a case where you need to create an instance of a class and use it
within the same method:

.. code-block:: php

    // Point.php
    <?php
    namespace App;

    class Point {
        public function setPoint($x, $y) {
            echo "Point (" . $x . ", " . $y . ")" . PHP_EOL;
        }
    }

    // Rectangle.php
    <?php
    namespace App;
    use App\Point;

    class Rectangle {
        public function create($x1, $y1, $x2, $y2) {
            $a = new Point();
            $a->setPoint($x1, $y1);

            $b = new Point();
            $b->setPoint($x2, $y1);

            $c = new Point();
            $c->setPoint($x2, $y2);

            $d = new Point();
            $d->setPoint($x1, $y2);

            $this->draw([$a, $b, $c, $d]);
        }

        public function draw($points) {
            echo "Do something with the points";
        }
    }

And that you want to test that a logic in ``Rectangle->create()`` calls
properly each used thing - in this case calls ``Point->setPoint()``, but
``Rectangle->draw()`` does some graphical stuff that you want to avoid calling.

You set the mocks for ``App\Point`` and ``App\Rectangle``:

.. code-block:: php

    <?php
    class MyTest extends PHPUnit\Framework\TestCase {
        public function testCreate() {
            $point = Mockery::mock("App\Point");
            // check if our mock is called
            $point->shouldReceive("setPoint")->andThrow(Exception::class);

            $rect = Mockery::mock("App\Rectangle")->makePartial();
            $rect->shouldReceive("draw");

            $rect->create(0, 0, 100, 100);  // does not throw exception
            Mockery::close();
        }
    }

and the test does not work. Why? The mocking relies on the class not being
present yet, but the class is autoloaded therefore the mock alone for
``App\Point`` is useless which you can see with ``echo`` being executed.

Mocks however work for the first class in the order of loading i.e.
``App\Rectangle``, which loads the ``App\Point`` class. In more complex example
that would be a single point that initiates the whole loading (``use Class``)
such as::

    A        // main loading initiator
    |- B     // another loading initiator
    |  |-E
    |  +-G
    |
    |- C     // another loading initiator
    |  +-F
    |
    +- D

That basically means that the loading prevents mocking and for each such
a loading initiator there needs to be implemented a workaround.
Overloading is one approach, however it pollutes the global state. In this case
we try to completely avoid the global state pollution with custom
``new Class()`` behavior per loading initiator and that can be mocked easily
in few critical places.

That being said, although we can't stop loading, we can return mocks. Let's
look at ``Rectangle->create()`` method:

.. code-block:: php

    class Rectangle {
        public function newPoint() {
            return new Point();
        }

        public function create($x1, $y1, $x2, $y2) {
            $a = $this->newPoint();
            $a->setPoint($x1, $y1);
            ...
        }
        ...
    }

We create a custom function to encapsulate ``new`` keyword that would otherwise
just use the autoloaded class ``App\Point`` and in our test we mock that function
so that it returns our mock:

.. code-block:: php

    <?php
    class MyTest extends PHPUnit\Framework\TestCase {
        public function testCreate() {
            $point = Mockery::mock("App\Point");
            // check if our mock is called
            $point->shouldReceive("setPoint")->andThrow(Exception::class);

            $rect = Mockery::mock("App\Rectangle")->makePartial();
            $rect->shouldReceive("draw");

            // pass the App\Point mock into App\Rectangle as an alternative
            // to using new App\Point() in-place.
            $rect->shouldReceive("newPoint")->andReturn($point);

            $this->expectException(Exception::class);
            $rect->create(0, 0, 100, 100);
            Mockery::close();
        }
    }

If we run this test now, it should pass. For more complex cases we'd find
the next loader in the program flow and proceed with wrapping and passing
mock instances with predefined behavior into already existing classes.