1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Storage\Tests\Units; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Storage\StorageInterface; |
15
|
|
|
use Cubiche\Core\Storage\Tests\Fixtures\User; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* StorageTestCase class. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class StorageTestCase extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @return StorageInterface |
26
|
|
|
*/ |
27
|
|
|
abstract protected function createStorage(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Test get method. |
31
|
|
|
*/ |
32
|
|
|
public function testGetSet() |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
|
|
->given($storage = $this->createStorage()) |
36
|
|
|
->and($user = new User('Ivan', 36, '[email protected]')) |
37
|
|
|
->when($storage->set('foo', $user)) |
38
|
|
|
->then() |
39
|
|
|
->object($storage->get('foo')) |
40
|
|
|
->isEqualTo($user) |
41
|
|
|
->integer($storage->get('baz', 15)) |
42
|
|
|
->isEqualTo(15) |
43
|
|
|
; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test has method. |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testHas() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this |
52
|
|
|
->given($storage = $this->createStorage()) |
53
|
|
|
->and($user = new User('Ivan', 36, '[email protected]')) |
54
|
|
|
->when($storage->set('foo', $user)) |
55
|
|
|
->then() |
56
|
|
|
->boolean($storage->has('foo')) |
57
|
|
|
->isTrue() |
58
|
|
|
->boolean($storage->has('baz')) |
59
|
|
|
->isFalse() |
60
|
|
|
; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test remove method. |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this |
69
|
|
|
->given($storage = $this->createStorage()) |
70
|
|
|
->when( |
71
|
|
|
$storage->set('a', 'red'), |
72
|
|
|
$storage->set('b', 'blue') |
73
|
|
|
) |
74
|
|
|
->then() |
75
|
|
|
->boolean($storage->remove('a')) |
76
|
|
|
->isTrue() |
77
|
|
|
->boolean($storage->remove('c')) |
78
|
|
|
->isFalse() |
79
|
|
|
; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test clear method. |
84
|
|
|
*/ |
85
|
|
|
public function testClear() |
86
|
|
|
{ |
87
|
|
|
$this |
88
|
|
|
->given($storage = $this->createStorage()) |
89
|
|
|
->when( |
90
|
|
|
$storage->set('a', 'red'), |
91
|
|
|
$storage->set('b', 'blue') |
92
|
|
|
) |
93
|
|
|
->then() |
94
|
|
|
->array($storage->keys()) |
95
|
|
|
->isNotEmpty() |
96
|
|
|
->when($storage->clear()) |
97
|
|
|
->then() |
98
|
|
|
->array($storage->keys()) |
99
|
|
|
->isEmpty() |
100
|
|
|
; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.