Skip to content

Commit b0fb363

Browse files
committed
Change the way the GetById command is tested
1 parent d08eb44 commit b0fb363

File tree

4 files changed

+290
-49
lines changed

4 files changed

+290
-49
lines changed

app/code/Magento/MediaGallery/Test/Unit/Model/Asset/Command/GetByIdTest.php renamed to app/code/Magento/MediaGallery/Test/Unit/Model/Asset/Command/GetByIdExceptionDuringMediaAssetInitializationTest.php

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
1212
use Magento\Framework\DB\Select;
1313
use Magento\Framework\Exception\IntegrationException;
14-
use Magento\Framework\Exception\NoSuchEntityException;
1514
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1615
use Magento\MediaGallery\Model\Asset\Command\GetById;
17-
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1816
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
1917
use PHPUnit\Framework\MockObject\MockObject;
20-
use PHPUnit\Framework\TestCase;
2118
use Psr\Log\LoggerInterface;
2219
use Zend\Db\Adapter\Driver\Pdo\Statement;
2320

2421
/**
25-
* Test the GetById command model
26-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
* Test the GetById command model with exception during media asset initialization
2723
*/
28-
class GetByIdTest extends TestCase
24+
class GetByIdExceptionDuringMediaAssetInitializationTest extends \PHPUnit\Framework\TestCase
2925
{
3026
private const MEDIA_ASSET_STUB_ID = 1;
3127

@@ -89,49 +85,6 @@ protected function setUp(): void
8985
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
9086
}
9187

92-
/**
93-
* Test successful get media asset by id command execution.
94-
*/
95-
public function testSuccessfulGetByIdExecution(): void
96-
{
97-
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
98-
$this->adapter->method('query')->willReturn($this->statementMock);
99-
100-
$mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock();
101-
$this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub);
102-
103-
$this->assertEquals(
104-
$mediaAssetStub,
105-
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID)
106-
);
107-
}
108-
109-
/**
110-
* Test an exception during the get asset data query.
111-
*/
112-
public function testExceptionDuringGetMediaAssetData(): void
113-
{
114-
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
115-
$this->adapter->method('query')->willThrowException(new \Exception());
116-
117-
$this->expectException(IntegrationException::class);
118-
119-
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
120-
}
121-
122-
/**
123-
* Test case when there is no found media asset by id.
124-
*/
125-
public function testNotFoundMediaAssetException(): void
126-
{
127-
$this->statementMock->method('fetch')->willReturn([]);
128-
$this->adapter->method('query')->willReturn($this->statementMock);
129-
130-
$this->expectException(NoSuchEntityException::class);
131-
132-
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
133-
}
134-
13588
/**
13689
* Test case when a problem occurred during asset initialization from received data.
13790
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\MediaGallery\Model\Asset\Command\GetById;
16+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use Psr\Log\LoggerInterface;
19+
use Zend\Db\Adapter\Driver\Pdo\Statement;
20+
21+
/**
22+
* Test the GetById command with exception thrown in case when there is no such entity
23+
*/
24+
class GetByIdExceptionNoSuchEntityTest extends \PHPUnit\Framework\TestCase
25+
{
26+
private const MEDIA_ASSET_STUB_ID = 1;
27+
28+
private const MEDIA_ASSET_DATA = ['id' => 1];
29+
30+
/**
31+
* @var GetById|MockObject
32+
*/
33+
private $getMediaAssetById;
34+
35+
/**
36+
* @var AssetInterfaceFactory|MockObject
37+
*/
38+
private $assetFactory;
39+
40+
/**
41+
* @var AdapterInterface|MockObject
42+
*/
43+
private $adapter;
44+
45+
/**
46+
* @var Select|MockObject
47+
*/
48+
private $selectStub;
49+
50+
/**
51+
* @var Statement|MockObject
52+
*/
53+
private $statementMock;
54+
55+
/**
56+
* @var LoggerInterface|MockObject
57+
*/
58+
private $logger;
59+
60+
/**
61+
* Initialize basic test class mocks
62+
*/
63+
protected function setUp(): void
64+
{
65+
$resourceConnection = $this->createMock(ResourceConnection::class);
66+
$this->assetFactory = $this->createMock(AssetInterfaceFactory::class);
67+
$this->logger = $this->createMock(LoggerInterface::class);
68+
69+
$this->getMediaAssetById = (new ObjectManager($this))->getObject(
70+
GetById::class,
71+
[
72+
'resourceConnection' => $resourceConnection,
73+
'assetFactory' => $this->assetFactory,
74+
'logger' => $this->logger,
75+
]
76+
);
77+
$this->adapter = $this->createMock(AdapterInterface::class);
78+
$resourceConnection->method('getConnection')->willReturn($this->adapter);
79+
80+
$this->selectStub = $this->createMock(Select::class);
81+
$this->selectStub->method('from')->willReturnSelf();
82+
$this->selectStub->method('where')->willReturnSelf();
83+
$this->adapter->method('select')->willReturn($this->selectStub);
84+
85+
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
86+
}
87+
88+
/**
89+
* Test case when there is no found media asset by id.
90+
*/
91+
public function testNotFoundMediaAssetException(): void
92+
{
93+
$this->statementMock->method('fetch')->willReturn([]);
94+
$this->adapter->method('query')->willReturn($this->statementMock);
95+
96+
$this->expectException(NoSuchEntityException::class);
97+
98+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
99+
}
100+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
13+
use Magento\Framework\Exception\IntegrationException;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\MediaGallery\Model\Asset\Command\GetById;
16+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use Psr\Log\LoggerInterface;
19+
use Zend\Db\Adapter\Driver\Pdo\Statement;
20+
21+
/**
22+
* Test the GetById command with exception during get media data
23+
*/
24+
class GetByIdExceptionOnGetDataTest extends \PHPUnit\Framework\TestCase
25+
{
26+
private const MEDIA_ASSET_STUB_ID = 1;
27+
28+
private const MEDIA_ASSET_DATA = ['id' => 1];
29+
30+
/**
31+
* @var GetById|MockObject
32+
*/
33+
private $getMediaAssetById;
34+
/**
35+
* @var AdapterInterface|MockObject
36+
*/
37+
private $adapter;
38+
39+
/**
40+
* @var Select|MockObject
41+
*/
42+
private $selectStub;
43+
44+
/**
45+
* @var Statement|MockObject
46+
*/
47+
private $statementMock;
48+
49+
/**
50+
* Initialize basic test class mocks
51+
*/
52+
protected function setUp(): void
53+
{
54+
$resourceConnection = $this->createMock(ResourceConnection::class);
55+
$assetFactory = $this->createMock(AssetInterfaceFactory::class);
56+
$logger = $this->createMock(LoggerInterface::class);
57+
58+
$this->getMediaAssetById = (new ObjectManager($this))->getObject(
59+
GetById::class,
60+
[
61+
'resourceConnection' => $resourceConnection,
62+
'assetFactory' => $assetFactory,
63+
'logger' => $logger,
64+
]
65+
);
66+
$this->adapter = $this->createMock(AdapterInterface::class);
67+
$resourceConnection->method('getConnection')->willReturn($this->adapter);
68+
69+
$this->selectStub = $this->createMock(Select::class);
70+
$this->selectStub->method('from')->willReturnSelf();
71+
$this->selectStub->method('where')->willReturnSelf();
72+
$this->adapter->method('select')->willReturn($this->selectStub);
73+
74+
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
75+
}
76+
77+
/**
78+
* Test an exception during the get asset data query.
79+
*/
80+
public function testExceptionDuringGetMediaAssetData(): void
81+
{
82+
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
83+
$this->adapter->method('query')->willThrowException(new \Exception());
84+
85+
$this->expectException(IntegrationException::class);
86+
87+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID);
88+
}
89+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\MediaGallery\Model\Asset\Command\GetById;
15+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
16+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use Psr\Log\LoggerInterface;
19+
use Zend\Db\Adapter\Driver\Pdo\Statement;
20+
21+
/**
22+
* Test the GetById command model
23+
*/
24+
class GetByIdTest extends \PHPUnit\Framework\TestCase
25+
{
26+
private const MEDIA_ASSET_STUB_ID = 1;
27+
28+
private const MEDIA_ASSET_DATA = ['id' => 1];
29+
30+
/**
31+
* @var GetById|MockObject
32+
*/
33+
private $getMediaAssetById;
34+
35+
/**
36+
* @var AssetInterfaceFactory|MockObject
37+
*/
38+
private $assetFactory;
39+
40+
/**
41+
* @var AdapterInterface|MockObject
42+
*/
43+
private $adapter;
44+
45+
/**
46+
* @var Select|MockObject
47+
*/
48+
private $selectStub;
49+
50+
/**
51+
* @var Statement|MockObject
52+
*/
53+
private $statementMock;
54+
55+
/**
56+
* Initialize basic test class mocks
57+
*/
58+
protected function setUp(): void
59+
{
60+
$resourceConnection = $this->createMock(ResourceConnection::class);
61+
$this->assetFactory = $this->createMock(AssetInterfaceFactory::class);
62+
$logger = $this->createMock(LoggerInterface::class);
63+
64+
$this->getMediaAssetById = (new ObjectManager($this))->getObject(
65+
GetById::class,
66+
[
67+
'resourceConnection' => $resourceConnection,
68+
'assetFactory' => $this->assetFactory,
69+
'logger' => $logger,
70+
]
71+
);
72+
$this->adapter = $this->createMock(AdapterInterface::class);
73+
$resourceConnection->method('getConnection')->willReturn($this->adapter);
74+
75+
$this->selectStub = $this->createMock(Select::class);
76+
$this->selectStub->method('from')->willReturnSelf();
77+
$this->selectStub->method('where')->willReturnSelf();
78+
$this->adapter->method('select')->willReturn($this->selectStub);
79+
80+
$this->statementMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)->getMock();
81+
}
82+
83+
/**
84+
* Test successful get media asset by id command execution.
85+
*/
86+
public function testSuccessfulGetByIdExecution(): void
87+
{
88+
$this->statementMock->method('fetch')->willReturn(self::MEDIA_ASSET_DATA);
89+
$this->adapter->method('query')->willReturn($this->statementMock);
90+
91+
$mediaAssetStub = $this->getMockBuilder(AssetInterface::class)->getMock();
92+
$this->assetFactory->expects($this->once())->method('create')->willReturn($mediaAssetStub);
93+
94+
$this->assertEquals(
95+
$mediaAssetStub,
96+
$this->getMediaAssetById->execute(self::MEDIA_ASSET_STUB_ID)
97+
);
98+
}
99+
}

0 commit comments

Comments
 (0)