Completed
Push — master ( e63334...a00c3a )
by Mewes
09:18
created

AbstractControllerTest::tearDownAfterClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Tests\Functional;
4
5
use InvalidArgumentException;
6
use PHPExcel_Reader_Excel2007;
7
use PHPExcel_Reader_Excel5;
8
use PHPExcel_Reader_OOCalc;
9
use Symfony\Bundle\FrameworkBundle\Routing\Router;
10
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
11
use Symfony\Component\BrowserKit\Client;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
/**
15
 * Class AbstractControllerTest
16
 * 
17
 * @package MewesK\TwigExcelBundle\Tests\Functional
18
 */
19
abstract class AbstractControllerTest extends WebTestCase
20
{
21
    protected static $CONFIG_FILE;
22
    protected static $TEMP_PATH = '/../../tmp/functional/';
23
24
    /**
25
     * @var Filesystem
26
     */
27
    protected static $fileSystem;
28
    /**
29
     * @var Client
30
     */
31
    protected static $client;
32
    /**
33
     * @var Router
34
     */
35
    protected static $router;
36
37
    //
38
    // Helper
39
    //
40
41
    /**
42
     * @param $uri
43
     * @param $format
44
     * @return \PHPExcel
45
     * @throws \InvalidArgumentException
46
     * @throws \Symfony\Component\Filesystem\Exception\IOException
47
     */
48
    protected function getDocument($uri, $format = 'xlsx')
49
    {
50
        // generate source
51
        static::$client->request('GET', $uri);
52
        $source = static::$client->getResponse()->getContent();
53
        
54
        // create paths
55
        $tempDirPath = __DIR__ . static::$TEMP_PATH;
56
        $tempFilePath = $tempDirPath . 'simple' . '.' . $format;
57
58
        // save source
59
        static::$fileSystem->dumpFile($tempFilePath, $source);
60
61
        // load source
62
        switch ($format) {
63
            case 'ods':
64
                $reader = new PHPExcel_Reader_OOCalc();
65
                break;
66
            case 'xls':
67
                $reader = new PHPExcel_Reader_Excel5();
68
                break;
69
            case 'xlsx':
70
                $reader = new PHPExcel_Reader_Excel2007();
71
                break;
72
            default:
73
                throw new InvalidArgumentException();
74
        }
75
76
        return $reader->load(__DIR__ . static::$TEMP_PATH . 'simple' . '.' . $format);
77
    }
78
79
    //
80
    // PhpUnit
81
    //
82
83
    /**
84
     * @return array
85
     */
86
    abstract public function formatProvider();
87
88
    /**
89
     * {@inheritdoc}
90
     * @throws \RuntimeException
91
     */
92
    protected static function createKernel(array $options = [])
93
    {
94
        return static::$kernel = new AppKernel(
95
            array_key_exists('config', $options) && is_string($options['config']) ? $options['config'] : 'config.yml'
96
        );
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     * @throws \Exception
102
     */
103
    protected function setUp()
104
    {
105
        static::$client = static::createClient(['config' => static::$CONFIG_FILE]);
106
        static::$router = static::$kernel->getContainer()->get('router');
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     * @throws \Exception
112
     */
113
    public static function setUpBeforeClass()
114
    {
115
        static::$fileSystem = new Filesystem();
116
        static::$fileSystem->remove(__DIR__ . static::$TEMP_PATH);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     * @throws \Symfony\Component\Filesystem\Exception\IOException
122
     */
123
    public static function tearDownAfterClass()
124
    {
125
        if (in_array(getenv('DELETE_TEMP_FILES'), ['true', '1', 1, true], true)) {
126
            static::$fileSystem->remove(__DIR__ . static::$TEMP_PATH);
127
        }
128
    }
129
}
130