Completed
Pull Request — master (#32)
by Robbie
02:14
created

testGetModuleNameFromComposerConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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;
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...
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 an unreadable composer file will return an empty array
86
     */
87
    public function testHandleUnreadableComposerFile()
88
    {
89
        $filename = 'framework/composer.json';
90
        chmod($filename, 0066);
91
        $this->assertSame([], $this->utility->getModuleComposerConfiguration('framework'));
92
        chmod($filename, 0644);
93
    }
94
95
    /**
96
     * Test that if no composer.json file exists, an empty array is returned
97
     */
98
    public function testHandleMissingComposerFile()
99
    {
100
        $this->assertSame([], $this->utility->getModuleComposerConfiguration('bin'));
101
    }
102
}
103