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 |
||
29 | class RouterPathTransformerSpec extends ObjectBehavior |
||
30 | { |
||
31 | function let(RouterContainer $router) |
||
32 | { |
||
33 | $this->beConstructedWith($router); |
||
34 | } |
||
35 | |||
36 | function it_is_initializable() |
||
37 | { |
||
38 | $this->shouldHaveType(RouterPathTransformer::class); |
||
39 | } |
||
40 | |||
41 | function it_implements_location_transformer_interface() |
||
42 | { |
||
43 | $this->shouldImplement(LocationTransformerInterface::class); |
||
44 | } |
||
45 | |||
46 | function it_will_return_null_for_unknown_routes( |
||
47 | RouterContainer $router, |
||
48 | Generator $generator |
||
49 | ) |
||
50 | { |
||
51 | $router->getGenerator()->willReturn($generator); |
||
52 | $this->beConstructedWith($router); |
||
53 | |||
54 | $generator->generate('test', []) |
||
55 | ->shouldBeCalled() |
||
56 | ->willThrow(new RouteNotFound()); |
||
57 | |||
58 | $this->transform('test')->shouldBeNull(); |
||
59 | } |
||
60 | |||
61 | function it_sets_the_uri_path_form_router_container( |
||
62 | RouterContainer $router, |
||
63 | Generator $generator |
||
64 | ) |
||
65 | { |
||
66 | $router->getGenerator()->willReturn($generator); |
||
67 | $this->beConstructedWith($router); |
||
68 | |||
69 | $generator->generate('about', []) |
||
70 | ->shouldBeCalled() |
||
71 | ->willReturn('/pages/about'); |
||
72 | |||
73 | $this->transform('about')->shouldBeAnUriWithPath('/pages/about'); |
||
74 | } |
||
75 | |||
76 | function it_accepts_query_params_in_options( |
||
77 | ServerRequestInterface $request, |
||
78 | RouterContainer $router, |
||
79 | Generator $generator |
||
80 | ) |
||
81 | { |
||
82 | $this->prepareTest($request, $router, $generator); |
||
83 | |||
84 | $this->transform('about', ['query' => ['foo' => 'bar']]) |
||
85 | ->shouldBeAnUriWithPath('/pages/about?foo=bar'); |
||
86 | } |
||
87 | |||
88 | function it_can_reuse_host_name_from_context_request( |
||
89 | ServerRequestInterface $request, |
||
90 | RouterContainer $router, |
||
91 | Generator $generator |
||
92 | ) |
||
93 | { |
||
94 | $this->prepareTest($request, $router, $generator); |
||
95 | $this->transform('about', ['reuseHostName' => 1]) |
||
96 | ->shouldBeAnUriWithPath('https://localhost:12541/pages/about'); |
||
97 | } |
||
98 | |||
99 | function it_can_reuse_the_request_query_params( |
||
100 | ServerRequestInterface $request, |
||
101 | RouterContainer $router, |
||
102 | Generator $generator |
||
103 | ) |
||
104 | { |
||
105 | $this->prepareTest($request, $router, $generator); |
||
106 | $this->transform('about', [ |
||
107 | 'reuseParams' => 1, |
||
108 | 'query' => ['foo' => 'bar'] |
||
109 | ]) |
||
110 | ->shouldBeAnUriWithPath( |
||
111 | '/pages/about?foo=bar&baz=bar' |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Prepares the test |
||
117 | * |
||
118 | * @param ServerRequestInterface|Collaborator $request |
||
119 | * @param RouterContainer|Collaborator $router |
||
120 | * @param Generator|Collaborator $generator |
||
121 | */ |
||
122 | private function prepareTest( |
||
123 | ServerRequestInterface $request, |
||
124 | RouterContainer $router, |
||
125 | Generator $generator |
||
126 | ) |
||
127 | { |
||
128 | $this->beConstructedWith($router); |
||
129 | $router->getGenerator()->willReturn($generator); |
||
130 | $generator->generate('about', Argument::type('array')) |
||
131 | ->shouldBeCalled() |
||
132 | ->willReturn('/pages/about'); |
||
133 | $serverData = [ |
||
134 | 'SCRIPT_NAME' => '/base/test.php', |
||
135 | 'HTTPS' => 'not-empty', |
||
136 | 'SERVER_PORT' => '12541', |
||
137 | 'SERVER_NAME' => 'localhost', |
||
138 | ]; |
||
139 | $request->getServerParams() |
||
140 | ->willReturn($serverData); |
||
141 | $request->getQueryParams()->willReturn( |
||
142 | ['foo' => 'bar', 'baz' => 'bar'] |
||
143 | ); |
||
144 | $this->setRequest($request); |
||
145 | } |
||
146 | |||
147 | public function getMatchers() |
||
148 | { |
||
149 | return [ |
||
150 | 'beAnUriWithPath' => function ($uri, $path) |
||
151 | { |
||
152 | if (!$uri instanceof UriInterface) { |
||
153 | $class = UriInterface::class; |
||
154 | $type = gettype($uri); |
||
155 | throw new FailureException( |
||
156 | "Expected {$class} instance, but got '{$type}'" |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | if ($uri->__toString() !== $path) { |
||
161 | throw new FailureException( |
||
162 | "Expected URI with path '{$path}', but got '{$uri}'" |
||
163 | ); |
||
164 | } |
||
165 | return true; |
||
166 | } |
||
167 | ]; |
||
168 | } |
||
169 | } |