1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Tests\Twig; |
4
|
|
|
|
5
|
|
|
use MewesK\TwigSpreadsheetBundle\Twig\TwigSpreadsheetExtension; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
8
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BaseTwigTest. |
15
|
|
|
*/ |
16
|
|
|
abstract class BaseTwigTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
protected static $TEMP_PATH = '/../../tmp/'; |
19
|
|
|
protected static $RESOURCE_PATH = '/../Resources/views/'; |
20
|
|
|
protected static $TEMPLATE_PATH = '/../Resources/templates/'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Filesystem |
24
|
|
|
*/ |
25
|
|
|
protected static $fileSystem; |
26
|
|
|
/** |
27
|
|
|
* @var \Twig_Environment |
28
|
|
|
*/ |
29
|
|
|
protected static $environment; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
* |
34
|
|
|
* @throws \Twig_Error_Loader |
35
|
|
|
*/ |
36
|
|
|
public static function setUpBeforeClass() |
37
|
|
|
{ |
38
|
|
|
static::$fileSystem = new Filesystem(); |
39
|
|
|
|
40
|
|
|
$twigFileSystem = new \Twig_Loader_Filesystem([__DIR__.static::$RESOURCE_PATH]); |
41
|
|
|
$twigFileSystem->addPath(__DIR__.static::$TEMPLATE_PATH, 'templates'); |
42
|
|
|
|
43
|
|
|
static::$environment = new \Twig_Environment($twigFileSystem, ['strict_variables' => true]); |
44
|
|
|
static::$environment->addExtension(new TwigSpreadsheetExtension()); |
45
|
|
|
static::$environment->setCache(__DIR__.static::$TEMP_PATH); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
* |
51
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
52
|
|
|
*/ |
53
|
|
|
public static function tearDownAfterClass() |
54
|
|
|
{ |
55
|
|
View Code Duplication |
if (in_array(getenv('DELETE_TEMP_FILES'), ['true', '1', 1, true], true)) { |
|
|
|
|
56
|
|
|
static::$fileSystem->remove(__DIR__.static::$TEMP_PATH); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// |
61
|
|
|
// PhpUnit |
62
|
|
|
// |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
abstract public function formatProvider(); |
68
|
|
|
|
69
|
|
|
// |
70
|
|
|
// Helper |
71
|
|
|
// |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $templateName |
75
|
|
|
* @param string $format |
76
|
|
|
* |
77
|
|
|
* @throws \Twig_Error_Syntax |
78
|
|
|
* @throws \Twig_Error_Loader |
79
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
80
|
|
|
* @throws \InvalidArgumentException |
81
|
|
|
* |
82
|
|
|
* @return Spreadsheet|string |
83
|
|
|
*/ |
84
|
|
|
protected function getDocument($templateName, $format) |
85
|
|
|
{ |
86
|
|
|
// prepare global variables |
87
|
|
|
$request = new Request(); |
88
|
|
|
$request->setRequestFormat($format); |
89
|
|
|
|
90
|
|
|
$requestStack = new RequestStack(); |
91
|
|
|
$requestStack->push($request); |
92
|
|
|
|
93
|
|
|
$appVariable = new AppVariable(); |
94
|
|
|
$appVariable->setRequestStack($requestStack); |
95
|
|
|
|
96
|
|
|
// generate source from template |
97
|
|
|
$source = static::$environment->load($templateName.'.twig')->render(['app' => $appVariable]); |
98
|
|
|
|
99
|
|
|
// create paths |
100
|
|
|
$tempDirPath = __DIR__.static::$TEMP_PATH; |
101
|
|
|
$tempFilePath = $tempDirPath.$templateName.'.'.$format; |
102
|
|
|
|
103
|
|
|
// save source |
104
|
|
|
static::$fileSystem->dumpFile($tempFilePath, $source); |
105
|
|
|
|
106
|
|
|
// load source |
107
|
|
|
switch ($format) { |
108
|
|
|
case 'ods': |
109
|
|
|
$readerType = 'Ods'; |
110
|
|
|
break; |
111
|
|
|
case 'xls': |
112
|
|
|
$readerType = 'Xls'; |
113
|
|
|
break; |
114
|
|
|
case 'xlsx': |
115
|
|
|
$readerType = 'Xlsx'; |
116
|
|
|
break; |
117
|
|
|
case 'pdf': |
118
|
|
|
case 'csv': |
119
|
|
|
return $tempFilePath; |
120
|
|
|
default: |
121
|
|
|
throw new \InvalidArgumentException(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$reader = IOFactory::createReader($readerType); |
125
|
|
|
|
126
|
|
|
return $reader->load($tempFilePath); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.