/
AbstractToolTest.php
163 lines (137 loc) · 5.93 KB
/
AbstractToolTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Unit tests for Open Power Autoloader
*
* @author Tomasz "Zyx" Jędrzejewski
* @copyright Copyright (c) 2009 Invenzzia Group
* @license http://www.invenzzia.org/license/new-bsd New BSD License
*/
namespace TestSuite\Toolset;
use Extra\DummyTool;
/**
* @covers \Opl\Autoloader\Toolset\AbstractTool
* @runTestsInSeparateProcesses
*/
class AbstractToolTest extends \PHPUnit_Framework_TestCase
{
public function testAddingNamespace()
{
$tool = new DummyTool();
$this->assertFalse($tool->hasNamespace('Foo'));
$this->assertFalse($tool->hasNamespace('Bar'));
$this->assertFalse($tool->hasNamespace('Bar\Joe'));
$tool->addNamespace('Foo', './');
$tool->addNamespace('Foo\Joe', './');
$this->assertTrue($tool->hasNamespace('Foo'));
$this->assertFalse($tool->hasNamespace('Bar'));
$this->assertTrue($tool->hasNamespace('Foo\Joe'));
} // end testAddingNamespace();
public function testAddNamespaceSetsDefaultPathAndExtension()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './foo/bar/');
$reflection = new \ReflectionObject($tool);
$namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('extensions');
$extensionsProperty->setAccessible(true);
$this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($tool));
$this->assertEquals(array('Foo' => '.php'), $extensionsProperty->getValue($tool));
} // end testAddNamespaceSetsDefaultPathAndExtension();
public function testAddNamespaceSetsCustomPathAndExtension()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './bar/joe/', '.php5');
$reflection = new \ReflectionObject($tool);
$namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('extensions');
$extensionsProperty->setAccessible(true);
$this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($tool));
$this->assertEquals(array('Foo' => '.php5'), $extensionsProperty->getValue($tool));
} // end testAddNamespaceSetsCustomPathAndExtension();
public function testAddNamespaceAddsTrailingSlash()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './bar/joe', '.php5');
$reflection = new \ReflectionObject($tool);
$namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('extensions');
$extensionsProperty->setAccessible(true);
$this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($tool));
$this->assertEquals(array('Foo' => '.php5'), $extensionsProperty->getValue($tool));
} // end testAddNamespaceAddsTrailingSlash();
/**
* @expectedException DomainException
*/
public function testAddNamespaceThrowsExceptionWhenNamespaceExists()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './foo/bar/');
$this->assertTrue($tool->hasNamespace('Foo'));
$tool->addNamespace('Foo', './foo/bar/');
} // end testNamespaceThrowsExceptionWhenNamespaceExists();
public function testRemoveNamespace()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './foo/bar/');
$this->assertTrue($tool->hasNamespace('Foo'));
$reflection = new \ReflectionObject($tool);
$namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('extensions');
$extensionsProperty->setAccessible(true);
$this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($tool));
$this->assertEquals(array('Foo' => '.php'), $extensionsProperty->getValue($tool));
$tool->removeNamespace('Foo');
$this->assertFalse($tool->hasNamespace('Foo'));
$this->assertEquals(array(), $namespacesProperty->getValue($tool));
$this->assertEquals(array(), $extensionsProperty->getValue($tool));
} // end testRemoveLibrary();
/**
* @depends testRemoveNamespace
* @expectedException DomainException
*/
public function testRemoveNamespaceThrowsExceptionWhenNamespaceDoesNotExist()
{
$tool = new DummyTool();
$this->assertFalse($tool->hasNamespace('Moo'));
$tool->removeNamespace('Moo');
} // end testRemoveNamespaceThrowsExceptionWhenNamespaceDoesNotExist();
public function testToFilenameReturnsTheClassName()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './foo/src/');
$tool->addNamespace('Foo\\Bar', './bar/src/');
$this->assertEquals('./foo/src/Foo/File.php', $tool->toFilename('Foo\\File'));
$this->assertEquals('./foo/src/Foo/Bar/File.php', $tool->toFilename('Foo\\Bar\\File'));
$this->assertEquals('./foo/src/Foo/File/Name.php', $tool->toFilename('Foo\\File_Name'));
} // end testToFilenameReturnsTheClassName();
/**
* @expectedException Opl\Autoloader\Exception\TranslationException
*/
public function testToFilenameThrowsExceptionIfNamespaceDoesNotExist()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './foo/src/');
$this->assertFalse($tool->hasNamespace('Bar'));
$tool->toFilename('Bar\\Joe');
} // end testToFilenameThrowsExceptionIfNamespaceDoesNotExist();
public function testToFilenameAllowsToSkipTheNamespacePath()
{
$tool = new DummyTool();
$tool->addNamespace('Foo', './foo/src/');
$tool->addNamespace('Foo\\Bar', './bar/src/');
$this->assertEquals('Foo/File.php', $tool->toFilename('Foo\\File', false));
$this->assertEquals('Foo/Bar/File.php', $tool->toFilename('Foo\\Bar\\File', false));
$this->assertEquals('Foo/File/Name.php', $tool->toFilename('Foo\\File_Name', false));
} // end testToFilenameReturnsTheClassName();
public function testGettingAndSettingTheNamespaceSeparator()
{
$tool = new DummyTool();
$this->assertEquals('\\', $tool->getNamespaceSeparator());
$this->assertSame($tool, $tool->setNamespaceSeparator('_'));
$this->assertEquals('_', $tool->getNamespaceSeparator());
} // end testGettingAndSettingTheNamespaceSeparator();
} // end AbstractToolTest;