|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Okapi\Aop\Tests; |
| 4 | + |
| 5 | +use Okapi\Aop\Core\AutoloadInterceptor\ClassLoader; |
| 6 | +use Okapi\Aop\Core\Cache\CachePaths; |
| 7 | +use Okapi\CodeTransformer\Core\DI; |
| 8 | +use Okapi\CodeTransformer\Core\StreamFilter; |
| 9 | +use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector; |
| 10 | +use Okapi\Path\Path; |
| 11 | +use PHPUnit\Framework\Assert; |
| 12 | +use ReflectionProperty; |
| 13 | + |
| 14 | +trait ClassLoaderMockTrait |
| 15 | +{ |
| 16 | + private ?ClassLoader $classLoader = null; |
| 17 | + |
| 18 | + private function findClassMock(string $class): string |
| 19 | + { |
| 20 | + if (!isset($this->classLoader)) { |
| 21 | + $this->findClassLoader(); |
| 22 | + } |
| 23 | + |
| 24 | + return $this->classLoader->findFile($class); |
| 25 | + } |
| 26 | + |
| 27 | + private function findOriginalClassMock(string $class): string |
| 28 | + { |
| 29 | + if (!isset($this->classLoader)) { |
| 30 | + $this->findClassLoader(); |
| 31 | + } |
| 32 | + |
| 33 | + $original = new ReflectionProperty(ClassLoader::class, 'originalClassLoader'); |
| 34 | + $original = $original->getValue($this->classLoader); |
| 35 | + return $original->findFile($class); |
| 36 | + } |
| 37 | + |
| 38 | + private function findClassLoader(): void |
| 39 | + { |
| 40 | + foreach (spl_autoload_functions() as $function) { |
| 41 | + if (is_array($function) && $function[0] instanceof ClassLoader) { |
| 42 | + $this->classLoader = $function[0]; |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function assertWillBeWoven(string $className): void |
| 49 | + { |
| 50 | + $originalFilePath = Path::resolve($this->findOriginalClassMock($className)); |
| 51 | + |
| 52 | + $wovenPath = |
| 53 | + FilterInjector::PHP_FILTER_READ . |
| 54 | + StreamFilter::FILTER_ID . '/resource=' . |
| 55 | + $originalFilePath; |
| 56 | + |
| 57 | + $filePathMock = $this->findClassMock($className); |
| 58 | + |
| 59 | + Assert::assertEquals( |
| 60 | + $wovenPath, |
| 61 | + $filePathMock, |
| 62 | + "$className will not be woven", |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function assertAspectLoadedFromCache(string $className): void |
| 67 | + { |
| 68 | + $filePath = $this->findOriginalClassMock($className); |
| 69 | + $cachePaths = DI::get(CachePaths::class); |
| 70 | + $cachePath = $cachePaths->getProxyCachePath($filePath); |
| 71 | + $filePathMock = $this->findClassMock($className); |
| 72 | + |
| 73 | + Assert::assertEquals( |
| 74 | + $cachePath, |
| 75 | + $filePathMock, |
| 76 | + "$className will not be loaded from cache", |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function assertAspectNotApplied(string $className): void |
| 81 | + { |
| 82 | + $originalFilePath = Path::resolve($this->findOriginalClassMock($className)); |
| 83 | + $filePathMock = $this->findClassMock($className); |
| 84 | + |
| 85 | + Assert::assertEquals( |
| 86 | + $originalFilePath, |
| 87 | + $filePathMock, |
| 88 | + "$className will be woven", |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
0 commit comments