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