1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Tests\Framework\Utility; |
4
|
|
|
|
5
|
|
|
use SilverLeague\Console\Framework\Utility\ObjectUtilities; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @coverDefaultClass \SilverLeague\Console\Framework\Utility\ObjectUtilities |
10
|
|
|
* @package silverstripe-console |
11
|
|
|
* @author Robbie Averill <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ObjectUtilitiesTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritDoc} |
17
|
|
|
*/ |
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->utility = $this |
23
|
|
|
->getMockBuilder(ObjectUtilities::class) |
24
|
|
|
->setMethods([]) |
25
|
|
|
->getMockForTrait(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Test that a ReflectionClass is returned when a valid class name is provided |
30
|
|
|
*/ |
31
|
|
|
public function testGetReflectionClass() |
32
|
|
|
{ |
33
|
|
|
$this->assertInstanceOf(ReflectionClass::class, $this->utility->getReflection("SilverStripe\Core\Object")); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test that ReflectionExceptions are handled gracefully for non existent classes |
38
|
|
|
*/ |
39
|
|
|
public function testGetReflectionHandlesMissingClassesGracefully() |
40
|
|
|
{ |
41
|
|
|
$this->assertFalse($this->utility->getReflection("Foo\Bar\Monkey\Man\Pluto\Saturn")); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Test that a composer configured module name is returned for a valid SilverStripe module |
46
|
|
|
*/ |
47
|
|
|
public function testGetModuleNameForValidClass() |
48
|
|
|
{ |
49
|
|
|
$this->assertSame('silverstripe/framework', $this->utility->getModuleName("SilverStripe\Core\Object")); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Test that an empty string is returned gracefully if a module cannot be found |
54
|
|
|
*/ |
55
|
|
|
public function testReturnEmptyStringWhenModuleNameCantBeFound() |
56
|
|
|
{ |
57
|
|
|
$this->assertSame('', $this->utility->getModuleName("Monolog\Logger")); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Ensure that the project name can be retrieved from its composer configuration, and if it's the $project (mysite) |
62
|
|
|
* then use the root directory composer.json |
63
|
|
|
* |
64
|
|
|
* @dataProvider composerConfigurationProvider |
65
|
|
|
*/ |
66
|
|
|
public function testGetModuleNameFromComposerConfiguration($projectName, $folderName, $expected) |
67
|
|
|
{ |
68
|
|
|
global $project; |
|
|
|
|
69
|
|
|
$project = $projectName; |
70
|
|
|
$this->assertSame($expected, $this->utility->getModuleComposerConfiguration($folderName)['name']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array[] |
75
|
|
|
*/ |
76
|
|
|
public function composerConfigurationProvider() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
['mysite', 'mysite', 'silverleague/console'], |
80
|
|
|
['mysite', 'framework', 'silverstripe/framework'] |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Test that when an invalid class is passed to getModuleName it will return a blank string |
86
|
|
|
*/ |
87
|
|
|
public function testGetModuleNameFromInvalidClassReturnsEmptyString() |
88
|
|
|
{ |
89
|
|
|
$this->assertSame('', $this->utility->getModuleName("Far\Out\There\Nowhere")); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test that when the module's folder name is empty or missing, a blank string is returned |
94
|
|
|
*/ |
95
|
|
|
public function testEmptyFolderNameInPathReturnsEmptyString() |
96
|
|
|
{ |
97
|
|
|
$reflectionMock = $this |
98
|
|
|
->getMockBuilder(ReflectionClass::class) |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->setMethods(['getFileName']) |
101
|
|
|
->getMock(); |
102
|
|
|
|
103
|
|
|
$reflectionMock |
104
|
|
|
->expects($this->once()) |
105
|
|
|
->method('getFileName') |
106
|
|
|
->willReturn(SILVERSTRIPE_ROOT_DIR . '/ /src/ORM/DataObject.php'); |
107
|
|
|
|
108
|
|
|
$mock = $this |
109
|
|
|
->getMockBuilder(ObjectUtilities::class) |
110
|
|
|
->setMethods(['getReflection']) |
111
|
|
|
->getMockForTrait(); |
112
|
|
|
|
113
|
|
|
$mock |
114
|
|
|
->expects($this->once()) |
115
|
|
|
->method('getReflection') |
116
|
|
|
->willReturn($reflectionMock); |
117
|
|
|
|
118
|
|
|
$this->assertSame('', $mock->getModuleName("SilverStripe\ORM\DataObject")); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Test that an unreadable composer file will return an empty array |
123
|
|
|
*/ |
124
|
|
|
public function testHandleUnreadableComposerFile() |
125
|
|
|
{ |
126
|
|
|
$filename = 'framework/composer.json'; |
127
|
|
|
chmod($filename, 0066); |
128
|
|
|
$this->assertSame([], $this->utility->getModuleComposerConfiguration('framework')); |
129
|
|
|
chmod($filename, 0644); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test that when composer.json is empty an empty array is returned |
134
|
|
|
*/ |
135
|
|
|
public function testEmptyComposerFile() |
136
|
|
|
{ |
137
|
|
|
mkdir('nothing'); |
138
|
|
|
touch('nothing/composer.json'); |
139
|
|
|
$this->assertSame([], $this->utility->getModuleComposerConfiguration('nothing')); |
140
|
|
|
unlink('nothing/composer.json'); |
141
|
|
|
rmdir('nothing'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Test that if no composer.json file exists, an empty array is returned |
146
|
|
|
*/ |
147
|
|
|
public function testHandleMissingComposerFile() |
148
|
|
|
{ |
149
|
|
|
$this->assertSame([], $this->utility->getModuleComposerConfiguration('bin')); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state