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 |
||
20 | class Parser extends atoum |
||
21 | { |
||
22 | public function testEmptyStringContent() |
||
23 | { |
||
24 | $this |
||
25 | ->given($rules = SUT::parse("")) |
||
26 | ->when($rule = $rules->get(LUT\Rules::DEFAULT_UA)) |
||
27 | ->then |
||
28 | ->object($rules) |
||
29 | ->isInstanceOf('Bee4\RobotsTxt\Rules') |
||
30 | ->object($rule) |
||
31 | ->isInstanceOf('Bee4\RobotsTxt\Rule'); |
||
32 | } |
||
33 | |||
34 | public function testEmptyContentInstance() |
||
35 | { |
||
36 | $this |
||
37 | ->given( |
||
38 | $content = new LUT\Content(""), |
||
39 | $rules = SUT::parse($content) |
||
40 | ) |
||
41 | ->when($rule = $rules->get(LUT\Rules::DEFAULT_UA)) |
||
42 | ->then |
||
43 | ->object($rules) |
||
44 | ->isInstanceOf('Bee4\RobotsTxt\Rules') |
||
45 | ->object($rule) |
||
46 | ->isInstanceOf('Bee4\RobotsTxt\Rule'); |
||
47 | } |
||
48 | |||
49 | public function testInvalidContentException() |
||
50 | { |
||
51 | $this->exception(function () { |
||
52 | SUT::parse(new \stdClass); |
||
53 | }) |
||
54 | ->isInstanceOf('Bee4\RobotsTxt\Exception\InvalidContentException'); |
||
55 | } |
||
56 | |||
57 | public function testDuplicateRuleException() |
||
58 | { |
||
59 | $robot = <<<ROBOTS |
||
60 | User-Agent: * |
||
61 | Allow: / |
||
62 | |||
63 | User-Agent: * |
||
64 | Disallow: /toto |
||
65 | ROBOTS; |
||
66 | |||
67 | $this |
||
68 | ->given($content = new LUT\Content($robot)) |
||
69 | ->exception(function () use ($content) { |
||
70 | SUT::parse($content); |
||
71 | }) |
||
72 | ->isInstanceOf('Bee4\RobotsTxt\Exception\DuplicateRuleException'); |
||
73 | } |
||
74 | |||
75 | public function testParse() |
||
76 | { |
||
77 | $robot = <<<ROBOTS |
||
78 | User-Agent: * |
||
79 | Allow: / |
||
80 | |||
81 | User-Agent: Google |
||
82 | Disallow: /toto |
||
83 | ROBOTS; |
||
84 | |||
85 | $this |
||
86 | ->given($rules = SUT::parse($robot)) |
||
87 | ->then |
||
88 | ->sizeOf($rules) |
||
89 | ->isEqualTo(2) |
||
90 | ->boolean($rules->match('*', '/page')) |
||
91 | ->isTrue() |
||
92 | ->boolean($rules->match('Google', '/page')) |
||
93 | ->isTrue() |
||
94 | ->boolean($rules->match('Google', '/toto/page')) |
||
95 | ->isFalse(); |
||
96 | } |
||
97 | } |
||
98 |