Skip to content

Fix exception handling and cover with unit tests the GetById media asset command #25547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions app/code/Magento/MediaGallery/Model/Asset/Command/GetById.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace Magento\MediaGallery\Model\Asset\Command;

use Magento\MediaGalleryApi\Api\Data\AssetInterface;
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Magento\MediaGalleryApi\Model\Asset\Command\GetByIdInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\IntegrationException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Magento\MediaGalleryApi\Model\Asset\Command\GetByIdInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -66,24 +66,34 @@ public function __construct(
public function execute(int $mediaAssetId): AssetInterface
{
try {
$mediaAssetTable = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET);
$connection = $this->resourceConnection->getConnection();
$select = $connection->select()
->from(['amg' => $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET)])
->from(['amg' => $mediaAssetTable])
->where('amg.id = ?', $mediaAssetId);
$data = $connection->query($select)->fetch();
$mediaAssetData = $connection->query($select)->fetch();
} catch (\Exception $exception) {
$message = __(
'En error occurred during get media asset data by id %id: %error',
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
);
$this->logger->critical($message);
throw new IntegrationException($message, $exception);
}

if (empty($data)) {
$message = __('There is no such media asset with id "%1"', $mediaAssetId);
throw new NoSuchEntityException($message);
}
if (empty($mediaAssetData)) {
$message = __('There is no such media asset with id "%1"', $mediaAssetId);
throw new NoSuchEntityException($message);
}

return $this->assetFactory->create(['data' => $data]);
try {
return $this->assetFactory->create(['data' => $mediaAssetData]);
} catch (\Exception $exception) {
$this->logger->critical($exception->getMessage());
$message = __(
'En error occurred during get media asset with id %id by id: %error',
'En error occurred during initialize media asset with id %id: %error',
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
);
$this->logger->critical($message);
throw new IntegrationException($message, $exception);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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 media asset initialization
*/
class GetByIdExceptionDuringMediaAssetInitializationTest 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 a problem occurred during asset initialization from received data.
*/
public function testErrorDuringMediaAssetInitializationException(): void
{
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
$this->adapter->method('query')->willReturn($this->statementMock);

$this->assetFactory->expects($this->once())->method('create')->willThrowException(new \Exception());

$this->expectException(IntegrationException::class);
$this->logger->expects($this->any())
->method('critical')
->willReturnSelf();

$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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;

/**
* @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 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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 LoggerInterface|MockObject
*/
private $logger;

/**
* @var Statement|MockObject
*/
private $statementMock;

/**
* Initialize basic test class mocks
*/
protected function setUp(): void
{
$resourceConnection = $this->createMock(ResourceConnection::class);
$assetFactory = $this->createMock(AssetInterfaceFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->getMediaAssetById = (new ObjectManager($this))->getObject(
GetById::class,
[
'resourceConnection' => $resourceConnection,
'assetFactory' => $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 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->logger->expects($this->any())
->method('critical')
->willReturnSelf();

$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
}
}
Loading