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 JsonFormExtensionTest extends WebTestCase |
||
6 | { |
||
7 | private $client; |
||
8 | |||
9 | public function setUp() |
||
10 | { |
||
11 | $this->client = $this->createClient(['test_case' => 'JsonFormExtension']); |
||
12 | } |
||
13 | |||
14 | public function testJsonRequest() |
||
15 | { |
||
16 | $json = '{ "name": "test1" }'; |
||
17 | $this->client->request( |
||
18 | 'POST', |
||
19 | '/json-form-extension/json', |
||
20 | [], |
||
21 | [], |
||
22 | ['CONTENT_TYPE' => 'application/json'], |
||
23 | $json |
||
24 | ); |
||
25 | |||
26 | $expectedJson = json_encode([ |
||
27 | 'Data' => ['name' => 'test1', 'lastname' => null], |
||
28 | 'NormData' => ['name' => 'test1', 'lastname' => null], |
||
29 | 'ViewData' => ['name' => 'test1', 'lastname' => null], |
||
30 | ], 15); |
||
31 | $this->assertEquals( |
||
32 | $expectedJson, |
||
33 | $this->client->getResponse()->getContent() |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | public function testJsonRequestInvalidJsonError() |
||
38 | { |
||
39 | $json = '{ "name" "test1" }'; |
||
40 | $this->client->request( |
||
41 | 'POST', |
||
42 | '/json-form-extension/json', |
||
43 | [], |
||
44 | [], |
||
45 | ['CONTENT_TYPE' => 'application/json'], |
||
46 | $json |
||
47 | ); |
||
48 | |||
49 | $expectedJsonRegExp = json_encode([ |
||
50 | 'Class' => 'InvalidArgumentException', |
||
51 | 'Message' => "Invalid submitted json data, error (.*) : (.*)", |
||
52 | ], 15); |
||
53 | |||
54 | $this->assertRegExp( |
||
55 | $expectedJsonRegExp, |
||
56 | $this->client->getResponse()->getContent() |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | public function testJsonRequestNotAStringError() |
||
61 | { |
||
62 | $json = ['test' => 'test']; |
||
63 | $this->client->request( |
||
64 | 'POST', |
||
65 | '/json-form-extension/json', |
||
66 | [], |
||
67 | [], |
||
68 | ['CONTENT_TYPE' => 'application/json'], |
||
69 | $json |
||
70 | ); |
||
71 | |||
72 | $expectedJson = json_encode([ |
||
73 | 'Class' => 'InvalidArgumentException', |
||
74 | 'Message' => 'Invalid argument: the submitted variable must be a string when you enable the json_format option.', |
||
75 | ], 15); |
||
76 | $this->assertEquals( |
||
77 | $expectedJson, |
||
78 | $this->client->getResponse()->getContent() |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | public function testPostRequest() |
||
83 | { |
||
84 | $this->client->request( |
||
85 | 'POST', |
||
86 | '/json-form-extension/post', |
||
87 | ['form' => ['name' => 'test1']] |
||
88 | ); |
||
89 | $expectedJson = json_encode([ |
||
90 | 'Data' => ['name' => 'test1', 'lastname' => null], |
||
91 | 'NormData' => ['name' => 'test1', 'lastname' => null], |
||
92 | 'ViewData' => ['name' => 'test1', 'lastname' => null], |
||
93 | ], 15); |
||
94 | $this->assertEquals( |
||
95 | $expectedJson, |
||
96 | $this->client->getResponse()->getContent() |
||
97 | ); |
||
98 | } |
||
99 | } |
||
100 |