1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
9
|
|
|
use Symfony\Component\BrowserKit\Client; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BaseFunctionalTest. |
15
|
|
|
*/ |
16
|
|
|
abstract class BaseFunctionalTest extends WebTestCase |
17
|
|
|
{ |
18
|
|
|
protected static $ENVIRONMENT; |
19
|
|
|
protected static $TEMP_PATH; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Filesystem |
23
|
|
|
*/ |
24
|
|
|
protected static $fileSystem; |
25
|
|
|
/** |
26
|
|
|
* @var Client |
27
|
|
|
*/ |
28
|
|
|
protected static $client; |
29
|
|
|
/** |
30
|
|
|
* @var Router |
31
|
|
|
*/ |
32
|
|
|
protected static $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
38
|
|
|
*/ |
39
|
|
|
public static function setUpBeforeClass() |
40
|
|
|
{ |
41
|
|
|
static::$fileSystem = new Filesystem(); |
42
|
|
|
static::$fileSystem->remove(static::$TEMP_PATH); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
* |
48
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
49
|
|
|
*/ |
50
|
|
|
public static function tearDownAfterClass() |
51
|
|
|
{ |
52
|
|
|
if (!getenv('TSB_KEEP_CACHE')) { |
53
|
|
|
static::$fileSystem->remove(static::$TEMP_PATH); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
* |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
|
|
protected function setUp() |
63
|
|
|
{ |
64
|
|
|
static::$client = static::createClient(['environment' => static::$ENVIRONMENT]); |
65
|
|
|
static::$router = static::$kernel->getContainer()->get('router'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// |
69
|
|
|
// PhpUnit |
70
|
|
|
// |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
abstract public function formatProvider(); |
76
|
|
|
|
77
|
|
|
// |
78
|
|
|
// Helper |
79
|
|
|
// |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $uri |
83
|
|
|
* @param string $format |
84
|
|
|
* |
85
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
86
|
|
|
* |
87
|
|
|
* @return Spreadsheet |
88
|
|
|
*/ |
89
|
|
|
protected function getDocument(string $uri, string $format = 'xlsx'): Spreadsheet |
90
|
|
|
{ |
91
|
|
|
$format = strtolower($format); |
92
|
|
|
|
93
|
|
|
// generate source |
94
|
|
|
static::$client->request(Request::METHOD_GET, $uri); |
95
|
|
|
$source = static::$client->getResponse()->getContent(); |
96
|
|
|
|
97
|
|
|
// create path for temp file |
98
|
|
|
$path = sprintf('%s/simple.%s', static::$TEMP_PATH, $format); |
99
|
|
|
|
100
|
|
|
// save source |
101
|
|
|
static::$fileSystem->dumpFile($path, $source); |
102
|
|
|
|
103
|
|
|
// load source |
104
|
|
|
return IOFactory::createReader(ucfirst($format))->load($path); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|