YamlReaderTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 45
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemporaryFilePath() 0 3 1
A testReader() 0 12 1
A testMissingFile() 0 6 1
A testUnparsableConfiguration() 0 9 1
1
<?php
2
3
namespace Jalle19\StatusManager\Test\Configuration\Reader;
4
5
use Jalle19\StatusManager\Configuration\Reader\YamlReader;
6
use Jalle19\StatusManager\Exception\InvalidConfigurationException;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Yaml\Yaml;
9
10
/**
11
 * Class YamlReaderTest
12
 * @package   Jalle19\StatusManager\Test\Configuration\Reader
13
 * @copyright Copyright &copy; Sam Stenvall 2016-
14
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
15
 */
16
class YamlReaderTest extends TestCase
17
{
18
19
	public function testReader()
20
	{
21
		$configuration = [
22
			'foo' => 'bar',
23
			'baz',
24
		];
25
26
		$tmpFile = $this->getTemporaryFilePath();
27
		file_put_contents($tmpFile, Yaml::dump($configuration));
28
29
		$reader = new YamlReader($tmpFile);
30
		$this->assertEquals($configuration, $reader->readConfiguration());
31
	}
32
33
34
	public function testMissingFile()
35
	{
36
		$this->expectExceptionMessageMatches("*The configuration file does not*");
37
		$this->expectException(InvalidConfigurationException::class);
38
		$reader = new YamlReader('/tmp/does/not/exist');
39
		$reader->readConfiguration();
40
	}
41
42
43
	public function testUnparsableConfiguration()
44
	{
45
		$this->expectExceptionMessageMatches("*Failed to parse*");
46
		$this->expectException(InvalidConfigurationException::class);
47
		$tmpFile = $this->getTemporaryFilePath();
48
		file_put_contents($tmpFile, "\t\tfail");
49
50
		$reader = new YamlReader($tmpFile);
51
		$reader->readConfiguration();
52
	}
53
54
55
	/**
56
	 * @return string
57
	 */
58
	private function getTemporaryFilePath()
59
	{
60
		return tempnam(sys_get_temp_dir(), 'yaml');
61
	}
62
63
}
64