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 |
||
34 | class CacheTest extends \PHPUnit_Framework_TestCase |
||
35 | { |
||
36 | use CacheProvider; |
||
37 | |||
38 | /** |
||
39 | * 正常系 |
||
40 | * キャッシュを新規追加できること |
||
41 | * @test |
||
42 | * @dataProvider cacheProvider |
||
43 | */ |
||
44 | public function okAddCache($cache) |
||
45 | { |
||
46 | $ttt = $cache->add("key", "value1", 0, false); |
||
47 | $this->assertEquals($cache->get("key"), "value1"); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * 正常系 |
||
52 | * キャッシュを上書きできること |
||
53 | * @test |
||
54 | * @dataProvider cacheProvider |
||
55 | */ |
||
56 | public function okAddOverwriteCache($cache) |
||
57 | { |
||
58 | $cache->add("key", "value1", 0, true); |
||
59 | $this->assertEquals($cache->get("key"), "value1"); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * 正常系 |
||
64 | * キャッシュを削除できること |
||
65 | * @test |
||
66 | * @dataProvider cacheProvider |
||
67 | */ |
||
68 | public function okDeleteCache($cache) |
||
69 | { |
||
70 | $cache->add("key", "value1", 0, true); |
||
71 | $cache->delete("key"); |
||
72 | $this->assertNull($cache->get("key")); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * 正常系 |
||
77 | * キャッシュを削除できること |
||
78 | * @test |
||
79 | * @dataProvider cacheProvider |
||
80 | */ |
||
81 | public function okClearCache($cache) |
||
82 | { |
||
83 | $cache->add("key1", "value1", 0, true); |
||
84 | $cache->add("key2", "value2", 0, true); |
||
85 | $cache->clear(); |
||
86 | $this->assertNull($cache->get("key1")); |
||
87 | $this->assertNull($cache->get("key2")); |
||
88 | } |
||
89 | } |
||
90 |