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 |
||
12 | class DefaultControllerTest extends AbstractControllerTest |
||
13 | { |
||
14 | protected static $ENVIRONMENT = 'controller'; |
||
15 | protected static $TEMP_PATH = '/../../tmp/functional/controller/'; |
||
16 | |||
17 | // |
||
18 | // PhpUnit |
||
19 | // |
||
20 | |||
21 | /** |
||
22 | * @return array |
||
23 | */ |
||
24 | public function formatProvider() |
||
25 | { |
||
26 | return [['ods'], ['xls'], ['xlsx']]; |
||
27 | } |
||
28 | |||
29 | // |
||
30 | // Tests |
||
31 | // |
||
32 | |||
33 | /** |
||
34 | * @param string $format |
||
35 | * |
||
36 | * @throws \Exception |
||
37 | * |
||
38 | * @dataProvider formatProvider |
||
39 | */ |
||
40 | public function testSimple($format) |
||
41 | { |
||
42 | $document = $this->getDocument(static::$router->generate('test_default', ['templateName' => 'simple', '_format' => $format]), $format); |
||
43 | static::assertNotNull($document, 'Document does not exist'); |
||
44 | |||
45 | $sheet = $document->getSheetByName('Test'); |
||
46 | static::assertNotNull($sheet, 'Sheet does not exist'); |
||
47 | |||
48 | static::assertEquals(100270, $sheet->getCell('B22')->getValue(), 'Unexpected value in B22'); |
||
49 | |||
50 | static::assertEquals('=SUM(B2:B21)', $sheet->getCell('B23')->getValue(), 'Unexpected value in B23'); |
||
51 | static::assertTrue($sheet->getCell('B23')->isFormula(), 'Unexpected value in isFormula'); |
||
52 | static::assertEquals(100270, $sheet->getCell('B23')->getCalculatedValue(), 'Unexpected calculated value in B23'); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $format |
||
57 | * |
||
58 | * @throws \Exception |
||
59 | * |
||
60 | * @dataProvider formatProvider |
||
61 | */ |
||
62 | public function testCustomResponse($format) |
||
63 | { |
||
64 | // Generate URI |
||
65 | $uri = static::$router->generate('test_custom_response', ['templateName' => 'simple', '_format' => $format]); |
||
66 | |||
67 | // Generate source |
||
68 | static::$client->request('GET', $uri); |
||
69 | |||
70 | /** |
||
71 | * @var Response |
||
72 | */ |
||
73 | $response = static::$client->getResponse(); |
||
74 | |||
75 | static::assertNotNull($response, 'Response does not exist'); |
||
76 | static::assertEquals('attachment; filename="foobar.bin"', $response->headers->get('Content-Disposition'), 'Unexpected or missing header "Content-Disposition"'); |
||
77 | static::assertEquals(600, $response->getMaxAge(), 'Unexpected value in maxAge'); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $format |
||
82 | * |
||
83 | * @throws \Exception |
||
84 | * |
||
85 | * @dataProvider formatProvider |
||
86 | */ |
||
87 | public function testDocumentTemplatePath1($format) |
||
88 | { |
||
89 | $document = $this->getDocument(static::$router->generate('test_default', ['templateName' => 'documentTemplatePath1', '_format' => $format]), $format); |
||
90 | static::assertNotNull($document, 'Document does not exist'); |
||
91 | |||
92 | $sheet = $document->getSheet(0); |
||
93 | static::assertNotNull($sheet, 'Sheet does not exist'); |
||
94 | |||
95 | static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1'); |
||
96 | static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1'); |
||
97 | static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2'); |
||
98 | static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2'); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param string $format |
||
103 | * |
||
104 | * @throws \Exception |
||
105 | * |
||
106 | * @dataProvider formatProvider |
||
107 | */ |
||
108 | public function testDocumentTemplatePath2($format) |
||
109 | { |
||
110 | $document = $this->getDocument(static::$router->generate('test_default', ['templateName' => 'documentTemplatePath2', '_format' => $format]), $format); |
||
111 | static::assertNotNull($document, 'Document does not exist'); |
||
112 | |||
113 | $sheet = $document->getSheet(0); |
||
114 | static::assertNotNull($sheet, 'Sheet does not exist'); |
||
115 | |||
116 | static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1'); |
||
117 | static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1'); |
||
118 | static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2'); |
||
119 | static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2'); |
||
120 | } |
||
121 | } |
||
122 |