Skip to content

Commit 2087479

Browse files
nicolas-grekasNyholm
authored andcommitted
Test generators returning several times the same key (#72)
1 parent ebbf705 commit 2087479

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/SimpleCacheTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,27 @@ public function testGetMultiple()
272272
}
273273

274274
$result = $this->cache->getMultiple(['key0', 'key1']);
275-
foreach ($result as $r) {
275+
$keys = [];
276+
foreach ($result as $i => $r) {
277+
$keys[] = $i;
276278
$this->assertNull($r);
277279
}
280+
sort($keys);
281+
$this->assertSame(['key0', 'key1'], $keys);
278282

279283
$this->cache->set('key3', 'value');
280284
$result = $this->cache->getMultiple(['key2', 'key3', 'key4'], 'foo');
285+
$keys = [];
281286
foreach ($result as $key => $r) {
287+
$keys[] = $key;
282288
if ($key === 'key3') {
283289
$this->assertEquals('value', $r);
284290
} else {
285291
$this->assertEquals('foo', $r);
286292
}
287293
}
294+
sort($keys);
295+
$this->assertSame(['key2', 'key3', 'key4'], $keys);
288296
}
289297

290298
public function testGetMultipleWithGenerator()
@@ -294,13 +302,15 @@ public function testGetMultipleWithGenerator()
294302
}
295303

296304
$gen = function () {
297-
yield 'key0';
298-
yield 'key1';
305+
yield 1 => 'key0';
306+
yield 1 => 'key1';
299307
};
300308

301309
$this->cache->set('key0', 'value0');
302310
$result = $this->cache->getMultiple($gen());
311+
$keys = [];
303312
foreach ($result as $key => $r) {
313+
$keys[] = $key;
304314
if ($key === 'key0') {
305315
$this->assertEquals('value0', $r);
306316
} elseif ($key === 'key1') {
@@ -309,6 +319,8 @@ public function testGetMultipleWithGenerator()
309319
$this->assertFalse(true, 'This should not happend');
310320
}
311321
}
322+
sort($keys);
323+
$this->assertSame(['key0', 'key1'], $keys);
312324
$this->assertEquals('value0', $this->cache->get('key0'));
313325
$this->assertNull($this->cache->get('key1'));
314326
}
@@ -336,8 +348,8 @@ public function testDeleteMultipleGenerator()
336348
}
337349

338350
$gen = function () {
339-
yield 'key0';
340-
yield 'key1';
351+
yield 1 => 'key0';
352+
yield 1 => 'key1';
341353
};
342354
$this->cache->set('key0', 'value0');
343355
$this->assertTrue($this->cache->deleteMultiple($gen()), 'Deleting a generator should return true');
@@ -632,10 +644,13 @@ public function testSetMultipleValidKeys($key)
632644

633645
$this->cache->setMultiple([$key => 'foobar']);
634646
$result = $this->cache->getMultiple([$key]);
647+
$keys = [];
635648
foreach ($result as $i => $r) {
649+
$keys[] = $i;
636650
$this->assertEquals($key, $i);
637651
$this->assertEquals('foobar', $r);
638652
}
653+
$this->assertSame([$key], $keys);
639654
}
640655

641656
/**
@@ -662,9 +677,12 @@ public function testSetMultipleValidData($data)
662677

663678
$this->cache->setMultiple(['key' => $data]);
664679
$result = $this->cache->getMultiple(['key']);
665-
foreach ($result as $r) {
680+
$keys = [];
681+
foreach ($result as $i => $r) {
682+
$keys[] = $i;
666683
$this->assertEquals($data, $r);
667684
}
685+
$this->assertSame(['key'], $keys);
668686
}
669687

670688
public function testObjectAsDefaultValue()

0 commit comments

Comments
 (0)