33![ Continuous Integration] ( https://github.com/tarantool-php/phpunit-extras/workflows/Continuous%20Integration/badge.svg )
44
55A collection of helpers for [ PHPUnit] ( https://phpunit.de/ ) to ease testing [ Tarantool] ( https://www.tarantool.io/en/developers/ ) libraries.
6+ It is based on [ rybakit/phpunit-extras] ( https://github.com/rybakit/phpunit-extras ) , please refer to this package for more documentation.
67
78
89## Table of contents
@@ -26,6 +27,81 @@ composer require --dev tarantool/phpunit-extras
2627
2728## Annotations
2829
30+ Besides the annotations provided by the package ` rybakit/phpunit-extras ` , the library is shipped
31+ with add-ons specific to Tarantool. The easiest way to enable them is by inheriting your test classes
32+ from the ` Tarantool\PhpUnit\TestCase ` class:
33+
34+ ``` php
35+ use Tarantool\Client\Client;
36+ use Tarantool\PhpUnit\TestCase;
37+
38+ final class MyTest extends TestCase
39+ {
40+ protected function getClient() : Client
41+ {
42+ // you have to implement this method
43+ }
44+
45+ // ...
46+ }
47+ ```
48+
49+ Another option is to register an extension called ` AnnotationExtension ` :
50+
51+ ``` xml
52+ <phpunit xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
53+ xsi : noNamespaceSchemaLocation =" vendor/phpunit/phpunit/phpunit.xsd"
54+ bootstrap =" vendor/autoload.php"
55+ >
56+ <!-- ... -->
57+
58+ <extensions >
59+ <extension class =" Tarantool\PhpUnit\Annotation\AnnotationExtension" />
60+ </extensions >
61+ </phpunit >
62+ ```
63+
64+ By default, the extension assumes that the Tarantool server you are going to connect to is available on ` 127.0.0.1:3301 ` .
65+ You can customize the default settings by specifying either a [ DSN string] ( https://github.com/tarantool-php/client#dsn-string ) or an [ array of options] ( https://github.com/tarantool-php/client#array-of-options )
66+ as extension configuration values:
67+
68+ ``` xml
69+ <extension class =" Tarantool\PhpUnit\Annotation" >
70+ <arguments >
71+ <string >tcp://127.0.0.1:3301/?socket_timeout=10</string >
72+ </arguments >
73+ </extension >
74+ ```
75+ or
76+ ``` xml
77+ <extension class =" Tarantool\PhpUnit\Annotation" >
78+ <arguments >
79+ <array >
80+ <element key =" uri" >
81+ <string >tcp://127.0.0.1:3301</string >
82+ </element >
83+ <element key =" socket_timeout" >
84+ <integer >10</integer >
85+ </element >
86+ </array >
87+ </arguments >
88+ </extension >
89+ ```
90+
91+ On top of that, the configuration values can resolve environment variables,
92+ which might be useful if you need to share the same settings with a Tarantool
93+ instance file or any other script:
94+
95+ ``` xml
96+ <extension class =" Tarantool\PhpUnit\Annotation" >
97+ <arguments >
98+ <string >tcp://%env(TARANTOOL_HOST)%:%env(TARANTOOL_PORT)%</string >
99+ </arguments >
100+ </extension >
101+ ```
102+
103+ Once the annotations are configured, you can start using them:
104+
29105### Processors
30106
31107* Lua*
@@ -74,9 +150,9 @@ public function testPackerUnpacksBigIntegerAsDecimal() : void
74150
75151``` php
76152/**
77- * @requires luaCondition box.space.foobar ~= nil
153+ * @requires luaCondition box.session.user() ~= 'guest'
78154 */
79- public function testUserCanAccessSpace () : void
155+ public function testChangeUserPassword () : void
80156{
81157 // ...
82158}
@@ -97,27 +173,54 @@ public function testPrepareCreatesPreparedStatement() : void
97173
98174## Expectations
99175
100- * RequestExpectations*
176+ To test that your code sends (or does not send) certain requests, the following methods are available:
177+
178+ * ` TestCase::expect<REQUEST_NAME>RequestToBeCalled(int $count) : void `
179+ * ` TestCase::expect<REQUEST_NAME>RequestToBeCalledAtLeast(int $count) : void `
180+ * ` TestCase::expect<REQUEST_NAME>RequestToBeCalledAtMost(int $count) : void `
181+ * ` TestCase::expect<REQUEST_NAME>RequestToBeCalledOnce() : void `
182+ * ` TestCase::expect<REQUEST_NAME>RequestToBeCalledAtLeastOnce() : void `
183+ * ` TestCase::expect<REQUEST_NAME>RequestToBeCalledAtMostOnce() : void `
184+ * ` TestCase::expect<REQUEST_NAME>RequestToBeNeverCalled() : void `
185+ * ` TestCase::expectNoRequestToBeCalled() : void `
186+
187+ where ` <REQUEST_NAME> ` is the name of the request, for example ` Call ` , ` Insert ` , etc.
188+ These methods are part of the ` Tarantool\PhpUnit\TestCase ` class, but can also be enabled through a trait:
189+
190+ ``` php
191+ use PHPUnit\Framework\TestCase;
192+ use Tarantool\PhpUnit\Expectation\RequestExpectations;
193+
194+ final class MyTest extends TestCase
195+ {
196+ use RequestExpectations;
197+
198+ // ...
199+ }
200+ ```
201+
202+ Usage example:
101203
102204``` php
103205public function testGetSpaceIsCached() : void
104206{
105207 $this->client->flushSpaces();
106208
107209 $this->expectSelectRequestToBeCalledOnce();
108- $this->client->getSpace('test_space')->delete(42) ;
109- $this->client->getSpace('test_space')->delete(42) ;
210+ $this->client->getSpace('test_space');
211+ $this->client->getSpace('test_space');
110212}
111213```
112214
113215
114216## Mocking
115217
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 :
218+ This library provides several helper classes to create test doubles for the [ Tarantool Сlient ] ( https://github.com/tarantool-php/client )
219+ to avoid sending real requests to the Tarantool server. For the convenience of creating such objects,
220+ add the trait ` ClientMocking ` to your test class :
119221
120222``` php
223+ use PHPUnit\Framework\TestCase;
121224use Tarantool\PhpUnit\Client\ClientMocking;
122225
123226final class MyTest extends TestCase
@@ -133,32 +236,38 @@ final class MyTest extends TestCase
133236> If your test cases extend the ` Tarantool\PhpUnit\TestCase ` class, this step is not needed
134237> because the trait is already included in that class.
135238
136- The most basic version of the mock object can be created as follows:
239+ A dummy client object can be created as follows:
137240
138241``` php
139242public function testFoo() : void
140243{
141- $mockClient = $this->createMockClient();
244+ $dummyClient = $this->createMockClient();
142245
143246 // ...
144247}
145248```
146249
147250To 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:
251+ or returning specific responses in a specific order from the server, use the facilities
252+ of the ` MockClientBuilder ` class. For example, to simulate ` PING ` request/response:
150253
151254``` php
152255use Tarantool\Client\Request\PingRequest;
153256use Tarantool\PhpUnit\Client\DummyFactory;
257+ use Tarantool\PhpUnit\TestCase;
154258
155- public function testFoo() : void
259+ final class MyTest extends TestCase
156260{
157- $mockClient = $this->getMockClientBuilder()
158- ->shouldHandle(
159- new PingRequest(),
160- DummyFactory::createEmptyResponse()
161- )->build();
261+ public function testFoo() : void
262+ {
263+ $mockClient = $this->getMockClientBuilder()
264+ ->shouldHandle(
265+ new PingRequest(),
266+ DummyFactory::createEmptyResponse()
267+ )->build();
268+
269+ // ...
270+ }
162271
163272 // ...
164273}
@@ -169,15 +278,21 @@ Another example, sending two `EVALUATE` requests and returning a different respo
169278``` php
170279use Tarantool\Client\RequestTypes;
171280use Tarantool\PhpUnit\Client\DummyFactory;
281+ use Tarantool\PhpUnit\TestCase;
172282
173- public function testFoo() : void
283+ final class MyTest extends TestCase
174284{
175- $mockClient = $this->getMockClientBuilder()
176- ->shouldHandle(
177- RequestTypes::EVALUATE,
178- DummyFactory::createResponseFromData([2]),
179- DummyFactory::createResponseFromData([3])
180- )->build();
285+ public function testFoo() : void
286+ {
287+ $mockClient = $this->getMockClientBuilder()
288+ ->shouldHandle(
289+ RequestTypes::EVALUATE,
290+ DummyFactory::createResponseFromData([2]),
291+ DummyFactory::createResponseFromData([3])
292+ )->build();
293+
294+ // ...
295+ }
181296
182297 // ...
183298}
@@ -186,7 +301,7 @@ public function testFoo() : void
186301Besides, the builder allows setting custom ` Connection ` and ` Packer ` instances:
187302
188303``` php
189- $mockClient = $this->getMockClientBuilder()
304+ $stubClient = $this->getMockClientBuilder()
190305 ->willUseConnection($myConnection)
191306 ->willUsePacker($myPacker)
192307 ->build();
0 commit comments