Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
5 | class MockCacheCest |
||
6 | { |
||
7 | /** |
||
8 | * @var int |
||
9 | */ |
||
10 | const DEFAULT_EXPIRATION = 86400; |
||
11 | |||
12 | /** |
||
13 | * @var MockCache |
||
14 | */ |
||
15 | protected $cache; |
||
16 | |||
17 | public function _before() |
||
18 | { |
||
19 | $this->cache = new MockCache(self::DEFAULT_EXPIRATION); |
||
20 | } |
||
21 | |||
22 | public function setGetAndDelete(UnitTester $I) |
||
23 | { |
||
24 | $I->assertTrue($this->cache->set("key1", "value1")); |
||
25 | $I->assertTrue($this->cache->set("key2", "value2")); |
||
26 | |||
27 | $I->assertSame("value1", $this->cache->get("key1")); |
||
28 | $I->assertSame("value2", $this->cache->get("key2")); |
||
29 | |||
30 | $this->cache->delete("key1"); |
||
31 | |||
32 | $I->assertSame(null, $this->cache->get("key1")); |
||
33 | $I->assertSame("value2", $this->cache->get("key2")); |
||
34 | } |
||
35 | |||
36 | public function getNonExisting(UnitTester $I) |
||
37 | { |
||
38 | $I->assertSame(null, $this->cache->get("key")); |
||
39 | $I->assertSame("default", $this->cache->get("key", "default")); |
||
40 | } |
||
41 | |||
42 | public function expirationInSeconds(UnitTester $I) |
||
43 | { |
||
44 | $this->cache->set("key", "value", 10); |
||
45 | |||
46 | $this->cache->skipTime(5); |
||
47 | |||
48 | $I->assertSame("value", $this->cache->get("key")); |
||
49 | |||
50 | $this->cache->skipTime(5); |
||
51 | |||
52 | $I->assertSame(null, $this->cache->get("key")); |
||
53 | $I->assertSame("default", $this->cache->get("key", "default")); |
||
54 | } |
||
55 | |||
56 | public function expirationByInterval(UnitTester $I) |
||
57 | { |
||
58 | $interval = new DateInterval("PT10S"); |
||
59 | |||
60 | $this->cache->set("key", "value", $interval); |
||
61 | |||
62 | $this->cache->skipTime(5); |
||
63 | |||
64 | $I->assertSame("value", $this->cache->get("key")); |
||
65 | |||
66 | $this->cache->skipTime(5); |
||
67 | |||
68 | $I->assertSame(null, $this->cache->get("key")); |
||
69 | $I->assertSame("default", $this->cache->get("key", "default")); |
||
70 | } |
||
71 | |||
72 | public function expirationByDefault(UnitTester $I) |
||
73 | { |
||
74 | $this->cache->set("key", "value"); |
||
75 | |||
76 | $this->cache->skipTime(self::DEFAULT_EXPIRATION - 5); |
||
77 | |||
78 | $I->assertSame("value", $this->cache->get("key")); |
||
79 | |||
80 | $this->cache->skipTime(10); |
||
81 | |||
82 | $I->assertSame(null, $this->cache->get("key")); |
||
83 | $I->assertSame("default", $this->cache->get("key", "default")); |
||
84 | } |
||
85 | |||
86 | public function expirationInThePast(UnitTester $I) |
||
87 | { |
||
88 | $this->cache->set("key1", "value1", 0); |
||
89 | $this->cache->set("key2", "value2", -10); |
||
90 | |||
91 | $I->assertSame("default", $this->cache->get("key1", "default")); |
||
92 | $I->assertSame("default", $this->cache->get("key2", "default")); |
||
93 | } |
||
94 | |||
95 | public function clear(UnitTester $I) |
||
96 | { |
||
97 | // add some values that should be gone when we clear cache: |
||
98 | |||
99 | $this->cache->set("key1", "value1"); |
||
100 | $this->cache->set("key2", "value2"); |
||
101 | |||
102 | $this->cache->clear(); |
||
103 | |||
104 | // check to confirm everything"s been wiped out: |
||
105 | |||
106 | $I->assertSame(null, $this->cache->get("key1")); |
||
107 | $I->assertSame("default", $this->cache->get("key1", "default")); |
||
108 | |||
109 | $I->assertSame(null, $this->cache->get("key2")); |
||
110 | $I->assertSame("default", $this->cache->get("key2", "default")); |
||
111 | } |
||
112 | |||
113 | public function testGetAndSetMultiple(UnitTester $I) |
||
114 | { |
||
115 | $this->cache->setMultiple(["key1" => "value1", "key2" => "value2"]); |
||
116 | |||
117 | $results = $this->cache->getMultiple(["key1", "key2", "key3"]); |
||
118 | |||
119 | $I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => null], $results); |
||
120 | } |
||
121 | |||
122 | public function testDeleteMultiple(UnitTester $I) |
||
123 | { |
||
124 | $this->cache->setMultiple(["key1" => "value1", "key2" => "value2", "key3" => "value3"]); |
||
125 | |||
126 | $this->cache->deleteMultiple(["key1", "key2"]); |
||
127 | |||
128 | $I->assertSame(["key1" => null, "key2" => null], $this->cache->getMultiple(["key1", "key2"])); |
||
129 | |||
130 | $I->assertSame("value3", $this->cache->get("key3")); |
||
131 | } |
||
132 | |||
133 | public function testHas(UnitTester $I) |
||
134 | { |
||
135 | $this->cache->set("key", "value"); |
||
136 | |||
137 | $I->assertSame(true, $this->cache->has("key")); |
||
138 | $I->assertSame(false, $this->cache->has("fudge")); |
||
139 | } |
||
140 | } |
||
141 |