1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace RoaveTest\DoctrineSimpleCache\Exception; |
5
|
|
|
|
6
|
|
|
use Roave\DoctrineSimpleCache\Exception\InvalidArgumentException; |
7
|
|
|
use Psr\SimpleCache\InvalidArgumentException as PsrInvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException |
11
|
|
|
*/ |
12
|
|
|
final class InvalidArgumentExceptionTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function testFromKeyAndInvalidTTLObject() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$invalidTTL = new \stdClass(); |
17
|
|
|
$exception = InvalidArgumentException::fromKeyAndInvalidTTL('key', $invalidTTL); |
18
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
19
|
|
|
self::assertInstanceOf(PsrInvalidArgumentException::class, $exception); |
20
|
|
|
|
21
|
|
|
self::assertStringMatchesFormat( |
22
|
|
|
'TTL for "%s" should be defined by an integer or a DateInterval, but stdClass is given.', |
23
|
|
|
$exception->getMessage() |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
View Code Duplication |
public function testFromInvalidKeyCharacters() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$invalidKey = uniqid('invalidKey', true); |
29
|
|
|
$exception = InvalidArgumentException::fromInvalidKeyCharacters($invalidKey); |
30
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
31
|
|
|
self::assertInstanceOf(PsrInvalidArgumentException::class, $exception); |
32
|
|
|
self::assertSame( |
33
|
|
|
sprintf( |
34
|
|
|
'Key "%s" is in an invalid format - must not contain characters: {}()/\@:', |
35
|
|
|
$invalidKey |
36
|
|
|
), |
37
|
|
|
$exception->getMessage() |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testFromInvalidType() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$invalidKey = random_int(100, 200); |
44
|
|
|
$exception = InvalidArgumentException::fromInvalidType($invalidKey); |
45
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
46
|
|
|
self::assertInstanceOf(PsrInvalidArgumentException::class, $exception); |
47
|
|
|
self::assertSame( |
48
|
|
|
'Key was not a valid type. Expected string, received integer', |
49
|
|
|
$exception->getMessage() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testFromEmptyKey() |
54
|
|
|
{ |
55
|
|
|
$exception = InvalidArgumentException::fromEmptyKey(); |
56
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
57
|
|
|
self::assertInstanceOf(PsrInvalidArgumentException::class, $exception); |
58
|
|
|
self::assertSame( |
59
|
|
|
'Requested key was an empty string.', |
60
|
|
|
$exception->getMessage() |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testFromKeyAndInvalidTTLNonObject() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$invalidTTL = random_int(100, 200); |
67
|
|
|
$exception = InvalidArgumentException::fromKeyAndInvalidTTL('key', $invalidTTL); |
68
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
69
|
|
|
self::assertInstanceOf(\Psr\SimpleCache\InvalidArgumentException::class, $exception); |
70
|
|
|
|
71
|
|
|
self::assertStringMatchesFormat( |
72
|
|
|
'TTL for "%s" should be defined by an integer or a DateInterval, but integer is given.', |
73
|
|
|
$exception->getMessage() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testFromNonIterableKeys() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$invalidKey = random_int(100, 200); |
80
|
|
|
$exception = InvalidArgumentException::fromNonIterableKeys($invalidKey); |
81
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
82
|
|
|
self::assertInstanceOf(PsrInvalidArgumentException::class, $exception); |
83
|
|
|
self::assertSame( |
84
|
|
|
'Keys passed were not iterable (i.e. \Traversable or array), received: integer', |
85
|
|
|
$exception->getMessage() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testFromNonIterableValues() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$invalidValue = random_int(100, 200); |
92
|
|
|
$exception = InvalidArgumentException::fromNonIterableValues($invalidValue); |
93
|
|
|
self::assertInstanceOf(InvalidArgumentException::class, $exception); |
94
|
|
|
self::assertInstanceOf(PsrInvalidArgumentException::class, $exception); |
95
|
|
|
self::assertSame( |
96
|
|
|
'Values passed were not iterable (i.e. \Traversable or array), received: integer', |
97
|
|
|
$exception->getMessage() |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.