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 CacheTest extends TestCase |
||
6 | { |
||
7 | public function tearDown() |
||
8 | { |
||
9 | Mockery::close(); |
||
10 | } |
||
11 | |||
12 | /** |
||
13 | * @todo: test each scenario individually |
||
14 | * @test |
||
15 | */ |
||
16 | public function canGetSetHasDelete() |
||
17 | { |
||
18 | $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class); |
||
19 | $cache = new \Venta\Cache\Cache($pool); |
||
20 | $pool->shouldReceive('hasItem')->with('key')->andReturn(false, true, false)->times(3); |
||
21 | |||
22 | $this->assertFalse($cache->has('key')); |
||
23 | |||
24 | $pool->shouldReceive('save')->with(Mockery::type(\Psr\Cache\CacheItemInterface::class))->andReturn(true); |
||
25 | $this->assertTrue($cache->set('key', 'value')); |
||
26 | |||
27 | $this->assertTrue($cache->has('key')); |
||
28 | |||
29 | $pool->shouldReceive('getItem')->with('key')->andReturn( |
||
30 | Mockery::mock(\Psr\Cache\CacheItemInterface::class) |
||
31 | ->shouldReceive('get') |
||
32 | ->withNoArgs() |
||
33 | ->andReturn('value') |
||
34 | ->getMock() |
||
35 | ); |
||
36 | $this->assertSame('value', $cache->get('key')); |
||
37 | |||
38 | $pool->shouldReceive('deleteItem')->with('key')->andReturn(true); |
||
39 | $this->assertTrue($cache->delete('key')); |
||
40 | |||
41 | $this->assertFalse($cache->has('key')); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @test |
||
46 | */ |
||
47 | public function canPutWithDateTimeExpire() |
||
48 | { |
||
49 | $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class); |
||
50 | $cache = new \Venta\Cache\Cache($pool); |
||
51 | |||
52 | $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) { |
||
53 | $this->assertSame('key', $cacheItem->getKey()); |
||
54 | $this->assertEquals('2030-01-01 00:00:00', $cacheItem->getExpirationDate()->format('Y-m-d H:i:s')); |
||
55 | |||
56 | return true; |
||
57 | }))->andReturn(true); |
||
58 | |||
59 | $this->assertTrue($cache->put('key', 'value', new DateTime('2030-01-01 00:00:00'))); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @test |
||
64 | */ |
||
65 | public function canPutWithIntExpire() |
||
66 | { |
||
67 | $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class); |
||
68 | $cache = new \Venta\Cache\Cache($pool); |
||
69 | |||
70 | $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) { |
||
71 | $this->assertSame('key', $cacheItem->getKey()); |
||
72 | $this->assertEquals(time() + 10, $cacheItem->getExpirationDate()->getTimestamp()); |
||
73 | |||
74 | return true; |
||
75 | }))->andReturn(true); |
||
76 | |||
77 | $this->assertTrue($cache->put('key', 'value', 10)); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @test |
||
82 | */ |
||
83 | public function canPutWithIntervalExpire() |
||
84 | { |
||
85 | $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class); |
||
86 | $cache = new \Venta\Cache\Cache($pool); |
||
87 | |||
88 | $interval = new DateInterval('P1M'); |
||
89 | |||
90 | $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) use ( |
||
91 | $interval |
||
92 | ) { |
||
93 | $this->assertSame('key', $cacheItem->getKey()); |
||
94 | $this->assertEquals( |
||
95 | (new \DateTime())->add($interval)->getTimestamp(), |
||
96 | $cacheItem->getExpirationDate()->getTimestamp() |
||
97 | ); |
||
98 | |||
99 | return true; |
||
100 | }))->andReturn(true); |
||
101 | |||
102 | $this->assertTrue($cache->put('key', 'value', $interval)); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @test |
||
107 | */ |
||
108 | public function canPutWithoutExpire() |
||
109 | { |
||
110 | $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class); |
||
111 | $cache = new \Venta\Cache\Cache($pool); |
||
112 | |||
113 | $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) { |
||
114 | $this->assertSame('key', $cacheItem->getKey()); |
||
115 | $this->assertNull($cacheItem->getExpirationDate()); |
||
116 | |||
117 | return true; |
||
118 | }))->andReturn(true); |
||
119 | |||
120 | $this->assertTrue($cache->put('key', 'value', 'invalid time')); |
||
121 | } |
||
122 | |||
123 | } |
||
124 |