Test Setup Failed
Push — master ( aa9039...94c501 )
by Derek
03:32
created

testIncludeClassReturnsFalseWhenNoPackageSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
namespace Subreality\Dilmun\Tests\Nabu;
3
4
use Subreality\Dilmun\Nabu\Autoloader;
5
use Subreality\Dilmun\Nabu\LoggerHandler\File;
6
use Subreality\Dilmun\Tests\TestClass;
7
8
/**
9
 * Class AutoloaderTest
10
 * @package Subreality\Dilmun\Tests\Nabu
11
 */
12
class AutoloaderTest extends \PHPUnit_Framework_TestCase
13
{
14
    public static $autoloader_log_location;
15
16
    public static function setUpBeforeClass()
17
    {
18
        self::$autoloader_log_location = DILMUN_LOGS_DIR . "autoloader.log";
19
20
        //Creating a pointed to the existing log file.
21
        $autoloader_file = new File("autoloader.log", DILMUN_LOGS_DIR);
22
        $autoloader_file->initialize();
23
24
        //Need to clear the log for valid log writing tests
25
        $autoloader_file->clearFile();
26
    }
27
28
    protected function assertPreconditions()
29
    {
30
    }
31
32
    protected function setUp()
33
    {
34
        $autoloader_array = $this->getAutoloaderFromAutoloadStack();
35
36
        if (!$autoloader_array) {
37
            $loader = new Autoloader();
38
39
            $loader->register();
40
41
            $loader->registerPackages("Subreality\\Dilmun", DILMUN_SOURCE_DIR);
42
            $loader->registerPackages("Subreality\\Dilmun\\Tests", DILMUN_TEST_DIR);
43
        } else {
44
            //Ensuring that packages are registered in the existing autoloader queued in the autoload stack since
45
            //we're mucking about with the stack!
46
47
            $autoloader = $autoloader_array[0];
48
49
            /** @var Autoloader $autoloader */
50
            $autoloader->registerPackages("Subreality\\Dilmun", DILMUN_SOURCE_DIR);
51
            $autoloader->registerPackages("Subreality\\Dilmun\\Tests", DILMUN_TEST_DIR);
52
        }
53
    }
54
55
    protected function tearDown()
56
    {
57
    }
58
59
    public static function tearDownAfterClass()
60
    {
61
    }
62
63
    public function testRegisterRegistersIncludeClassMethod()
64
    {
65
        $autoloader_array = $this->getAutoloaderFromAutoloadStack();
66
67
        if ($autoloader_array) {
68
            spl_autoload_unregister($autoloader_array);
69
        }
70
71
        $this->assertFalse($this->autoloaderClassExists());
72
73
        $new_autoloader = new Autoloader();
74
75
        $new_autoloader->register();
76
77
        $autoloader_exists = $this->autoloaderClassExists();
78
79
        $this->assertTrue($autoloader_exists);
80
81
        $autoloader_array = $this->getAutoloaderFromAutoloadStack();
82
83
        $registered_autoload_method = $autoloader_array[1];
84
85
        $this->assertEquals("includeClass", $registered_autoload_method);
86
    }
87
88
    public function testAutoloaderLogsNewLoadedClass()
89
    {
90
        //We're not doing anything with this; it's an arbitrary instantiation to get something written to the log.
91
        /** @noinspection PhpUnusedLocalVariableInspection */
92
        $stub = new TestClass();
0 ignored issues
show
Unused Code introduced by
$stub is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
94
        $autoloader_log = file_get_contents(DILMUN_LOGS_DIR . "autoloader.log");
95
96
        $this->assertContains("TestClass", $autoloader_log);
97
    }
98
99
    public function testIncludeClassReturnsFalseWhenClassInstantiatedWithoutCorrespondingClassFile()
100
    {
101
        $fake_class_exists = class_exists('Subreality\Dilmun\Nabu\Xkcd');
102
103
        $this->assertFalse($fake_class_exists);
104
    }
105
106
    public function testIncludeClassReturnsFalseWhenNoPackageSet()
107
    {
108
        $autoloader_array = $this->getAutoloaderFromAutoloadStack();
109
110
        if ($autoloader_array) {
111
            spl_autoload_unregister($autoloader_array);
112
        }
113
114
        $this->assertFalse($this->autoloaderClassExists());
115
116
        $new_autoloader = new Autoloader();
117
118
        $new_autoloader->register();
119
120
        $autoloader_exists = $this->autoloaderClassExists();
121
122
        $this->assertTrue($autoloader_exists);
123
124
        $fake_class_exists = class_exists('Subreality\Dilmun\Nabu\Xkcd');
125
126
        $this->assertFalse($fake_class_exists);
127
    }
128
129
    /**
130
     * @return bool
131
     */
132 View Code Duplication
    private function autoloaderClassExists()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $autoload_functions = spl_autoload_functions();
135
136
        $autoloader_class_exists = false;
137
138
        if ($autoload_functions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $autoload_functions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
139
            foreach ($autoload_functions as $function) {
140
                if ($function[0] instanceof Autoloader) {
141
                    $autoloader_class_exists = true;
142
                }
143
            }
144
        }
145
146
        return $autoloader_class_exists;
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152 View Code Duplication
    private function getAutoloaderFromAutoloadStack()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $autoloader_object = false;
155
156
        $autoload_functions = spl_autoload_functions();
157
158
        if ($autoload_functions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $autoload_functions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
159
            foreach ($autoload_functions as $function_array) {
160
                if ($function_array[0] instanceof Autoloader) {
161
                    $autoloader_object = $function_array;
162
                }
163
            }
164
        }
165
166
        return $autoloader_object;
167
    }
168
}
169