-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#20773: Make autoloader PSR-4 compliant
- Loading branch information
Showing
2 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
dev/tests/integration/testsuite/Magento/Framework/Code/Generator/AutoloaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Framework\Code\Generator; | ||
|
||
use Magento\Framework\Code\Generator; | ||
use Magento\Framework\Logger\Monolog as MagentoMonologLogger; | ||
use Magento\TestFramework\ObjectManager; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class AutoloaderTest extends TestCase | ||
{ | ||
/** | ||
* This method exists to fix the wrong return type hint on \Magento\Framework\App\ObjectManager::getInstance. | ||
* This way the IDE knows it's dealing with an instance of \Magento\TestFramework\ObjectManager and | ||
* not \Magento\Framework\App\ObjectManager. The former has the method addSharedInstance, the latter does not. | ||
* | ||
* @return ObjectManager|\Magento\Framework\App\ObjectManager | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
*/ | ||
private function getTestFrameworkObjectManager() | ||
{ | ||
return ObjectManager::getInstance(); | ||
} | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function setupLoggerTestDouble(): void | ||
{ | ||
$loggerTestDouble = $this->createMock(LoggerInterface::class); | ||
$this->getTestFrameworkObjectManager()->addSharedInstance($loggerTestDouble, MagentoMonologLogger::class); | ||
} | ||
|
||
/** | ||
* @after | ||
*/ | ||
public function removeLoggerTestDouble(): void | ||
{ | ||
$this->getTestFrameworkObjectManager()->removeSharedInstance(MagentoMonologLogger::class); | ||
} | ||
|
||
/** | ||
* @param \RuntimeException $testException | ||
* @return Generator|MockObject | ||
*/ | ||
private function createExceptionThrowingGeneratorTestDouble(\RuntimeException $testException) | ||
{ | ||
/** @var Generator|MockObject $generatorStub */ | ||
$generatorStub = $this->createMock(Generator::class); | ||
$generatorStub->method('generateClass')->willThrowException($testException); | ||
|
||
return $generatorStub; | ||
} | ||
|
||
public function testLogsExceptionDuringGeneration(): void | ||
{ | ||
$exceptionMessage = 'Test exception thrown during generation'; | ||
$testException = new \RuntimeException($exceptionMessage); | ||
|
||
$loggerMock = ObjectManager::getInstance()->get(LoggerInterface::class); | ||
$loggerMock->expects($this->once())->method('debug')->with($exceptionMessage, ['exception' => $testException]); | ||
|
||
$autoloader = new Autoloader($this->createExceptionThrowingGeneratorTestDouble($testException)); | ||
$this->assertNull($autoloader->load(NonExistingClassName::class)); | ||
} | ||
|
||
public function testFiltersDuplicateExceptionMessages(): void | ||
{ | ||
$exceptionMessage = 'Test exception thrown during generation'; | ||
$testException = new \RuntimeException($exceptionMessage); | ||
|
||
$loggerMock = ObjectManager::getInstance()->get(LoggerInterface::class); | ||
$loggerMock->expects($this->once())->method('debug')->with($exceptionMessage, ['exception' => $testException]); | ||
|
||
$autoloader = new Autoloader($this->createExceptionThrowingGeneratorTestDouble($testException)); | ||
$autoloader->load(OneNonExistingClassName::class); | ||
$autoloader->load(AnotherNonExistingClassName::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters