-
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.
Change the way the GetById command is tested
- Loading branch information
Showing
4 changed files
with
290 additions
and
49 deletions.
There are no files selected for viewing
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
100 changes: 100 additions & 0 deletions
100
...e/Magento/MediaGallery/Test/Unit/Model/Asset/Command/GetByIdExceptionNoSuchEntityTest.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,100 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\DB\Select; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\MediaGallery\Model\Asset\Command\GetById; | ||
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Psr\Log\LoggerInterface; | ||
use Zend\Db\Adapter\Driver\Pdo\Statement; | ||
|
||
/** | ||
* Test the GetById command with exception thrown in case when there is no such entity | ||
*/ | ||
class GetByIdExceptionNoSuchEntityTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
private const MEDIA_ASSET_STUB_ID = 1; | ||
|
||
private const MEDIA_ASSET_DATA = ['id' => 1]; | ||
|
||
/** | ||
* @var GetById|MockObject | ||
*/ | ||
private $getMediaAssetById; | ||
|
||
/** | ||
* @var AssetInterfaceFactory|MockObject | ||
*/ | ||
private $assetFactory; | ||
|
||
/** | ||
* @var AdapterInterface|MockObject | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* @var Select|MockObject | ||
*/ | ||
private $selectStub; | ||
|
||
/** | ||
* @var Statement|MockObject | ||
*/ | ||
private $statementMock; | ||
|
||
/** | ||
* @var LoggerInterface|MockObject | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* Initialize basic test class mocks | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$resourceConnection = $this->createMock(ResourceConnection::class); | ||
$this->assetFactory = $this->createMock(AssetInterfaceFactory::class); | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
|
||
$this->getMediaAssetById = (new ObjectManager($this))->getObject( | ||
GetById::class, | ||
[ | ||
'resourceConnection' => $resourceConnection, | ||
'assetFactory' => $this->assetFactory, | ||
'logger' => $this->logger, | ||
] | ||
); | ||
$this->adapter = $this->createMock(AdapterInterface::class); | ||
$resourceConnection->method('getConnection')->willReturn($this->adapter); | ||
|
||
$this->selectStub = $this->createMock(Select::class); | ||
$this->selectStub->method('from')->willReturnSelf(); | ||
$this->selectStub->method('where')->willReturnSelf(); | ||
$this->adapter->method('select')->willReturn($this->selectStub); | ||
|
||
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock(); | ||
} | ||
|
||
/** | ||
* Test case when there is no found media asset by id. | ||
*/ | ||
public function testNotFoundMediaAssetException(): void | ||
{ | ||
$this->statementMock->method('fetch')->willReturn([]); | ||
$this->adapter->method('query')->willReturn($this->statementMock); | ||
|
||
$this->expectException(NoSuchEntityException::class); | ||
|
||
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...code/Magento/MediaGallery/Test/Unit/Model/Asset/Command/GetByIdExceptionOnGetDataTest.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,89 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\DB\Select; | ||
use Magento\Framework\Exception\IntegrationException; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\MediaGallery\Model\Asset\Command\GetById; | ||
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Psr\Log\LoggerInterface; | ||
use Zend\Db\Adapter\Driver\Pdo\Statement; | ||
|
||
/** | ||
* Test the GetById command with exception during get media data | ||
*/ | ||
class GetByIdExceptionOnGetDataTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
private const MEDIA_ASSET_STUB_ID = 1; | ||
|
||
private const MEDIA_ASSET_DATA = ['id' => 1]; | ||
|
||
/** | ||
* @var GetById|MockObject | ||
*/ | ||
private $getMediaAssetById; | ||
/** | ||
* @var AdapterInterface|MockObject | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* @var Select|MockObject | ||
*/ | ||
private $selectStub; | ||
|
||
/** | ||
* @var Statement|MockObject | ||
*/ | ||
private $statementMock; | ||
|
||
/** | ||
* Initialize basic test class mocks | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$resourceConnection = $this->createMock(ResourceConnection::class); | ||
$assetFactory = $this->createMock(AssetInterfaceFactory::class); | ||
$logger = $this->createMock(LoggerInterface::class); | ||
|
||
$this->getMediaAssetById = (new ObjectManager($this))->getObject( | ||
GetById::class, | ||
[ | ||
'resourceConnection' => $resourceConnection, | ||
'assetFactory' => $assetFactory, | ||
'logger' => $logger, | ||
] | ||
); | ||
$this->adapter = $this->createMock(AdapterInterface::class); | ||
$resourceConnection->method('getConnection')->willReturn($this->adapter); | ||
|
||
$this->selectStub = $this->createMock(Select::class); | ||
$this->selectStub->method('from')->willReturnSelf(); | ||
$this->selectStub->method('where')->willReturnSelf(); | ||
$this->adapter->method('select')->willReturn($this->selectStub); | ||
|
||
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock(); | ||
} | ||
|
||
/** | ||
* Test an exception during the get asset data query. | ||
*/ | ||
public function testExceptionDuringGetMediaAssetData(): void | ||
{ | ||
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA); | ||
$this->adapter->method('query')->willThrowException(new \Exception()); | ||
|
||
$this->expectException(IntegrationException::class); | ||
|
||
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
app/code/Magento/MediaGallery/Test/Unit/Model/Asset/Command/GetByIdSuccessfulTest.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,99 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\DB\Select; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\MediaGallery\Model\Asset\Command\GetById; | ||
use Magento\MediaGalleryApi\Api\Data\AssetInterface; | ||
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Psr\Log\LoggerInterface; | ||
use Zend\Db\Adapter\Driver\Pdo\Statement; | ||
|
||
/** | ||
* Test the GetById command model | ||
*/ | ||
class GetByIdTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
private const MEDIA_ASSET_STUB_ID = 1; | ||
|
||
private const MEDIA_ASSET_DATA = ['id' => 1]; | ||
|
||
/** | ||
* @var GetById|MockObject | ||
*/ | ||
private $getMediaAssetById; | ||
|
||
/** | ||
* @var AssetInterfaceFactory|MockObject | ||
*/ | ||
private $assetFactory; | ||
|
||
/** | ||
* @var AdapterInterface|MockObject | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* @var Select|MockObject | ||
*/ | ||
private $selectStub; | ||
|
||
/** | ||
* @var Statement|MockObject | ||
*/ | ||
private $statementMock; | ||
|
||
/** | ||
* Initialize basic test class mocks | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$resourceConnection = $this->createMock(ResourceConnection::class); | ||
$this->assetFactory = $this->createMock(AssetInterfaceFactory::class); | ||
$logger = $this->createMock(LoggerInterface::class); | ||
|
||
$this->getMediaAssetById = (new ObjectManager($this))->getObject( | ||
GetById::class, | ||
[ | ||
'resourceConnection' => $resourceConnection, | ||
'assetFactory' => $this->assetFactory, | ||
'logger' => $logger, | ||
] | ||
); | ||
$this->adapter = $this->createMock(AdapterInterface::class); | ||
$resourceConnection->method('getConnection')->willReturn($this->adapter); | ||
|
||
$this->selectStub = $this->createMock(Select::class); | ||
$this->selectStub->method('from')->willReturnSelf(); | ||
$this->selectStub->method('where')->willReturnSelf(); | ||
$this->adapter->method('select')->willReturn($this->selectStub); | ||
|
||
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock(); | ||
} | ||
|
||
/** | ||
* Test successful get media asset by id command execution. | ||
*/ | ||
public function testSuccessfulGetByIdExecution(): void | ||
{ | ||
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA); | ||
$this->adapter->method('query')->willReturn($this->statementMock); | ||
|
||
$mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock(); | ||
$this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub); | ||
|
||
$this->assertEquals( | ||
$mediaAssetStub, | ||
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID) | ||
); | ||
} | ||
} |