WriteCurrencyInfoMapFilesTest::createTestDir()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace VinaiKopp\CurrencyInfo\Build;
4
5
/**
6
 * @covers \VinaiKopp\CurrencyInfo\Build\WriteCurrencyInfoMapFiles
7
 */
8
class WriteCurrencyInfoMapFilesTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var string
12
     */
13
    private $tmpDir;
14
15
    protected function setUp()
16
    {
17
        $this->tmpDir = sys_get_temp_dir() . '/' . uniqid('test-');
18
        $this->createTestDir();
19
    }
20
21
    protected function tearDown()
22
    {
23
        if (is_dir($this->tmpDir)) {
24
            $this->removeTestDir();
25
        }
26
    }
27
28
    private function createTestDir()
29
    {
30
        mkdir($this->tmpDir, 0700, true);
31
        if (!is_dir($this->tmpDir) || !is_writable($this->tmpDir)) {
32
            $this->markTestSkipped(sprintf('Unable to write to temporary directory "%s"', $this->tmpDir));
33
        }
34
    }
35
36
    private function removeTestDir()
37
    {
38
        foreach (new \DirectoryIterator($this->tmpDir) as $fileName => $fileInfo) {
39
            if (!$fileInfo->isDir()) {
40
                unlink($fileInfo->getRealPath());
41
            }
42
        }
43
        rmdir($this->tmpDir);
44
    }
45
46
    public function testWritesMaps()
47
    {
48
        $infoTypeKeys = ['foo', 'bar'];
49
        $source = [
50
            ['foo' => 'BAZ', 'bar' => 2],
51
            ['foo' => 'QUX', 'bar' => 3],
52
        ];
53
54
        /** @var CurrencyInfoKeys|\PHPUnit_Framework_MockObject_MockObject $stubCurrencyInfoKeys */
55
        $stubCurrencyInfoKeys = $this->getMock(CurrencyInfoKeys::class);
56
        $stubCurrencyInfoKeys->method('get')->willReturn($infoTypeKeys);
57
58
        $mapWriter = new WriteCurrencyInfoMapFiles(new CurrencyInfoSource($source), new \SplFileInfo($this->tmpDir));
59
        $mapWriter->write($stubCurrencyInfoKeys);
60
61
        $this->assertFileExists($this->tmpDir . '/currencymap-foo.php');
62
        $this->assertFileExists($this->tmpDir . '/currencymap-bar.php');
63
        $this->assertFileExists($this->tmpDir . '/currencymap.php');
64
    }
65
}
66