|
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -A collection of helpers for [PHPUnit](https://phpunit.de/) to ease testing [Tarantool](https://www.tarantool.io/en/developers/) libraries. |
| 5 | +A collection of helpers for [PHPUnit](https://phpunit.de/) to ease testing [Tarantool](https://www.tarantool.io/en/developers/) libraries. |
| 6 | + |
| 7 | + |
| 8 | +## Table of contents |
| 9 | + |
| 10 | + * [Installation](#installation) |
| 11 | + * [Annotations](#annotations) |
| 12 | + * [Processors](#processors) |
| 13 | + * [Requirements](#requirements) |
| 14 | + * [Expectations](#expectations) |
| 15 | + * [Mocking](#mocking) |
| 16 | + * [Testing](#testing) |
| 17 | + * [License](#license) |
6 | 18 |
|
7 | 19 |
|
8 | 20 | ## Installation |
@@ -85,6 +97,116 @@ public function testPrepareCreatesPreparedStatement() : void |
85 | 97 |
|
86 | 98 | ## Expectations |
87 | 99 |
|
| 100 | +*RequestExpectations* |
| 101 | + |
| 102 | +```php |
| 103 | +public function testGetSpaceIsCached() : void |
| 104 | +{ |
| 105 | + $this->client->flushSpaces(); |
| 106 | + |
| 107 | + $this->expectSelectRequestToBeCalledOnce(); |
| 108 | + $this->client->getSpace('test_space')->delete(42); |
| 109 | + $this->client->getSpace('test_space')->delete(42); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +## Mocking |
| 115 | + |
| 116 | +This library provides several helper classes to mock the [Tarantool Client](https://github.com/tarantool-php/client) |
| 117 | +without having to send real requests to the Tarantool server. For convenient creation of mocks in your tests, |
| 118 | +add a `ClientMocking` trait to your test case: |
| 119 | + |
| 120 | +```php |
| 121 | +use Tarantool\PhpUnit\Client\ClientMocking; |
| 122 | + |
| 123 | +final class MyTest extends TestCase |
| 124 | +{ |
| 125 | + use ClientMocking; |
| 126 | + |
| 127 | + // ... |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +> *Note* |
| 132 | +> |
| 133 | +> If your test cases extend the `Tarantool\PhpUnit\TestCase` class, this step is not needed |
| 134 | +> because the trait is already included in that class. |
| 135 | +
|
| 136 | +The most basic version of the mock object can be created as follows: |
| 137 | + |
| 138 | +```php |
| 139 | +public function testFoo() : void |
| 140 | +{ |
| 141 | + $mockClient = $this->createMockClient(); |
| 142 | + |
| 143 | + // ... |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +To simulate specific scenarios, such as establishing a connection to a server |
| 148 | +or returning specific responses in a specific order from a server, use the facilities |
| 149 | +of the `MockClientBuilder` class. For example, to simulate the ping request/response: |
| 150 | + |
| 151 | +```php |
| 152 | +use Tarantool\Client\Request\PingRequest; |
| 153 | +use Tarantool\PhpUnit\Client\DummyFactory; |
| 154 | + |
| 155 | +public function testFoo() : void |
| 156 | +{ |
| 157 | + $mockClient = $this->getMockClientBuilder() |
| 158 | + ->shouldHandle( |
| 159 | + new PingRequest(), |
| 160 | + DummyFactory::createEmptyResponse() |
| 161 | + )->build(); |
| 162 | + |
| 163 | + // ... |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +Another example, sending two `EVALUATE` requests and returning a different response for each: |
| 168 | + |
| 169 | +```php |
| 170 | +use Tarantool\Client\RequestTypes; |
| 171 | +use Tarantool\PhpUnit\Client\DummyFactory; |
| 172 | + |
| 173 | +public function testFoo() : void |
| 174 | +{ |
| 175 | + $mockClient = $this->getMockClientBuilder() |
| 176 | + ->shouldHandle( |
| 177 | + RequestTypes::EVALUATE, |
| 178 | + DummyFactory::createResponseFromData([2]), |
| 179 | + DummyFactory::createResponseFromData([3]) |
| 180 | + )->build(); |
| 181 | + |
| 182 | + // ... |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +Besides, the builder allows setting custom `Connection` and `Packer` instances: |
| 187 | + |
| 188 | +```php |
| 189 | +$mockClient = $this->getMockClientBuilder() |
| 190 | + ->willUseConnection($myConnection) |
| 191 | + ->willUsePacker($myPacker) |
| 192 | + ->build(); |
| 193 | +``` |
| 194 | + |
| 195 | +## Testing |
| 196 | + |
| 197 | +Before running tests, the development dependencies must be installed: |
| 198 | + |
| 199 | +```bash |
| 200 | +composer install |
| 201 | +``` |
| 202 | + |
| 203 | +Then, to run all the tests: |
| 204 | + |
| 205 | +```bash |
| 206 | +vendor/bin/phpunit |
| 207 | +vendor/bin/phpunit -c phpunit-extension.xml |
| 208 | +``` |
| 209 | + |
88 | 210 |
|
89 | 211 | ## License |
90 | 212 |
|
|
0 commit comments