Skip to content

Commit 362e508

Browse files
committed
Add Interceptor Unit Tests
1 parent 214a677 commit 362e508

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Autoload;
14+
15+
/**
16+
* Dummy class that can be loaded via the Autoloader in the test cases.
17+
*/
18+
class Helper
19+
{
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Autoload;
14+
15+
/**
16+
* Dummy class that can be loaded via the Autoloader in the test cases.
17+
*/
18+
class HelperInterceptor
19+
{
20+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Autoload;
14+
15+
use bitExpert\PHPStan\Magento\Autoload\DataProvider\ClassLoaderProvider;
16+
use PHPStan\Cache\Cache;
17+
use PHPStan\Cache\CacheStorage;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class InterceptorAutoloaderUnitTest extends TestCase
22+
{
23+
private CacheStorage&MockObject $storage;
24+
private ClassLoaderProvider&MockObject $classLoader;
25+
private InterceptorAutoloader $autoloader;
26+
27+
public function setUp(): void
28+
{
29+
$this->storage = $this->createMock(CacheStorage::class);
30+
$this->classLoader = $this->createMock(ClassLoaderProvider::class);
31+
32+
$this->autoloader = new InterceptorAutoloader(new Cache($this->storage), $this->classLoader);
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function autoloaderIgnoresClassesWithoutInterceptorPostfix(): void
39+
{
40+
$this->classLoader->expects(self::never())
41+
->method('findFile');
42+
$this->storage->expects(self::never())
43+
->method('load');
44+
45+
$this->autoloader->autoload('SomeClass');
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function autoloaderPrefersLocalFile(): void
52+
{
53+
$this->classLoader->expects(self::once())
54+
->method('findFile')
55+
->willReturn(__DIR__ . '/HelperInterceptor.php');
56+
$this->storage->expects(self::never())
57+
->method('load');
58+
59+
$this->autoloader->autoload('\bitExpert\PHPStan\Magento\Autoload\Helper\Interceptor');
60+
61+
self::assertTrue(class_exists(HelperInterceptor::class, false));
62+
}
63+
64+
/**
65+
* @test
66+
*/
67+
public function autoloaderUsesCachedFileWhenFound(): void
68+
{
69+
$this->classLoader->expects(self::once())
70+
->method('findFile')
71+
->willReturn(false);
72+
$this->storage->expects(self::once())
73+
->method('load')
74+
->willReturn(__DIR__ . '/HelperInterceptor.php');
75+
76+
$this->autoloader->autoload('\bitExpert\PHPStan\Magento\Autoload\Helper\Interceptor');
77+
78+
self::assertTrue(class_exists(HelperInterceptor::class, false));
79+
}
80+
81+
/**
82+
* @test
83+
*/
84+
public function autoloaderGeneratesCacheFileWhenNotFoundInCache(): void
85+
{
86+
$this->classLoader->expects(self::once())
87+
->method('findFile')
88+
->willReturn(false);
89+
$this->storage->expects(self::atMost(2))
90+
->method('load')
91+
->willReturnOnConsecutiveCalls(null, __DIR__ . '/HelperInterceptor.php');
92+
$this->storage->expects(self::once())
93+
->method('save');
94+
95+
$this->autoloader->autoload('\bitExpert\PHPStan\Magento\Autoload\Helper\Interceptor');
96+
97+
self::assertTrue(class_exists(HelperInterceptor::class, false));
98+
}
99+
}

tests/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloaderUnitTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ public function autoloaderUsesCachedFileWhenFound(): void
9191
*/
9292
public function autoloaderGeneratesCacheFileWhenNotFoundInCache(): void
9393
{
94-
// little hack: the proxy autoloader will use Reflection to look for a class without the \Proxy prefix,
95-
// to avoid having another stub class file, we define an class alias here
96-
class_alias('\bitExpert\PHPStan\Magento\Autoload\HelperProxy', '\bitExpert\PHPStan\Magento\Autoload\Helper');
97-
9894
$this->classLoader->expects(self::once())
9995
->method('findFile')
10096
->willReturn(false);

0 commit comments

Comments
 (0)