|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MewesK\TwigExcelBundle\Tests\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use MewesK\TwigExcelBundle\Twig\TwigExcelExtension; |
|
7
|
|
|
use PHPExcel_Reader_Excel2007; |
|
8
|
|
|
use PHPExcel_Reader_Excel5; |
|
9
|
|
|
use PHPExcel_Reader_OOCalc; |
|
10
|
|
|
use PHPUnit_Framework_TestCase; |
|
11
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
15
|
|
|
use Twig_Environment; |
|
16
|
|
|
use Twig_Loader_Filesystem; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class AbstractTwigTest |
|
20
|
|
|
* @package MewesK\TwigExcelBundle\Tests\Twig |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractTwigTest extends PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
protected static $TEMP_PATH = '/../../tmp/'; |
|
25
|
|
|
protected static $RESOURCE_PATH = '/../Resources/views/'; |
|
26
|
|
|
protected static $TEMPLATE_PATH = '/../Resources/templates/'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Filesystem |
|
30
|
|
|
*/ |
|
31
|
|
|
protected static $fileSystem; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Twig_Environment |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static $environment; |
|
36
|
|
|
|
|
37
|
|
|
// |
|
38
|
|
|
// Helper |
|
39
|
|
|
// |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $templateName |
|
43
|
|
|
* @param string $format |
|
44
|
|
|
* @return \PHPExcel |
|
45
|
|
|
* @throws \Twig_Error_Syntax |
|
46
|
|
|
* @throws \Twig_Error_Loader |
|
47
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
48
|
|
|
* @throws \InvalidArgumentException |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getDocument($templateName, $format) |
|
51
|
|
|
{ |
|
52
|
|
|
// prepare global variables |
|
53
|
|
|
$request = new Request(); |
|
54
|
|
|
$request->setRequestFormat($format); |
|
55
|
|
|
|
|
56
|
|
|
$requestStack = new RequestStack(); |
|
57
|
|
|
$requestStack->push($request); |
|
58
|
|
|
|
|
59
|
|
|
$appVariable = new AppVariable(); |
|
60
|
|
|
$appVariable->setRequestStack($requestStack); |
|
61
|
|
|
|
|
62
|
|
|
// generate source from template |
|
63
|
|
|
$source = static::$environment->loadTemplate($templateName . '.twig')->render(['app' => $appVariable]); |
|
64
|
|
|
|
|
65
|
|
|
// create paths |
|
66
|
|
|
$tempDirPath = __DIR__ . static::$TEMP_PATH; |
|
67
|
|
|
$tempFilePath = $tempDirPath . $templateName . '.' . $format; |
|
68
|
|
|
|
|
69
|
|
|
// save source |
|
70
|
|
|
static::$fileSystem->dumpFile($tempFilePath, $source); |
|
71
|
|
|
|
|
72
|
|
|
// load source |
|
73
|
|
|
switch ($format) { |
|
74
|
|
|
case 'ods': |
|
75
|
|
|
$reader = new PHPExcel_Reader_OOCalc(); |
|
76
|
|
|
break; |
|
77
|
|
|
case 'xls': |
|
78
|
|
|
$reader = new PHPExcel_Reader_Excel5(); |
|
79
|
|
|
break; |
|
80
|
|
|
case 'xlsx': |
|
81
|
|
|
$reader = new PHPExcel_Reader_Excel2007(); |
|
82
|
|
|
break; |
|
83
|
|
|
case 'pdf': |
|
84
|
|
|
case 'csv': |
|
85
|
|
|
return $tempFilePath; |
|
86
|
|
|
default: |
|
87
|
|
|
throw new InvalidArgumentException(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $reader->load($tempFilePath); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// |
|
94
|
|
|
// PhpUnit |
|
95
|
|
|
// |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract public function formatProvider(); |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
* @throws \Twig_Error_Loader |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function setUpBeforeClass() |
|
107
|
|
|
{ |
|
108
|
|
|
static::$fileSystem = new Filesystem(); |
|
109
|
|
|
|
|
110
|
|
|
$twigFileSystem = new Twig_Loader_Filesystem([__DIR__ . static::$RESOURCE_PATH]); |
|
111
|
|
|
$twigFileSystem->addPath( __DIR__ . static::$TEMPLATE_PATH, 'templates'); |
|
112
|
|
|
|
|
113
|
|
|
static::$environment = new Twig_Environment($twigFileSystem, ['strict_variables' => true]); |
|
114
|
|
|
static::$environment->addExtension(new TwigExcelExtension()); |
|
115
|
|
|
static::$environment->setCache(__DIR__ . static::$TEMP_PATH); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function tearDownAfterClass() |
|
123
|
|
|
{ |
|
124
|
|
|
if (in_array(getenv('DELETE_TEMP_FILES'), ['true', '1', 1, true], true)) { |
|
125
|
|
|
static::$fileSystem->remove(__DIR__ . static::$TEMP_PATH); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|