Skip to content

Commit 0fa1949

Browse files
authored
Use single $path argument in Queue::statistics()
* Change Queue::statistics() signature * Throw an exception if stats path is wrong * Use the latest version of tarantool-queue * Fix phpunit warning
1 parent 9a11a4f commit 0fa1949

File tree

6 files changed

+106
-37
lines changed

6 files changed

+106
-37
lines changed

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ env:
1111
- PHP_RUNTIME='php:5.6-cli' PHPUNIT_OPTS='--coverage-clover=coverage.clover'
1212

1313
install:
14+
- docker build -t tarantool/tarantool github.com/tarantool/tarantool-docker#:debian
1415
- ./dockerfile.sh | docker build -t queue -
1516

1617
script:
1718
- docker run -d --name tarantool -v $(pwd):/queue tarantool/tarantool /queue/tests/Integration/queues.lua
1819
- docker run --rm --name queue --link tarantool -v $(pwd):/queue -w /queue -e PHPUNIT_OPTS="$PHPUNIT_OPTS" queue
1920

2021
after_script:
21-
- docker run --rm --name queue -v $(pwd):/queue -w /queue queue bash -c "
22-
if [[ -f coverage.clover ]]; then
23-
curl -sSOL https://scrutinizer-ci.com/ocular.phar &&
24-
php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
25-
fi
26-
"
22+
- if [[ -f coverage.clover ]]; then
23+
curl -sSOL https://scrutinizer-ci.com/ocular.phar &&
24+
php ocular.phar code-coverage:upload --format=php-clover coverage.clover;
25+
fi

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ In addition, you can specify a key (or keys) to return only a subset of the arra
211211

212212
```php
213213
$calls = $queue->statistics('calls');
214-
$total = $queue->statistics('tasks', 'total');
214+
$total = $queue->statistics('tasks.total');
215215
```
216216

217217

src/Queue.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,26 @@ public function delete($taskId)
126126
}
127127

128128
/**
129-
* @return array|int|null
129+
* @param string|null $path
130+
*
131+
* @return array|int
132+
*
133+
* @throws \InvalidArgumentException
130134
*/
131-
public function statistics(/* ... */)
135+
public function statistics($path = null)
132136
{
133137
$result = $this->client->call('queue.statistics', [$this->tubeName]);
134138

135-
if (empty($result[0][0])) {
136-
return;
139+
if (null === $path) {
140+
return $result[0][0];
137141
}
138142

139143
$result = $result[0][0];
140-
foreach (func_get_args() as $arg) {
141-
if (!isset($result[$arg])) {
142-
return;
144+
foreach (explode('.', $path) as $key) {
145+
if (!isset($result[$key])) {
146+
throw new \InvalidArgumentException(sprintf('Invalid path "%s".', $path));
143147
}
144-
$result = $result[$arg];
148+
$result = $result[$key];
145149
}
146150

147151
return $result;

tests/Integration/QueueTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,31 @@ public function testStatisticsIsEmpty()
228228
{
229229
$stat = $this->queue->statistics();
230230

231-
$this->assertSame([
231+
$this->assertEquals([
232232
'tasks' => [
233233
'taken' => 0,
234234
'buried' => 0,
235235
'ready' => 0,
236+
'done' => 0,
236237
'delayed' => 0,
237238
'total' => 0,
238239
],
239240
'calls' => [
241+
'ack' => 0,
242+
'bury' => 0,
243+
'delete' => 0,
244+
'kick' => 0,
245+
'put' => 0,
246+
'release' => 0,
247+
'take' => 0,
240248
],
241249
], $stat);
242250
}
243251

244252
/**
245253
* @dataProvider provideFailureCallbackData
246254
* @expectedException \Exception
247-
* @expectedExceptionMessageRegex ^Query error
255+
* @expectedExceptionMessageRegExp /^Query error/
248256
*/
249257
public function testThrowException($methodName, array $args)
250258
{

tests/Integration/Ttl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function testDelayedRelease()
9393
*/
9494
public function testStatisticsDelayed()
9595
{
96-
$count = $this->queue->statistics('tasks', 'delayed');
96+
$count = $this->queue->statistics('tasks.delayed');
9797

9898
$this->assertSame(2, $count);
9999
}

tests/Unit/QueueTest.php

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,39 @@
1616

1717
class QueueTest extends \PHPUnit_Framework_TestCase
1818
{
19+
/**
20+
* @var \Tarantool|\PHPUnit_Framework_MockObject_MockObject
21+
*/
1922
private $client;
23+
24+
/**
25+
* @var Queue
26+
*/
2027
private $queue;
2128

29+
private static $stats = [
30+
'tasks' => [
31+
'taken' => 1,
32+
'buried' => 2,
33+
'ready' => 3,
34+
'done' => 4,
35+
'delayed' => 5,
36+
'total' => 15,
37+
],
38+
'calls' => [
39+
'ack' => 1,
40+
'delete' => 2,
41+
'take' => 3,
42+
'kick' => 4,
43+
'release' => 5,
44+
'put' => 6,
45+
'bury' => 7,
46+
],
47+
];
48+
2249
protected function setUp()
2350
{
24-
$this->client = $this->getMock('Tarantool');
51+
$this->client = $this->getMockBuilder('Tarantool')->setMethods(['call'])->getMock();
2552
$this->queue = new Queue($this->client, 'foo');
2653
}
2754

@@ -64,36 +91,67 @@ public function provideApiMethodData()
6491
/**
6592
* @dataProvider provideStatisticsData
6693
*/
67-
public function testStatistics(array $args, array $returnValue, $result)
94+
public function testStatistics(array $stats, $result, $path = null)
6895
{
6996
$this->client->expects($this->once())->method('call')
7097
->with('queue.statistics')
71-
->will($this->returnValue([$returnValue]));
98+
->willReturn([[$stats]]);
7299

73-
$actualResult = call_user_func_array([$this->queue, 'statistics'], $args);
100+
$actualResult = 3 === func_num_args()
101+
? $this->queue->statistics($path)
102+
: $this->queue->statistics();
74103

75104
$this->assertSame($result, $actualResult);
76105
}
77106

78107
public function provideStatisticsData()
79108
{
80-
$stats = ['tasks' => ['ready' => 1, 'done' => 0], 'calls' => ['put' => 3]];
109+
return [
110+
[self::$stats, self::$stats],
111+
[self::$stats, self::$stats['tasks'], 'tasks'],
112+
[self::$stats, self::$stats['tasks']['taken'], 'tasks.taken'],
113+
[self::$stats, self::$stats['tasks']['buried'], 'tasks.buried'],
114+
[self::$stats, self::$stats['tasks']['ready'], 'tasks.ready'],
115+
[self::$stats, self::$stats['tasks']['done'], 'tasks.done'],
116+
[self::$stats, self::$stats['tasks']['delayed'], 'tasks.delayed'],
117+
[self::$stats, self::$stats['tasks']['total'], 'tasks.total'],
118+
[self::$stats, self::$stats['calls'], 'calls'],
119+
[self::$stats, self::$stats['calls']['ack'], 'calls.ack'],
120+
[self::$stats, self::$stats['calls']['delete'], 'calls.delete'],
121+
[self::$stats, self::$stats['calls']['take'], 'calls.take'],
122+
[self::$stats, self::$stats['calls']['kick'], 'calls.kick'],
123+
[self::$stats, self::$stats['calls']['release'], 'calls.release'],
124+
[self::$stats, self::$stats['calls']['put'], 'calls.put'],
125+
[self::$stats, self::$stats['calls']['bury'], 'calls.bury'],
126+
];
127+
}
81128

129+
/**
130+
* @dataProvider provideStatisticsInvalidPath
131+
* @expectedException \InvalidArgumentException
132+
* @expectedExceptionMessageRegExp /^Invalid path ".*?"\.$/
133+
*/
134+
public function testStatisticsInvalidPath($path)
135+
{
136+
$this->client->expects($this->once())->method('call')
137+
->with('queue.statistics')
138+
->willReturn([[self::$stats]]);
139+
140+
$this->queue->statistics($path);
141+
}
142+
143+
public function provideStatisticsInvalidPath()
144+
{
82145
return [
83-
[[], [$stats], $stats],
84-
[['tasks'], [$stats], $stats['tasks']],
85-
[['tasks', 'ready'], [$stats], $stats['tasks']['ready']],
86-
[['tasks', 'done'], [$stats], $stats['tasks']['done']],
87-
[['calls'], [$stats], $stats['calls']],
88-
[['calls', 'put'], [$stats], $stats['calls']['put']],
89-
[[], [], null],
90-
[[null], [$stats], null],
91-
[[null, null], [$stats], null],
92-
[[''], [$stats], null],
93-
[['foo'], [$stats], null],
94-
[['tasks', 'foo'], [$stats], null],
95-
[[null, 'tasks'], [$stats], null],
96-
[['tasks', ''], [$stats], null],
146+
[''],
147+
['.'],
148+
['foo'],
149+
['tasks.foo'],
150+
['.tasks'],
151+
['tasks.'],
152+
['calls.foo'],
153+
['.calls'],
154+
['calls.'],
97155
];
98156
}
99157
}

0 commit comments

Comments
 (0)