Test Failed
Push — master ( 389090...91c650 )
by Ryuichi
02:53
created

ClassLoaderTest::ngLoadTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace WebStream\ClassLoader\Test;
3
4
require_once dirname(__FILE__) . '/../Modules/DI/Injector.php';
5
require_once dirname(__FILE__) . '/../Modules/IO/File.php';
6
require_once dirname(__FILE__) . '/../Modules/IO/InputStream.php';
7
require_once dirname(__FILE__) . '/../Modules/IO/FileInputStream.php';
8
require_once dirname(__FILE__) . '/../ClassLoader.php';
9
require_once dirname(__FILE__) . '/../Test/Providers/ClassLoaderProvider.php';
10
require_once dirname(__FILE__) . '/../Test/Fixtures/DummyLogger.php';
11
12
use WebStream\ClassLoader\ClassLoader;
13
use WebStream\ClassLoader\Test\Providers\ClassLoaderProvider;
14
use WebStream\ClassLoader\Test\Fixtures\DummyLogger;
15
16
/**
17
* ClassLoaderTest
18
* @author Ryuichi TANAKA.
19
* @since 2017/01/21
20
* @version 0.7
21
 */
22
class ClassLoaderTest extends \PHPUnit\Framework\TestCase
23
{
24
    use ClassLoaderProvider;
25
26
    /**
27
     * 正常系
28
     * loadが成功すること
29
     * @test
30
     * @dataProvider loadProvider
31
     */
32
    public function okLoadTest($rootDir, $className)
33
    {
34
        $classLoader = new ClassLoader($rootDir);
35
        $this->assertCount(1, $classLoader->load($className));
36
    }
37
38
    /**
39
     * 正常系
40
     * loadが成功すること(リスト読み込み)
41
     * @dataProvider loadListProvider
42
     */
43
    public function okLoadListTest($rootDir, $classList)
44
    {
45
        $classLoader = new ClassLoader($rootDir);
46
        $this->assertCount(2, $classLoader->load($classList));
47
    }
48
49
    /**
50
     * 正常系
51
     * サブディレクトリを指定してloadが成功すること
52
     * @test
53
     * @dataProvider loadSubDirProvider
54
     */
55
    public function okLoadSubDirTest($rootDir, $className, $subDirList)
56
    {
57
        $classLoader = new ClassLoader($rootDir, $subDirList);
0 ignored issues
show
Unused Code introduced by
The call to ClassLoader::__construct() has too many arguments starting with $subDirList.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
        $this->assertCount(1, $classLoader->load($className));
59
    }
60
61
    /**
62
     * 正常系
63
     * importで指定ファイルをインポートできること
64
     * @test
65
     * @dataProvider importProvider
66
     */
67
    public function okImportTest($rootDir, $className)
68
    {
69
        $classLoader = new ClassLoader($rootDir);
70
        $this->assertTrue($classLoader->import($className));
71
        $this->assertTrue(class_exists(\WebStream\ClassLoader\Test\Fixtures\ImportFixture1::class));
72
    }
73
74
    /**
75
     * 正常系
76
     * importAllで指定ディレクトリ配下のファイルをすべてインポートできること
77
     * @test
78
     * @dataProvider importAllProvider
79
     */
80
    public function okImportAllTest($rootDir, $dirName)
81
    {
82
        $classLoader = new ClassLoader($rootDir);
83
        $this->assertTrue($classLoader->importAll($dirName));
84
        $this->assertTrue(class_exists(\WebStream\ClassLoader\Test\Fixtures\ImportFixture2::class));
85
        $this->assertTrue(class_exists(\WebStream\ClassLoader\Test\Fixtures\ImportFixture3::class));
86
    }
87
88
    /**
89
     * 正常系
90
     * フィルタ付きimportで指定ファイルをインポートできること
91
     * @test
92
     * @dataProvider filteredImportProvider
93
     */
94
    public function okFilteredImportTest($rootDir, $className, $ignoreClassName)
95
    {
96
        $classLoader = new ClassLoader($rootDir);
97
        $this->assertTrue($classLoader->import($className, function ($filepath) use ($ignoreClassName) {
98
            return $filepath === $ignoreClassName;
99
        }));
100
        $this->assertTrue(class_exists(\WebStream\ClassLoader\Test\Fixtures\ImportFixture4::class));
101
    }
102
103
    /**
104
     * 正常系
105
     * フィルタ付きimportAllで指定ファイルをインポートできること
106
     * @test
107
     * @dataProvider filteredImportAllProvider
108
     */
109
    public function okFilteredImportAllTest($rootDir, $dirName, $ignoreClassName)
110
    {
111
        $classLoader = new ClassLoader($rootDir);
112
        $this->assertTrue($classLoader->importAll($dirName, function ($filepath) use ($ignoreClassName) {
113
            return $filepath === $ignoreClassName;
114
        }));
115
        $this->assertTrue(class_exists(\WebStream\ClassLoader\Test\Fixtures\ImportFixture5::class));
116
    }
117
118
    /**
119
     * 正常系
120
     * 指定ファイルの名前空間が取得できること
121
     * @test
122
     * @dataProvider loadNamespaceProvider
123
     */
124
    public function okLoadNamespaceTest($rootDir, $filePath, $list)
125
    {
126
        $classLoader = new ClassLoader($rootDir);
127
        $namespaces = $classLoader->getNamespaces($filePath);
128
        foreach ($list as $namespace) {
129
            $this->assertTrue(in_array($namespace, $namespaces, true));
130
        }
131
    }
132
133
    /**
134
     * 異常系
135
     * loadに失敗した場合、結果が0件になること
136
     * @test
137
     * @dataProvider unLoadProvider
138
     */
139
    public function ngLoadTest($rootDir, $className)
140
    {
141
        $classLoader = new ClassLoader($rootDir);
142
        $this->assertCount(0, $classLoader->load($className));
143
    }
144
145
    /**
146
     * 異常系
147
     * @test
148
     * 存在しないファイルはインポートできないこと
149
     * @dataProvider unImportProvider
150
     */
151
    public function ngImportTest($rootDir)
152
    {
153
        $classLoader = new ClassLoader($rootDir);
154
        $this->assertFalse($classLoader->import("Dummy.php"));
155
    }
156
157
    /**
158
     * 異常系
159
     * フィルタにマッチしない場合、importAllで指定ファイルをインポートできないこと
160
     * @test
161
     * @dataProvider filteredImportAllProvider
162
     */
163
    public function ngFilteredImportAllTest($rootDir, $dirName, $ignoreClassName)
164
    {
165
        $classLoader = new ClassLoader($rootDir);
166
        $classLoader->inject('logger', new DummyLogger());
167
        $classLoader->importAll($dirName, function ($filepath) use ($ignoreClassName) {
0 ignored issues
show
Unused Code introduced by
The parameter $filepath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
            return false;
169
        });
170
        $this->expectOutputString('');
171
    }
172
173
    /**
174
     * 異常系
175
     * 指定ファイルの名前空間が取得できないこと
176
     * @test
177
     * @dataProvider unLoadNamespaceProvider
178
     */
179
    public function ngLoadNamespaceTest($rootDir, $filePath, $result)
180
    {
181
        $classLoader = new ClassLoader($rootDir);
182
        $this->assertEquals($classLoader->getNamespaces($filePath), $result);
183
    }
184
}
185