Skip to content

Commit 06abbb3

Browse files
committed
Update expectations
1 parent 3e8c8a2 commit 06abbb3

File tree

10 files changed

+691
-284
lines changed

10 files changed

+691
-284
lines changed

README.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ public function testExecuteQueryFetchesAllRows() : void
136136

137137
*ClientPacker*
138138

139+
Format:
140+
141+
```
142+
@requires clientPacker <packer>
143+
```
144+
where `<packer>` is either `pure`, `pecl`, or a fully qualified class name, e.g. `Tarantool\Client\Packer\PurePacker`.
145+
146+
Example:
147+
139148
```php
140149
/**
141150
* @requires clientPacker pure
@@ -148,6 +157,15 @@ public function testPackerUnpacksBigIntegerAsDecimal() : void
148157

149158
*LuaCondition*
150159

160+
Format:
161+
162+
```
163+
@requires luaCondition <lua-expression>
164+
```
165+
where `<lua-expression>` is an arbitrary lua expression that should be evaluated to a Boolean value.
166+
167+
Example:
168+
151169
```php
152170
/**
153171
* @requires luaCondition box.session.user() ~= 'guest'
@@ -160,6 +178,16 @@ public function testChangeUserPassword() : void
160178

161179
*Tarantool*
162180

181+
Format:
182+
183+
```
184+
@requires Tarantool <version-constraint>
185+
```
186+
where `<version-constraint>` is a composer-like version constraint. For details on supported formats,
187+
please see the Composer [documentation](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints).
188+
189+
Example:
190+
163191
```php
164192
/**
165193
* @requires Tarantool ^2.3.2
@@ -170,6 +198,11 @@ public function testPrepareCreatesPreparedStatement() : void
170198
}
171199
```
172200

201+
> *Note*
202+
>
203+
> If you're interested in how to create and register your own annotations,
204+
> please refer to the `rybakit/phpunit-extras` [README](https://github.com/rybakit/phpunit-extras).
205+
173206

174207
## Expectations
175208

@@ -212,16 +245,18 @@ public function testGetSpaceIsCached() : void
212245
}
213246
```
214247

215-
In order to check SQL statements, use the `Tarantool\PhpUnit\Expectation\SqlStatementExpectations` trait,
248+
In order to assert prepared statement allocations, use the `Tarantool\PhpUnit\Expectation\PreparedStatementExpectations` trait,
216249
which contains the following methods:
217250

218-
* `expectSqlStatementToBeExecuted(int $count) : void`
219-
* `expectSqlStatementToBeExecutedAtLeast(int $count) : void`
220-
* `expectSqlStatementToBeExecutedAtMost(int $count) : void`
221-
* `expectSqlStatementToBeExecutedOnce() : void`
222-
* `expectSqlStatementToBeNeverCalled() : void`
223-
* `expectSqlStatementToBeExecutedAtLeastOnce() : void`
224-
* `expectSqlStatementToBeExecutedAtMostOnce() : void`
251+
* `expectPreparedStatementToBe<TYPE>(int $count) : void`
252+
* `expectPreparedStatementToBe<TYPE>AtLeast(int $count) : void`
253+
* `expectPreparedStatementToBe<TYPE>AtMost(int $count) : void`
254+
* `expectPreparedStatementToBe<TYPE>Once() : void`
255+
* `expectPreparedStatementToBeNever<TYPE>() : void`
256+
* `expectPreparedStatementToBe<TYPE>AtLeastOnce() : void`
257+
* `expectPreparedStatementToBe<TYPE>AtMostOnce() : void`
258+
259+
where `<TYPE>` is either `Allocated` or `Deallocated`.
225260

226261
Usage example:
227262

@@ -230,7 +265,7 @@ public function testCloseDeallocatesPreparedStatement() : void
230265
{
231266
$stmt = $this->client->prepare('SELECT ?');
232267

233-
$this->expectSqlStatementToBeExecutedOnce();
268+
$this->expectPreparedStatementToBeDeallocatedOnce();
234269
$stmt->close();
235270
}
236271
```

src/Expectation/Expectations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
trait Expectations
1717
{
1818
use RequestExpectations;
19-
use SqlStatementExpectations;
19+
use PreparedStatementExpectations;
2020
}

src/Expectation/ExpressionContext/SqlStatementCountContext.php renamed to src/Expectation/ExpressionContext/PreparedStatementCountContext.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPUnitExtras\Expectation\ExpressionContext;
1717
use Tarantool\Client\Client;
1818

19-
final class SqlStatementCountContext implements ExpressionContext
19+
final class PreparedStatementCountContext implements ExpressionContext
2020
{
2121
/** @var Client */
2222
private $client;
@@ -27,6 +27,9 @@ final class SqlStatementCountContext implements ExpressionContext
2727
/** @var int */
2828
private $initialValue;
2929

30+
/** @var int|null */
31+
private $finalValue;
32+
3033
private function __construct(Client $client, string $expression)
3134
{
3235
$this->client = $client;
@@ -56,9 +59,13 @@ public function getExpression() : string
5659

5760
public function getValues() : array
5861
{
62+
if (null === $this->finalValue) {
63+
$this->finalValue = $this->getValue();
64+
}
65+
5966
return [
6067
'old_count' => $this->initialValue,
61-
'new_count' => $this->getValue(),
68+
'new_count' => $this->finalValue,
6269
];
6370
}
6471

src/Expectation/ExpressionContext/RequestCountContext.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ final class RequestCountContext implements ExpressionContext
4848
/** @var int */
4949
private $initialValue;
5050

51+
/** @var int|null */
52+
private $finalValue;
53+
5154
private function __construct(Client $client, RequestCounter $requestCounter, string $requestName, string $expression)
5255
{
5356
$this->requestCounter = $requestCounter;
@@ -79,9 +82,13 @@ public function getExpression() : string
7982

8083
public function getValues() : array
8184
{
85+
if (null === $this->finalValue) {
86+
$this->finalValue = $this->getValue();
87+
}
88+
8289
return [
8390
'old_count' => $this->initialValue,
84-
'new_count' => $this->getValue(),
91+
'new_count' => $this->finalValue,
8592
];
8693
}
8794

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/phpunit-extras package.
5+
*
6+
* (c) Eugene Leonovich <gen.work@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Tarantool\PhpUnit\Expectation;
15+
16+
use PHPUnitExtras\Expectation\Expectation;
17+
use PHPUnitExtras\Expectation\ExpressionExpectation;
18+
use Tarantool\Client\Client;
19+
use Tarantool\PhpUnit\Expectation\ExpressionContext\PreparedStatementCountContext;
20+
21+
trait PreparedStatementExpectations
22+
{
23+
public function expectPreparedStatementToBeAllocated(int $count) : void
24+
{
25+
$context = PreparedStatementCountContext::exactly($this->getClient(), $count);
26+
$this->expect(new ExpressionExpectation($context));
27+
}
28+
29+
public function expectPreparedStatementToBeDeallocated(int $count) : void
30+
{
31+
$context = PreparedStatementCountContext::exactly($this->getClient(), -$count);
32+
$this->expect(new ExpressionExpectation($context));
33+
}
34+
35+
public function expectPreparedStatementToBeAllocatedAtLeast(int $count) : void
36+
{
37+
$context = PreparedStatementCountContext::atLeast($this->getClient(), $count);
38+
$this->expect(new ExpressionExpectation($context));
39+
}
40+
41+
public function expectPreparedStatementToBeDeallocatedAtLeast(int $count) : void
42+
{
43+
$context = PreparedStatementCountContext::atMost($this->getClient(), -$count);
44+
$this->expect(new ExpressionExpectation($context));
45+
}
46+
47+
public function expectPreparedStatementToBeAllocatedAtMost(int $count) : void
48+
{
49+
$context = PreparedStatementCountContext::atMost($this->getClient(), $count);
50+
$this->expect(new ExpressionExpectation($context));
51+
}
52+
53+
public function expectPreparedStatementToBeDeallocatedAtMost(int $count) : void
54+
{
55+
$context = PreparedStatementCountContext::atLeast($this->getClient(), -$count);
56+
$this->expect(new ExpressionExpectation($context));
57+
}
58+
59+
public function expectPreparedStatementToBeAllocatedOnce() : void
60+
{
61+
$this->expectPreparedStatementToBeAllocated(1);
62+
}
63+
64+
public function expectPreparedStatementToBeDeallocatedOnce() : void
65+
{
66+
$this->expectPreparedStatementToBeDeallocated(1);
67+
}
68+
69+
public function expectPreparedStatementToBeNeverAllocated() : void
70+
{
71+
$this->expectPreparedStatementToBeAllocated(0);
72+
}
73+
74+
public function expectPreparedStatementToBeNeverDeallocated() : void
75+
{
76+
$this->expectPreparedStatementToBeDeallocated(0);
77+
}
78+
79+
public function expectPreparedStatementToBeAllocatedAtLeastOnce() : void
80+
{
81+
$this->expectPreparedStatementToBeAllocatedAtLeast(1);
82+
}
83+
84+
public function expectPreparedStatementToBeDeallocatedAtLeastOnce() : void
85+
{
86+
$this->expectPreparedStatementToBeDeallocatedAtLeast(1);
87+
}
88+
89+
public function expectPreparedStatementToBeAllocatedAtMostOnce() : void
90+
{
91+
$this->expectPreparedStatementToBeAllocatedAtMost(1);
92+
}
93+
94+
public function expectPreparedStatementToBeDeallocatedAtMostOnce() : void
95+
{
96+
$this->expectPreparedStatementToBeDeallocatedAtMost(1);
97+
}
98+
99+
abstract protected function expect(Expectation $expectation) : void;
100+
101+
abstract protected function getClient() : Client;
102+
}

src/Expectation/SqlStatementExpectations.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)