Skip to content

Commit 571d70c

Browse files
committed
advanced unit test
1 parent b59ab3c commit 571d70c

1 file changed

Lines changed: 39 additions & 83 deletions

File tree

tests/SimpleCacheNullCacheTest.php

Lines changed: 39 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ public function __construct(?string $name = null, array $data = [], string $data
1515
{
1616
parent::__construct($name, $data, $dataName);
1717
$this->nullCache = new \ihipop\PsrNullCache\SimpleCache\NullCache(false);
18-
$this->invalidKey = str_split('{}()/\@:');
18+
$this->invalidKey = array_merge([''], str_split('{}()/\@:'));
1919
}
2020

21-
public function testInvalidKeySet()
21+
protected function performKeyTest(callable $action, array $invalidKeyChr)
2222
{
23-
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
2423
$totalException = count($invalidKeyChr);
2524
$exceptionContainer = [];
2625
foreach ($invalidKeyChr as $key) {
2726
try {
28-
$this->nullCache->set($key, 'foobar');
27+
$ret = $action($key);
28+
if ($ret instanceof \Traversable) {
29+
foreach ($ret as $K => $__) {
30+
//Do nothing to trig the error
31+
}
32+
}
2933
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
3034
$exceptionContainer[] = $e;
3135
}
@@ -34,107 +38,59 @@ public function testInvalidKeySet()
3438
$this->assertEquals($totalException, count($exceptionContainer));
3539
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
3640
}
41+
public function testInvalidKeySet()
42+
{
43+
$invalidKeyChr = array_merge([new stdClass()], $this->invalidKey);
44+
$this->performKeyTest(function ($key) {
45+
$this->nullCache->set($key, 'Foobar');
46+
}, $invalidKeyChr);
47+
}
3748

3849
public function testInvalidKeyGet()
3950
{
40-
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
41-
$totalException = count($invalidKeyChr);
42-
$exceptionContainer = [];
43-
foreach ($invalidKeyChr as $key) {
44-
try {
45-
$this->nullCache->get($key);
46-
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
47-
$exceptionContainer[] = $e;
48-
}
49-
}
50-
51-
$this->assertEquals($totalException, count($exceptionContainer));
52-
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
51+
$invalidKeyChr = array_merge([new stdClass()], $this->invalidKey);
52+
$this->performKeyTest(function ($key) {
53+
$this->nullCache->get($key);
54+
}, $invalidKeyChr);
5355
}
5456

5557
public function testInvalidKeyDelete()
5658
{
57-
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
58-
$totalException = count($invalidKeyChr);
59-
foreach ($invalidKeyChr as $key) {
60-
try {
61-
$this->nullCache->delete($key);
62-
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
63-
$exceptionContainer[] = $e;
64-
}
65-
}
66-
67-
$this->assertEquals($totalException, count($exceptionContainer));
68-
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
59+
$invalidKeyChr = array_merge([new stdClass()], $this->invalidKey);
60+
$this->performKeyTest(function ($key) {
61+
$this->nullCache->delete($key);
62+
}, $invalidKeyChr);
6963
}
7064

7165
public function testInvalidKeyHas()
7266
{
73-
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
74-
$totalException = count($invalidKeyChr);
75-
$exceptionContainer = [];
76-
foreach ($invalidKeyChr as $key) {
77-
try {
78-
$this->nullCache->has($key);
79-
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
80-
$exceptionContainer[] = $e;
81-
}
82-
}
83-
84-
$this->assertEquals($totalException, count($exceptionContainer));
85-
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
67+
$invalidKeyChr = array_merge([new stdClass()], $this->invalidKey);
68+
$this->performKeyTest(function ($key) {
69+
$this->nullCache->has($key);
70+
}, $invalidKeyChr);
8671
}
8772

8873
public function testInvalidKeySetMultiple()
8974
{
90-
$invalidKeyChr = array_merge([''], $this->invalidKey);
91-
$totalException = count($invalidKeyChr);
92-
$exceptionContainer = [];
93-
foreach ($invalidKeyChr as $key) {
94-
try {
95-
$this->nullCache->setMultiple(['foo' => 'bar', $key => 'bar']);
96-
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
97-
$exceptionContainer[] = $e;
98-
}
99-
}
100-
101-
$this->assertEquals($totalException, count($exceptionContainer));
102-
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
75+
$invalidKeyChr = array_merge([], $this->invalidKey);
76+
$this->performKeyTest(function ($key) {
77+
$this->nullCache->setMultiple(['foo' => 'bar', $key => 'bar']);
78+
}, $invalidKeyChr);
10379
}
10480

10581
public function testInvalidKeyGetMultiple()
10682
{
107-
$invalidKeyChr = array_merge([''], $this->invalidKey);
108-
$totalException = count($invalidKeyChr);
109-
$exceptionContainer = [];
110-
foreach ($invalidKeyChr as $key) {
111-
try {
112-
foreach ($this->nullCache->getMultiple(['foo', $key]) as $iter) {
113-
// fo nothing
114-
}
115-
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
116-
$exceptionContainer[] = $e;
117-
}
118-
}
119-
120-
$this->assertEquals($totalException, count($exceptionContainer));
121-
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
83+
$invalidKeyChr = array_merge([], $this->invalidKey);
84+
$this->performKeyTest(function ($key) {
85+
return $this->nullCache->getMultiple(['foo', $key]);
86+
}, $invalidKeyChr);
12287
}
12388

12489
public function testInvalidKeyDeleteMultiple()
12590
{
126-
$invalidKeyChr = array_merge([''], $this->invalidKey);
127-
$totalException = count($invalidKeyChr);
128-
$exceptionContainer = [];
129-
foreach ($invalidKeyChr as $key) {
130-
try {
131-
$this->nullCache->deleteMultiple(['foo', $key]);
132-
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
133-
$exceptionContainer[] = $e;
134-
}
135-
}
136-
137-
$this->assertEquals($totalException, count($exceptionContainer));
138-
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
91+
$invalidKeyChr = array_merge([], $this->invalidKey);
92+
$this->performKeyTest(function ($key) {
93+
return $this->nullCache->deleteMultiple(['foo', $key]);
94+
}, $invalidKeyChr);
13995
}
14096
}

0 commit comments

Comments
 (0)