Completed
Pull Request — master (#74)
by John
03:17 queued 38s
created

LoaderTest::willFailWhenFileDoesNotExist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Document;
10
11
use KleijnWeb\SwaggerBundle\Document\Loader;
12
use org\bovigo\vfs\vfsStream;
13
use org\bovigo\vfs\vfsStreamWrapper;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class LoaderTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @test
22
     * @expectedException \KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotReadableException
23
     */
24
    public function willFailWhenFileDoesNotExist()
25
    {
26
        $loader = new Loader();
27
        $loader->load('does/not/exist.json');
28
    }
29
30
    /**
31
     * @test
32
     * @expectedException \KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotDecodableException
33
     */
34
    public function willFailWhenYamlIsNotDecodable()
35
    {
36
        $yaml = <<<YAML
37
foo:
38
  bar: 1
39
 foo:
40
  bar: 1
41
YAML;
42
43
        $this->loadContentViaVfs('invalid.yaml', $yaml);
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function canLoadValidYaml()
50
    {
51
        $yaml = <<<YAML
52
foo:
53
  bar: 1
54
YAML;
55
        $this->assertSame(1, $this->loadContentViaVfs('invalid.yaml', $yaml)->foo->bar);
56
    }
57
58
    /**
59
     * @test
60
     * @expectedException \KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotDecodableException
61
     */
62
    public function willFailWhenJsonIsNotDecodable()
63
    {
64
        $this->loadContentViaVfs('invalid/json.json', 'NOT VALID JSON');
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function canLoadValidJson()
71
    {
72
        $this->loadContentViaVfs('some/valid.json', json_encode(['valid' => true]));
73
    }
74
75
    /**
76
     * @test
77
     * @expectedException \KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotDecodableException
78
     */
79
    public function willFailWhenContentIsNeitherYamlNorJson()
80
    {
81
        $this->loadContentViaVfs('resource.xml', '<foo>bar</foo>');
82
    }
83
84
    /**
85
     * @param string $path
86
     * @param string $content
87
     *
88
     * @return \stdClass
89
     * @throws \KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotDecodableException
90
     * @throws \KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotReadableException
91
     * @throws \org\bovigo\vfs\vfsStreamException
92
     */
93
    private function loadContentViaVfs($path, $content)
94
    {
95
        $rootDirName = 'willProperlyResolveExternalReferences';
96
        vfsStreamWrapper::register();
97
        vfsStreamWrapper::setRoot(vfsStream::newDirectory($rootDirName));
98
99
        $loader = new Loader();
100
101
        $filePathName = vfsStream::url("$rootDirName/$path");
102
103
        $dir = dirname($filePathName);
104
        if (!is_dir($dir)) {
105
            mkdir($dir, 0777, true);
106
        }
107
108
        file_put_contents($filePathName, $content);
109
110
        return $loader->load($filePathName);
111
    }
112
}
113