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