Completed
Push — master ( 6a6c80...a2c1e8 )
by Marco
79:00 queued 52:38
created

ProxyManagerTest/Autoloader/AutoloaderTest.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
class AutoloaderTest extends TestCase
28
{
29
    /** @var Autoloader */
30
    protected $autoloader;
31
32
    /** @var FileLocatorInterface|MockObject */
33
    protected $fileLocator;
34
35
    /** @var ClassNameInflectorInterface|MockObject */
36
    protected $classNameInflector;
37
38
    /**
39
     * @covers \ProxyManager\Autoloader\Autoloader::__construct
40
     */
41
    protected function setUp() : void
42
    {
43
        $this->fileLocator        = $this->createMock(FileLocatorInterface::class);
44
        $this->classNameInflector = $this->createMock(ClassNameInflectorInterface::class);
45
        $this->autoloader         = new Autoloader($this->fileLocator, $this->classNameInflector);
46
    }
47
48
    /**
49
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
50
     */
51
    public function testWillNotAutoloadUserClasses() : void
52
    {
53
        $className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar');
54
        $this
55
            ->classNameInflector
56
            ->expects(self::once())
57
            ->method('isProxyClassName')
58
            ->with($className)
59
            ->will(self::returnValue(false));
60
61
        self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className));
62
    }
63
64
    /**
65
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
66
     */
67
    public function testWillNotAutoloadNonExistingClass() : void
68
    {
69
        $className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar');
70
        $this
71
            ->classNameInflector
72
            ->expects(self::once())
73
            ->method('isProxyClassName')
74
            ->with($className)
75
            ->will(self::returnValue(true));
76
        $this
0 ignored issues
show
The method expects() does not seem to exist on object<ProxyManager\File...r\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            ->fileLocator
78
            ->expects(self::once())
79
            ->method('getProxyFileName')
80
            ->will(self::returnValue(__DIR__ . '/non-existing'));
81
82
        self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className));
83
    }
84
85
    /**
86
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
87
     */
88
    public function testWillNotAutoloadExistingClass() : void
89
    {
90
        self::assertFalse($this->autoloadWithoutFurtherAutoloaders(self::class));
91
    }
92
93
    /**
94
     * @covers \ProxyManager\Autoloader\Autoloader::__invoke
95
     */
96
    public function testWillAutoloadExistingFile() : void
97
    {
98
        $namespace = 'Foo';
99
        $className = UniqueIdentifierGenerator::getIdentifier('Bar');
100
        $fqcn      = $namespace . '\\' . $className;
101
        $fileName  = sys_get_temp_dir() . '/foo_' . uniqid() . '.php';
102
103
        file_put_contents($fileName, '<?php namespace ' . $namespace . '; class ' . $className . '{}');
104
105
        $this
106
            ->classNameInflector
107
            ->expects(self::once())
108
            ->method('isProxyClassName')
109
            ->with($fqcn)
110
            ->will(self::returnValue(true));
111
        $this
0 ignored issues
show
The method expects() does not seem to exist on object<ProxyManager\File...r\FileLocatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
            ->fileLocator
113
            ->expects(self::once())
114
            ->method('getProxyFileName')
115
            ->will(self::returnValue($fileName));
116
117
        self::assertTrue($this->autoloadWithoutFurtherAutoloaders($fqcn));
118
        self::assertTrue(class_exists($fqcn, false));
119
    }
120
121
    private function autoloadWithoutFurtherAutoloaders(string $className) : bool
122
    {
123
        $failingAutoloader = null;
124
        $failingAutoloader = function (string $className) use (& $failingAutoloader) : void {
125
            spl_autoload_unregister($failingAutoloader);
126
127
            $this->fail(sprintf('Fallback autoloading was triggered to load "%s"', $className));
128
        };
129
130
        spl_autoload_register($failingAutoloader);
131
132
        $result = $this->autoloader->__invoke($className);
133
134
        spl_autoload_unregister($failingAutoloader);
135
136
        return $result;
137
    }
138
}
139