Skip to content

Commit

Permalink
Change the way the GetById command is tested
Browse files Browse the repository at this point in the history
  • Loading branch information
coderimus committed Nov 12, 2019
1 parent d08eb44 commit b0fb363
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\Exception\IntegrationException;
use Magento\Framework\Exception\NoSuchEntityException;
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 PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Zend\Db\Adapter\Driver\Pdo\Statement;

/**
* Test the GetById command model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* Test the GetById command model with exception during media asset initialization
*/
class GetByIdTest extends TestCase
class GetByIdExceptionDuringMediaAssetInitializationTest extends \PHPUnit\Framework\TestCase
{
private const MEDIA_ASSET_STUB_ID = 1;

Expand Down Expand Up @@ -89,49 +85,6 @@ protected function setUp(): void
$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)
);
}

/**
* 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);
}

/**
* 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);
}

/**
* Test case when a problem occurred during asset initialization from received data.
*/
Expand Down
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);
}
}
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);
}
}
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)
);
}
}

0 comments on commit b0fb363

Please sign in to comment.