Completed
Push — master ( 597ac5...b01469 )
by Robbie
9s
created

ObjectUtilitiesTest::testGetReflectionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Tests\Framework\Utility;
4
5
use SilverLeague\Console\Framework\Scaffold;
6
use SilverLeague\Console\Framework\Utility\ObjectUtilities;
7
use ReflectionClass;
8
9
/**
10
 * @coverDefaultClass \SilverLeague\Console\Framework\Utility\ObjectUtilities
11
 * @package silverstripe-console
12
 * @author  Robbie Averill <[email protected]>
13
 */
14
class ObjectUtilitiesTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function setUp()
20
    {
21
        parent::setUp();
22
23
        $this->utility = $this
24
            ->getMockBuilder(ObjectUtilities::class)
25
            ->setMethods([])
26
            ->getMockForTrait();
27
28
        // Trigger bootstrapping
29
        (new Scaffold);
30
    }
31
32
    /**
33
     * Test that a ReflectionClass is returned when a valid class name is provided
34
     */
35
    public function testGetReflectionClass()
36
    {
37
        $this->assertInstanceOf(ReflectionClass::class, $this->utility->getReflection("SilverStripe\Core\Object"));
38
    }
39
40
    /**
41
     * Test that ReflectionExceptions are handled gracefully for non existent classes
42
     */
43
    public function testGetReflectionHandlesMissingClassesGracefully()
44
    {
45
        $this->assertFalse($this->utility->getReflection("Foo\Bar\Monkey\Man\Pluto\Saturn"));
46
    }
47
48
    /**
49
     * Test that a composer configured module name is returned for a valid SilverStripe module
50
     */
51
    public function testGetModuleNameForValidClass()
52
    {
53
        $this->assertSame('silverstripe/framework', $this->utility->getModuleName("SilverStripe\Core\Object"));
54
    }
55
56
    /**
57
     * Test that an empty string is returned gracefully if a module cannot be found
58
     */
59
    public function testReturnEmptyStringWhenModuleNameCantBeFound()
60
    {
61
        $this->assertSame('', $this->utility->getModuleName("Monolog\Logger"));
62
    }
63
64
    /**
65
     * Ensure that the project name can be retrieved from its composer configuration, and if it's the $project (mysite)
66
     * then use the root directory composer.json
67
     *
68
     * @dataProvider composerConfigurationProvider
69
     */
70
    public function testGetModuleNameFromComposerConfiguration($projectName, $folderName, $expected)
71
    {
72
        global $project;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
73
        $project = $projectName;
74
        $this->assertSame($expected, $this->utility->getModuleComposerConfiguration($folderName)['name']);
75
    }
76
77
    /**
78
     * @return array[]
79
     */
80
    public function composerConfigurationProvider()
81
    {
82
        return [
83
            ['mysite', 'mysite', 'silverleague/console'],
84
            ['mysite', 'framework', 'silverstripe/framework']
85
        ];
86
    }
87
88
    /**
89
     * Test that when an invalid class is passed to getModuleName it will return a blank string
90
     */
91
    public function testGetModuleNameFromInvalidClassReturnsEmptyString()
92
    {
93
        $this->assertSame('', $this->utility->getModuleName("Far\Out\There\Nowhere"));
94
    }
95
96
    /**
97
     * Test that when the module's folder name is empty or missing, a blank string is returned
98
     */
99
    public function testEmptyFolderNameInPathReturnsEmptyString()
100
    {
101
        $reflectionMock = $this
102
            ->getMockBuilder(ReflectionClass::class)
103
            ->disableOriginalConstructor()
104
            ->setMethods(['getFileName'])
105
            ->getMock();
106
107
        $reflectionMock
108
            ->expects($this->once())
109
            ->method('getFileName')
110
            ->willReturn(SILVERSTRIPE_ROOT_DIR . '/0/src/ORM/DataObject.php');
111
112
        $mock = $this
113
            ->getMockBuilder(ObjectUtilities::class)
114
            ->setMethods(['getReflection'])
115
            ->getMockForTrait();
116
117
        $mock
118
            ->expects($this->once())
119
            ->method('getReflection')
120
            ->willReturn($reflectionMock);
121
122
        $this->assertSame('', $mock->getModuleName("SilverStripe\ORM\DataObject"));
123
    }
124
125
    /**
126
     * Test that an unreadable composer file will return an empty array
127
     */
128
    public function testHandleUnreadableComposerFile()
129
    {
130
        $filename = 'framework/composer.json';
131
        chmod($filename, 0066);
132
        $this->assertSame([], $this->utility->getModuleComposerConfiguration('framework'));
133
        chmod($filename, 0644);
134
    }
135
136
    /**
137
     * Test that when composer.json is empty an empty array is returned
138
     */
139
    public function testEmptyComposerFile()
140
    {
141
        mkdir('nothing');
142
        touch('nothing/composer.json');
143
        $this->assertSame([], $this->utility->getModuleComposerConfiguration('nothing'));
144
        unlink('nothing/composer.json');
145
        rmdir('nothing');
146
    }
147
148
    /**
149
     * Test that if no composer.json file exists, an empty array is returned
150
     */
151
    public function testHandleMissingComposerFile()
152
    {
153
        $this->assertSame([], $this->utility->getModuleComposerConfiguration('bin'));
154
    }
155
}
156