AbstractControllerTest::getDocument()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 4
nop 2
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
        if (null === static::$class) {
95
            static::$class = static::getKernelClass();
96
        }
97
98
        return new static::$class(
99
            array_key_exists('config', $options) && is_string($options['config']) ? $options['config'] : 'config.yml'
100
        );
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     * @throws \Exception
106
     */
107
    protected function setUp()
108
    {
109
        static::$client = static::createClient(['config' => static::$CONFIG_FILE]);
110
        static::$router = static::$kernel->getContainer()->get('router');
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     * @throws \Exception
116
     */
117
    public static function setUpBeforeClass()
118
    {
119
        static::$fileSystem = new Filesystem();
120
        static::$fileSystem->remove(__DIR__ . static::$TEMP_PATH);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     * @throws \Symfony\Component\Filesystem\Exception\IOException
126
     */
127
    public static function tearDownAfterClass()
128
    {
129
        if (in_array(getenv('DELETE_TEMP_FILES'), ['true', '1', 1, true], true)) {
130
            static::$fileSystem->remove(__DIR__ . static::$TEMP_PATH);
131
        }
132
    }
133
}
134