Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReadEntityRowTest.php
Go to the documentation of this file.
1 <?php
7 
11 class ReadEntityRowTest extends \PHPUnit\Framework\TestCase
12 {
18  protected $subject;
19 
23  protected $select;
24 
28  protected $connection;
29 
33  protected $metadataPool;
34 
35  protected function setUp()
36  {
37  $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
38 
39  $this->connection = $this->getMockForAbstractClass(
40  \Magento\Framework\DB\Adapter\AdapterInterface::class,
41  [],
42  '',
43  false,
44  false,
45  true,
46  []
47  );
48 
49  $this->connection->expects($this->any())
50  ->method('select')
51  ->willReturn($this->select);
52 
53  $this->connection->expects($this->any())
54  ->method('quoteIdentifier')
55  ->willReturnArgument(0);
56 
57  $metadata = $this->createMock(\Magento\Framework\EntityManager\EntityMetadata::class);
58 
59  $metadata->expects($this->any())
60  ->method('getEntityTable')
61  ->willReturn('entity_table');
62 
63  $metadata->expects($this->any())
64  ->method('getEntityConnection')
65  ->willReturn($this->connection);
66 
67  $metadata->expects($this->any())
68  ->method('getIdentifierField')
69  ->willReturn('identifier');
70 
71  $this->metadataPool = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
72 
73  $this->metadataPool->expects($this->any())
74  ->method('getMetadata')
75  ->with('Test\Entity\Type')
76  ->willReturn($metadata);
77 
78  $this->subject = new \Magento\Framework\Model\ResourceModel\Db\ReadEntityRow(
79  $this->metadataPool
80  );
81  }
82 
83  public function testExecute()
84  {
85  $identifier = '100000001';
86 
87  $context = ['store_id' => 1];
88  $expectedData = ['entity_id' => 1];
89 
90  $this->select->expects($this->once())
91  ->method('from')
92  ->with(['t' => 'entity_table'])
93  ->willReturnSelf();
94 
95  $this->select->expects($this->at(1))
96  ->method('where')
97  ->with('identifier = ?', $identifier)
98  ->willReturnSelf();
99 
100  $this->select->expects($this->at(2))
101  ->method('where')
102  ->with('store_id = ?', 1)
103  ->willReturnSelf();
104 
105  $this->connection->expects($this->once())
106  ->method('fetchRow')
107  ->willReturn($expectedData);
108 
109  $actualData = $this->subject->execute('Test\Entity\Type', $identifier, $context);
110 
111  $this->assertEquals($expectedData, $actualData);
112  }
113 }