AbstractTestCase::dataFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Btccom\JustEncrypt\Test;
4
5
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
6
{
7
    /**
8
     * @var array
9
     */
10
    private $vectors = null;
11
12
    /**
13
     * AbstractTestCase constructor.
14
     * @param null $name
15
     * @param array $data
16
     * @param string $dataName
17
     */
18
    public function __construct($name = null, array $data = [], $dataName = '')
19
    {
20
        $this->getTestVectors();
21
        parent::__construct($name, $data, $dataName);
22
    }
23
24
    /**
25
     * @param string $file
26
     * @return string
27
     */
28
    public function dataPath($file)
29
    {
30
        return __DIR__ . '/data/' . $file;
31
    }
32
33
    /**
34
     * @param string $filename
35
     * @return string
36
     */
37
    public function dataFile($filename)
38
    {
39
        $contents = file_get_contents($this->dataPath($filename));
40
        if (false === $contents) {
41
            throw new \RuntimeException('Failed to data file ' . $filename);
42
        }
43
        return $contents;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    private function readTestVectorsFile()
50
    {
51
        $decoded = json_decode(file_get_contents(__DIR__ .'/data/crypt_vectors.json'), true);
52
        if (!$decoded || json_last_error() !== JSON_ERROR_NONE) {
53
            throw new \RuntimeException('failed to read vectors file');
54
        }
55
56
        return $decoded;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getTestVectors()
63
    {
64
        if (null === $this->vectors) {
65
            $this->vectors = $this->readTestVectorsFile();
66
        }
67
68
        return $this->vectors;
69
    }
70
}
71