Completed
Pull Request — master (#453)
by Marco
220:48 queued 199:45
created

AutoloaderTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 112
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerTest\Autoloader;
6
7
use PHPUnit\Framework\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use ProxyManager\Autoloader\Autoloader;
10
use ProxyManager\FileLocator\FileLocatorInterface;
11
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
12
use ProxyManager\Inflector\ClassNameInflectorInterface;
13
use function class_exists;
14
use function file_put_contents;
15
use function spl_autoload_register;
16
use function spl_autoload_unregister;
17
use function sprintf;
18
use function sys_get_temp_dir;
19
use function uniqid;
20
21
/**
22
 * Tests for {@see \ProxyManager\Autoloader\Autoloader}
23
 *
24
 * @covers \ProxyManager\Autoloader\Autoloader
25
 * @group Coverage
26
 */
27
final class AutoloaderTest extends TestCase
28
{
29
    private Autoloader $autoloader;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
30
31
    /** @var FileLocatorInterface&MockObject */
32
    private FileLocatorInterface $fileLocator;
33
34
    /** @var ClassNameInflectorInterface&MockObject */
35
    private ClassNameInflectorInterface $classNameInflector;
36
37
    /**
38
     * @covers \ProxyManager\Autoloader\Autoloader::__construct
39
     */
40
    protected function setUp() : void
41
    {
42
        $this->fileLocator        = $this->createMock(FileLocatorInterface::class);
43
        $this->classNameInflector = $this->createMock(ClassNameInflectorInterface::class);
44
        $this->autoloader         = new Autoloader($this->fileLocator, $this->classNameInflector);
45
    }
46
47
    /**
48
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
49
     */
50
    public function testWillNotAutoloadUserClasses() : void
51
    {
52
        $className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar');
53
        $this
54
            ->classNameInflector
55
            ->expects(self::once())
56
            ->method('isProxyClassName')
57
            ->with($className)
58
            ->willReturn(false);
59
60
        self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className));
61
    }
62
63
    /**
64
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
65
     */
66
    public function testWillNotAutoloadNonExistingClass() : void
67
    {
68
        $className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar');
69
        $this
70
            ->classNameInflector
71
            ->expects(self::once())
72
            ->method('isProxyClassName')
73
            ->with($className)
74
            ->willReturn(true);
75
        $this
76
            ->fileLocator
77
            ->expects(self::once())
78
            ->method('getProxyFileName')
79
            ->willReturn(__DIR__ . '/non-existing');
80
81
        self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className));
82
    }
83
84
    /**
85
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
86
     */
87
    public function testWillNotAutoloadExistingClass() : void
88
    {
89
        self::assertFalse($this->autoloadWithoutFurtherAutoloaders(self::class));
90
    }
91
92
    /**
93
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
94
     */
95
    public function testWillAutoloadExistingFile() : void
96
    {
97
        $namespace = 'Foo';
98
        $className = UniqueIdentifierGenerator::getIdentifier('Bar');
99
        $fqcn      = $namespace . '\\' . $className;
100
        $fileName  = sys_get_temp_dir() . '/foo_' . uniqid('file', true) . '.php';
101
102
        file_put_contents($fileName, '<?php namespace ' . $namespace . '; class ' . $className . '{}');
103
104
        $this
105
            ->classNameInflector
106
            ->expects(self::once())
107
            ->method('isProxyClassName')
108
            ->with($fqcn)
109
            ->willReturn(true);
110
        $this
111
            ->fileLocator
112
            ->expects(self::once())
113
            ->method('getProxyFileName')
114
            ->willReturn($fileName);
115
116
        self::assertTrue($this->autoloadWithoutFurtherAutoloaders($fqcn));
117
        self::assertTrue(class_exists($fqcn, false));
118
    }
119
120
    private function autoloadWithoutFurtherAutoloaders(string $className) : bool
121
    {
122
        $failingAutoloader = null;
123
        $failingAutoloader = function (string $className) use (& $failingAutoloader) : void {
124
            spl_autoload_unregister($failingAutoloader);
125
126
            $this->fail(sprintf('Fallback autoloading was triggered to load "%s"', $className));
127
        };
128
129
        spl_autoload_register($failingAutoloader);
130
131
        $result = $this->autoloader->__invoke($className);
132
133
        spl_autoload_unregister($failingAutoloader);
134
135
        return $result;
136
    }
137
}
138