Skip to content

Commit a9a1bb9

Browse files
author
Jakub Chabek
committed
Added dataProvider for some tests
1 parent 05a322e commit a9a1bb9

File tree

3 files changed

+100
-143
lines changed

3 files changed

+100
-143
lines changed

tests/PoolTest.php

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,55 +70,56 @@ public function testAddItem()
7070
$this->assertKeys(['test']);
7171
}
7272

73-
public function testAddItemEmpty()
73+
/**
74+
* @dataProvider providerAddItemsEmpty
75+
*/
76+
public function testAddItemEmpty($emptyItem = null)
7477
{
7578
$pool = new Pool($this->redis, 'test');
79+
$pool->addItem(1);
7680

7781
try {
78-
$pool->addItem('');
79-
$this->fail('Expected InvalidArgumentException to be thrown');
80-
} catch (Exception\InvalidArgument $e) {}
81-
82-
try {
83-
$pool->addItem('');
84-
$this->fail('Expected InvalidArgumentException to be thrown');
85-
} catch (Exception\InvalidArgument $e) {}
86-
87-
try {
88-
$pool->addItem(null);
89-
$this->fail('Expected InvalidArgumentException to be thrown');
90-
} catch (Exception\InvalidArgument $e) {}
91-
92-
try {
93-
$pool->addItem(0);
94-
$this->fail('Expected InvalidArgumentException to be thrown');
82+
$pool->addItem($emptyItem);
83+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
9584
} catch (Exception\InvalidArgument $e) {}
9685

97-
$this->assertKeys([]);
86+
$this->assertSame(['1'], $this->redis->zrange('test', 0, 5));
87+
$this->assertKeys(['test']);
9888
}
9989

100-
public function testAddItemsEmpty()
90+
public function testAddItems()
10191
{
92+
$time = time();
10293
$pool = new Pool($this->redis, 'test');
94+
$pool->addItems([1, 3, 5, 3]);
95+
$pool->addItems([3, 6]);
10396

104-
try {
105-
$pool->addItems([1, '', 5]);
106-
$this->fail('Expected InvalidArgumentException to be thrown');
107-
} catch (Exception\InvalidArgument $e) {}
108-
109-
try {
110-
$pool->addItems([1, null, 5]);
111-
$this->fail('Expected InvalidArgumentException to be thrown');
112-
} catch (Exception\InvalidArgument $e) {}
97+
$this->assertSame(
98+
array_fill_keys(['1', '3', '5', '6'], (string)$time),
99+
$this->redis->zrange('test', 0, 5, 'WITHSCORES')
100+
);
101+
$this->assertKeys(['test']);
102+
}
113103

104+
/**
105+
* @dataProvider providerAddItemsEmpty
106+
*/
107+
public function testAddItemsEmpty($emptyItem = null)
108+
{
109+
$pool = new Pool($this->redis, 'test');
114110
try {
115-
$pool->addItems([1, 0, 5]);
116-
$this->fail('Expected InvalidArgumentException to be thrown');
111+
$pool->addItems([1, $emptyItem, 5]);
112+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
117113
} catch (Exception\InvalidArgument $e) {}
118114

119115
$this->assertKeys([]);
120116
}
121117

118+
public function providerAddItemsEmpty()
119+
{
120+
return ['', null, 0, false];
121+
}
122+
122123
public function testGetItems()
123124
{
124125
$time = time();
@@ -143,29 +144,21 @@ public function testGetItems()
143144
$this->assertKeys(['test']);
144145
}
145146

146-
public function testGetItemsInvalid()
147+
/**
148+
* @dataProvider providerGetItemsInvalid
149+
*/
150+
public function testGetItemsInvalid($count = null)
147151
{
148152
$pool = new Pool($this->redis, 'test');
149-
150153
try {
151-
$pool->getItems(0);
152-
$this->fail('Expected InvalidArgumentException to be thrown');
153-
} catch (Exception\InvalidArgument $e) {}
154-
155-
try {
156-
$pool->getItems(-5);
157-
$this->fail('Expected InvalidArgumentException to be thrown');
158-
} catch (Exception\InvalidArgument $e) {}
159-
160-
try {
161-
$pool->getItems('');
162-
$this->fail('Expected InvalidArgumentException to be thrown');
154+
$pool->getItems($count);
155+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
163156
} catch (Exception\InvalidArgument $e) {}
157+
}
164158

165-
try {
166-
$pool->getItems(null);
167-
$this->fail('Expected InvalidArgumentException to be thrown');
168-
} catch (Exception\InvalidArgument $e) {}
159+
public function providerGetItemsInvalid()
160+
{
161+
return [0, -5, '', null, false];
169162
}
170163

171164
public function testGetAllItems()

tests/QueueTest.php

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,17 @@ public function testAddItem()
3636
$this->assertKeys(['test']);
3737
}
3838

39-
public function testAddItemEmpty()
39+
/**
40+
* @dataProvider providerAddItemsEmpty
41+
*/
42+
public function testAddItemEmpty($invalidItem = null)
4043
{
4144
$queue = new Queue($this->redis, 'test');
4245
$queue->addItem(1);
4346

4447
try {
45-
$queue->addItem('');
46-
$this->fail('Expected InvalidArgumentException to be thrown');
47-
} catch (Exception\InvalidArgument $e) {}
48-
49-
try {
50-
$queue->addItem('');
51-
$this->fail('Expected InvalidArgumentException to be thrown');
52-
} catch (Exception\InvalidArgument $e) {}
53-
54-
try {
55-
$queue->addItem(null);
56-
$this->fail('Expected InvalidArgumentException to be thrown');
57-
} catch (Exception\InvalidArgument $e) {}
58-
59-
try {
60-
$queue->addItem(0);
61-
$this->fail('Expected InvalidArgumentException to be thrown');
48+
$queue->addItem($invalidItem);
49+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
6250
} catch (Exception\InvalidArgument $e) {}
6351

6452
$this->assertSame(['1'], $this->redis->lrange('test', 0, 5));
@@ -75,28 +63,26 @@ public function testAddItems()
7563
$this->assertKeys(['test']);
7664
}
7765

78-
public function testAddItemsEmpty()
66+
/**
67+
* @dataProvider providerAddItemsEmpty
68+
*/
69+
public function testAddItemsEmpty($emptyItem = null)
7970
{
8071
$queue = new Queue($this->redis, 'test');
8172

8273
try {
83-
$queue->addItems([1, '', 5]);
84-
$this->fail('Expected InvalidArgumentException to be thrown');
85-
} catch (Exception\InvalidArgument $e) {}
86-
87-
try {
88-
$queue->addItems([1, null, 5]);
89-
$this->fail('Expected InvalidArgumentException to be thrown');
90-
} catch (Exception\InvalidArgument $e) {}
91-
92-
try {
93-
$queue->addItems([1, 0, 5]);
94-
$this->fail('Expected InvalidArgumentException to be thrown');
74+
$queue->addItems([1, $emptyItem, 5]);
75+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
9576
} catch (Exception\InvalidArgument $e) {}
9677

9778
$this->assertKeys([]);
9879
}
9980

81+
public function providerAddItemsEmpty()
82+
{
83+
return ['', null, 0, false];
84+
}
85+
10086
public function testGetItems()
10187
{
10288
$this->redis->lpush('test', [1, 5, 5, 3]);
@@ -118,29 +104,21 @@ public function testGetItems()
118104
]);
119105
}
120106

121-
public function testGetItemsInvalid()
107+
/**
108+
* @dataProvider providerGetItemsInvalid
109+
*/
110+
public function testGetItemsInvalid($count = null)
122111
{
123112
$queue = new Queue($this->redis, 'test');
124-
125-
try {
126-
$queue->getItems(0);
127-
$this->fail('Expected InvalidArgumentException to be thrown');
128-
} catch (Exception\InvalidArgument $e) {}
129-
130113
try {
131-
$queue->getItems(-5);
132-
$this->fail('Expected InvalidArgumentException to be thrown');
133-
} catch (Exception\InvalidArgument $e) {}
134-
135-
try {
136-
$queue->getItems('');
137-
$this->fail('Expected InvalidArgumentException to be thrown');
114+
$queue->getItems($count);
115+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
138116
} catch (Exception\InvalidArgument $e) {}
117+
}
139118

140-
try {
141-
$queue->getItems(null);
142-
$this->fail('Expected InvalidArgumentException to be thrown');
143-
} catch (Exception\InvalidArgument $e) {}
119+
public function providerGetItemsInvalid()
120+
{
121+
return [0, -5, '', null, false];
144122
}
145123

146124
public function testGetAllItems()

tests/UniqueQueueTest.php

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,21 @@ public function testAddItem()
4242
$this->assertKeys(['test', 'test-unique']);
4343
}
4444

45-
public function testAddItemEmpty()
45+
/**
46+
* @dataProvider providerAddItemsEmpty
47+
*/
48+
public function testAddItemEmpty($invalidItem = null)
4649
{
47-
$queue = new UniqueQueue($this->redis, 'test');
48-
49-
try {
50-
$queue->addItem('');
51-
$this->fail('Expected InvalidArgumentException to be thrown');
52-
} catch (Exception\InvalidArgument $e) {}
53-
54-
try {
55-
$queue->addItem(null);
56-
$this->fail('Expected InvalidArgumentException to be thrown');
57-
} catch (Exception\InvalidArgument $e) {}
50+
$queue = new Queue($this->redis, 'test');
51+
$queue->addItem(1);
5852

5953
try {
60-
$queue->addItem(0);
61-
$this->fail('Expected InvalidArgumentException to be thrown');
54+
$queue->addItem($invalidItem);
55+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
6256
} catch (Exception\InvalidArgument $e) {}
6357

64-
$this->assertKeys([]);
58+
$this->assertSame(['1'], $this->redis->lrange('test', 0, 5));
59+
$this->assertKeys(['test']);
6560
}
6661

6762
public function testAddItems()
@@ -81,28 +76,26 @@ public function testAddItems()
8176
$this->assertKeys(['test', 'test-unique']);
8277
}
8378

84-
public function testAddItemsEmpty()
79+
/**
80+
* @dataProvider providerAddItemsEmpty
81+
*/
82+
public function testAddItemsEmpty($emptyItem = null)
8583
{
8684
$queue = new UniqueQueue($this->redis, 'test');
8785

8886
try {
89-
$queue->addItems([1, '', 5]);
90-
$this->fail('Expected InvalidArgumentException to be thrown');
91-
} catch (Exception\InvalidArgument $e) {}
92-
93-
try {
94-
$queue->addItems([1, null, 5]);
95-
$this->fail('Expected InvalidArgumentException to be thrown');
96-
} catch (Exception\InvalidArgument $e) {}
97-
98-
try {
99-
$queue->addItems([1, 0, 5]);
100-
$this->fail('Expected InvalidArgumentException to be thrown');
87+
$queue->addItems([1, $emptyItem, 5]);
88+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
10189
} catch (Exception\InvalidArgument $e) {}
10290

10391
$this->assertKeys([]);
10492
}
10593

94+
public function providerAddItemsEmpty()
95+
{
96+
return ['', null, 0, false];
97+
}
98+
10699
public function testGetItems()
107100
{
108101
$this->redis->lpush('test', [1, 5, 3]);
@@ -125,29 +118,22 @@ public function testGetItems()
125118
]);
126119
}
127120

128-
public function testGetItemsInvalid()
121+
/**
122+
* @dataProvider providerGetItemsInvalid
123+
*/
124+
public function testGetItemsInvalid($count = null)
129125
{
130126
$queue = new UniqueQueue($this->redis, 'test');
131127

132128
try {
133-
$queue->getItems(0);
134-
$this->fail('Expected InvalidArgumentException to be thrown');
135-
} catch (Exception\InvalidArgument $e) {}
136-
137-
try {
138-
$queue->getItems(-5);
139-
$this->fail('Expected InvalidArgumentException to be thrown');
140-
} catch (Exception\InvalidArgument $e) {}
141-
142-
try {
143-
$queue->getItems('');
144-
$this->fail('Expected InvalidArgumentException to be thrown');
129+
$queue->getItems($count);
130+
$this->fail('Expected \PhpRQ\Exception\InvalidArgument to be thrown');
145131
} catch (Exception\InvalidArgument $e) {}
132+
}
146133

147-
try {
148-
$queue->getItems(null);
149-
$this->fail('Expected InvalidArgumentException to be thrown');
150-
} catch (Exception\InvalidArgument $e) {}
134+
public function providerGetItemsInvalid()
135+
{
136+
return [0, -5, '', null, false];
151137
}
152138

153139
public function testGetAllItems()

0 commit comments

Comments
 (0)