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
+ }
0 commit comments