DefaultControllerTest::testDocumentTemplatePath2()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Tests\Functional;
4
5
use Symfony\Component\HttpFoundation\Response;
6
7
/**
8
 * Class DefaultControllerTest
9
 * @package MewesK\TwigExcelBundle\Tests\Functional
10
 */
11
class DefaultControllerTest extends AbstractControllerTest
12
{
13
    protected static $TEMP_PATH = '/../../tmp/functional/controller/';
14
15
    //
16
    // PhpUnit
17
    //
18
19
    /**
20
     * @return array
21
     */
22
    public function formatProvider()
23
    {
24
        return [['ods'], ['xls'], ['xlsx']];
25
    }
26
27
    //
28
    // Tests
29
    //
30
31
    /**
32
     * @param string $format
33
     * @throws \Exception
34
     *
35
     * @dataProvider formatProvider
36
     */
37
    public function testSimple($format)
38
    {
39
        $document = $this->getDocument(static::$router->generate('test_default', ['templateName' => 'simple', '_format' => $format]), $format);
40
        static::assertNotNull($document, 'Document does not exist');
41
42
        $sheet = $document->getSheetByName('Test');
43
        static::assertNotNull($sheet, 'Sheet does not exist');
44
45
        static::assertEquals(100270, $sheet->getCell('B22')->getValue(), 'Unexpected value in B22');
46
47
        static::assertEquals('=SUM(B2:B21)', $sheet->getCell('B23')->getValue(), 'Unexpected value in B23');
48
        static::assertTrue($sheet->getCell('B23')->isFormula(), 'Unexpected value in isFormula');
49
        static::assertEquals(100270, $sheet->getCell('B23')->getCalculatedValue(), 'Unexpected calculated value in B23');
50
    }
51
52
    /**
53
     * @param string $format
54
     * @throws \Exception
55
     *
56
     * @dataProvider formatProvider
57
     */
58
    public function testCustomResponse($format)
59
    {
60
        // Generate URI
61
        $uri = static::$router->generate('test_custom_response', ['templateName' => 'simple', '_format' => $format]);
62
63
        // Generate source
64
        static::$client->request('GET', $uri);
65
66
        /**
67
         * @var $response Response
68
         */
69
        $response = static::$client->getResponse();
70
71
        static::assertNotNull($response, 'Response does not exist');
72
        static::assertEquals('attachment; filename="foobar.bin"', $response->headers->get('content-disposition'), 'Unexpected or missing header "Content-Disposition"');
73
        static::assertEquals('max-age=600, private', $response->headers->get('cache-control'), 'Unexpected or missing header "Cache-Control"');
74
    }
75
76
    /**
77
     * @param string $format
78
     * @throws \Exception
79
     *
80
     * @dataProvider formatProvider
81
     */
82
    public function testDocumentTemplatePath1($format)
83
    {
84
        $document = $this->getDocument(static::$router->generate('test_default', ['templateName' => 'documentTemplatePath1', '_format' => $format]), $format);
85
        static::assertNotNull($document, 'Document does not exist');
86
87
        $sheet = $document->getSheet(0);
88
        static::assertNotNull($sheet, 'Sheet does not exist');
89
90
        static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
91
        static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
92
        static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
93
        static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
94
    }
95
96
    /**
97
     * @param string $format
98
     * @throws \Exception
99
     *
100
     * @dataProvider formatProvider
101
     */
102
    public function testDocumentTemplatePath2($format)
103
    {
104
        $document = $this->getDocument(static::$router->generate('test_default', ['templateName' => 'documentTemplatePath2', '_format' => $format]), $format);
105
        static::assertNotNull($document, 'Document does not exist');
106
107
        $sheet = $document->getSheet(0);
108
        static::assertNotNull($sheet, 'Sheet does not exist');
109
110
        static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
111
        static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
112
        static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
113
        static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
114
    }
115
}
116