|
1 | 1 | # Creating a double |
2 | 2 |
|
3 | 3 | ## Creating a double instance |
4 | | -The "sitphp/doubles" library can help you to make doubles of any kind of class, interface or trait easily. It doesn't matter if it has final, abstract or static calls. |
| 4 | + |
| 5 | +The "sitphp/doubles" library can help you to make doubles of any kind of class, interface or trait easily. It doesn't |
| 6 | +matter if it has final, abstract or static calls. |
5 | 7 |
|
6 | 8 | ### Double instance of type "dummy" |
7 | | -A doubles of type "dummy" will overwrite the original class methods to return `null`. If you are not sure what doubles of type "dummy" are, you can get more details on the [introduction page](/doc/intro). To create a "dummy" double of a given class use the `dummy` method. : |
| 9 | + |
| 10 | +A doubles of type "dummy" will overwrite the original class methods to return `null`. If you are not sure what doubles |
| 11 | +of type "dummy" are, you can get more details on the [introduction page](/doc/intro). To create a "dummy" double of a |
| 12 | +given class use the `dummy` method. : |
8 | 13 |
|
9 | 14 | ```php |
10 | 15 | // Get a double instance of type "dummy" for "MyClass" class, interface or trait |
@@ -13,11 +18,15 @@ $my_double = Double::dummy(MyClass::class)->getInstance(); |
13 | 18 | // This will return null |
14 | 19 | $my_double->myMethod(); |
15 | 20 | ``` |
16 | | - |
| 21 | + |
17 | 22 | > {.note .info} Note : The constructor will not be executed so you don't need to pass any of the constructor arguments to the `getInstance` method. |
18 | | - |
| 23 | +
|
19 | 24 | ### Double instance of type "mock" |
20 | | -A doubles of type "mock" will overwrite the original class methods to behave exactly the same as in the original class. If you are not sure what doubles of type "mock" are, you can get more details on the [introduction page](/doc/intro). You should use a "mock" double when you want to leave the behaviour of the original class unchanged. To get a "mock" double of a given class use the `mock` method and pass the constructor arguments to the `getInstance` method : |
| 25 | + |
| 26 | +A doubles of type "mock" will overwrite the original class methods to behave exactly the same as in the original class. |
| 27 | +If you are not sure what doubles of type "mock" are, you can get more details on the [introduction page](/doc/intro). |
| 28 | +You should use a "mock" double when you want to leave the behaviour of the original class unchanged. To get a "mock" |
| 29 | +double of a given class use the `mock` method and pass the constructor arguments to the `getInstance` method : |
21 | 30 |
|
22 | 31 | ```php |
23 | 32 | // Get a double instance of type "mock" for "MyClass" class, interface or trait |
@@ -27,15 +36,20 @@ $my_double = Double::mock(MyClass::class)->getInstance($arg1, $arg2); |
27 | 36 | $my_double->myMethod(); |
28 | 37 | ``` |
29 | 38 |
|
30 | | - |
31 | | -### Double instance of type "alias" |
32 | | -Doubles of type "alias" can be used to create doubles of non existent classes or to override existent classes. Overriding an existent class might be useful when you cannot inject the double in the class you want to test.You should avoid using "alias" doubles as much as possible because they have drawbacks. : |
| 39 | +### Double instance of type "alias" |
33 | 40 |
|
34 | | -- An "alias" double will not implement any method from the original class. You will have to define yourself which methods you want to build your "alias" double with. |
| 41 | +Doubles of type "alias" can be used to create doubles of non existent classes or to override existent classes. |
| 42 | +Overriding an existent class might be useful when you cannot inject the double in the class you want to test.You should |
| 43 | +avoid using "alias" doubles as much as possible because they have drawbacks. : |
| 44 | + |
| 45 | +- An "alias" double will not implement any method from the original class. You will have to define yourself which |
| 46 | + methods you want to build your "alias" double with. |
35 | 47 | - All "alias" double implemented methods will return `null` by default an cannot behave like the original class methods. |
36 | | -- If you are using "alias" doubles to replace an existent class, this class should not be loaded before you create your "alias" double. Otherwise you will receive an error . |
37 | | - |
38 | | -To get an "alias" double use the `alias` method. Then add the methods you want to implement with the `addMethod` method : |
| 48 | +- If you are using "alias" doubles to replace an existent class, this class should not be loaded before you create |
| 49 | + your "alias" double. Otherwise you will receive an error . |
| 50 | + |
| 51 | +To get an "alias" double use the `alias` method. Then add the methods you want to implement with the `addMethod` |
| 52 | +method : |
39 | 53 |
|
40 | 54 | ```php |
41 | 55 | // Get an double instance of type "alias" for non existent class "MyNonExistentClass" with methods "myMethod" and "myOtherMethod" |
@@ -54,19 +68,25 @@ $my_class_to_test->methodUsingMyClass(); |
54 | 68 | ``` |
55 | 69 |
|
56 | 70 | ## Get a double class name |
57 | | -Sometimes, you may need to instantiate the double yourself or you may not need a double instance at all (if you are working with a static class for example). You can get the class name of the generated double only with the `getClass` method. |
58 | | - |
| 71 | + |
| 72 | +Sometimes, you may need to instantiate the double yourself or you may not need a double instance at all (if you are |
| 73 | +working with a static class for example). You can get the class name of the generated double only with the `getClass` |
| 74 | +method. |
| 75 | + |
59 | 76 | ```php |
60 | 77 | // Get class name of double of type "dummy" for class "MyClass" |
61 | 78 | $my_dummy_double_class = Double::dummy(MyClass::class)->getClass(); |
62 | 79 |
|
63 | 80 | $my_dummy_double_class::foo(); |
64 | 81 | ``` |
65 | | - |
66 | | - |
| 82 | + |
67 | 83 | ## Implementing undefined methods |
68 | | -When you create a double, it will automatically implements all the methods of the original class (unless you're creating an [alias](#double-instance-of-type-alias) double). If you want your double to implement some methods that are not present in the original class, use the `addMethod` method. If you want to implement a static method, prefix it with the "static:" keyword : |
69 | | - |
| 84 | + |
| 85 | +When you create a double, it will automatically implements all the methods of the original class (unless you're creating |
| 86 | +an [alias](#double-instance-of-type-alias) double). If you want your double to implement some methods that are not |
| 87 | +present in the original class, use the `addMethod` method. If you want to implement a static method, prefix it with |
| 88 | +the "static:" keyword : |
| 89 | + |
70 | 90 | ```php |
71 | 91 | // Get a double instance with method "myMethod" and static method "myOtherMethod" |
72 | 92 | $my_double = Double::dummy(MyClass::class) |
@@ -75,17 +95,20 @@ $my_double = Double::dummy(MyClass::class) |
75 | 95 | ``` |
76 | 96 |
|
77 | 97 | ## Passing constructor arguments |
| 98 | + |
78 | 99 | You can pass constructor arguments in the getInstance method : |
79 | | - |
| 100 | + |
80 | 101 | ```php |
81 | 102 | // Get double instance and run original constructor with arguments "first_argument" and "second_argument" |
82 | 103 | $my_dummy_double = Double::dummy(MyClass::class)->getInstance($arg1, $arg2); |
83 | 104 | ``` |
84 | | - |
| 105 | + |
85 | 106 | > {.note .info} Note : When you pass constructor arguments, the original `__construct` method will be called with the given arguments regardless if your double is of type "dummy" or "mock". In the same way, if you don't pass constructor arguments, the original `__construct` method will not be called regardless if your double is of type "dummy" or "mock". |
86 | 107 |
|
87 | 108 | ## Implementing interfaces and trait |
88 | | -Use `addInterface` and/or `addTrait` to declare the interfaces and/or traits you want to implement in your double class : |
| 109 | + |
| 110 | +Use `addInterface` and/or `addTrait` to declare the interfaces and/or traits you want to implement in your double |
| 111 | +class : |
89 | 112 |
|
90 | 113 | ```php |
91 | 114 | // Implement one interface |
@@ -98,11 +121,13 @@ $my_dummy_double = Double::mock_instance(MyClass::class) |
98 | 121 | ->addInterface([MyInterface::class, MyOtherInterface::class]) |
99 | 122 | ->getInstance(); |
100 | 123 | ``` |
101 | | - |
| 124 | + |
102 | 125 | > {.note .info} Note : Every double class will automatically implement the `Doubles\DoubleInterface` interface in case you need to identify a double instance. |
103 | 126 |
|
104 | | -## Naming the double class |
105 | | -Double class names are automaticaly generated but you can also set the name of the double class yourself with the `setName` method : |
| 127 | +## Naming the double class |
| 128 | + |
| 129 | +Double class names are automaticaly generated but you can also set the name of the double class yourself with |
| 130 | +the `setName` method : |
106 | 131 |
|
107 | 132 | ```php |
108 | 133 | // Get a double instance of class "MyClass" with class name "MyDoubleClassName". |
|