Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateEntityRowTest.php
Go to the documentation of this file.
1 <?php
7 
11 class CreateEntityRowTest extends \PHPUnit\Framework\TestCase
12 {
18  protected $subject;
19 
23  protected $connection;
24 
28  protected $metadataPool;
29 
30  protected function setUp()
31  {
32  $this->connection = $this->getMockForAbstractClass(
33  \Magento\Framework\DB\Adapter\AdapterInterface::class,
34  [],
35  '',
36  false,
37  false,
38  true,
39  ['lastInsertId']
40  );
41 
42  $this->connection->expects($this->any())
43  ->method('lastInsertId')
44  ->willReturn(1);
45 
46  $metadata = $this->createMock(\Magento\Framework\EntityManager\EntityMetadata::class);
47 
48  $metadata->expects($this->any())
49  ->method('getLinkField')
50  ->willReturn('entity_id');
51 
52  $metadata->expects($this->any())
53  ->method('getEntityTable')
54  ->willReturn('entity_table');
55 
56  $metadata->expects($this->any())
57  ->method('getEntityConnection')
58  ->willReturn($this->connection);
59 
60  $metadata->expects($this->any())
61  ->method('getIdentifierField')
62  ->willReturn('identifier');
63 
64  $metadata->expects($this->once())
65  ->method('generateIdentifier')
66  ->willReturn('100000001');
67 
68  $this->metadataPool = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
69 
70  $this->metadataPool->expects($this->any())
71  ->method('getMetadata')
72  ->with('Test\Entity\Type')
73  ->willReturn($metadata);
74 
75  $this->subject = new \Magento\Framework\Model\ResourceModel\Db\CreateEntityRow(
76  $this->metadataPool
77  );
78  }
79 
87  public function testExecute($inputData, $tableData, $preparedData, $finalData)
88  {
89  $this->connection->expects($this->any())
90  ->method('describeTable')
91  ->with('entity_table')
92  ->willReturn($tableData);
93 
94  $this->connection->expects($this->once())
95  ->method('insert')
96  ->with('entity_table', $preparedData);
97 
98  $actualData = $this->subject->execute('Test\Entity\Type', $inputData);
99 
100  $this->assertEquals($finalData, $actualData);
101  }
102 
106  public function executeDataProvider()
107  {
108  $inputData = [
109  'test_field_1' => 'test_value_1',
110  'test_field_2' => 100,
111  'test_field_3' => 'test_value_2'
112  ];
113 
114  $tableData = [
115  [
116  'COLUMN_NAME' => 'TEST_FIELD_1',
117  'DEFAULT' => null
118  ],
119  [
120  'COLUMN_NAME' => 'TEST_FIELD_2',
121  'DEFAULT' => null
122  ],
123  [
124  'COLUMN_NAME' => 'TEST_FIELD_3',
125  'DEFAULT' => 'CURRENT_TIMESTAMP'
126  ],
127  [
128  'COLUMN_NAME' => 'TEST_FIELD_4',
129  'DEFAULT' => null
130  ]
131  ];
132 
133  $preparedData = [
134  'test_field_1' => 'test_value_1',
135  'test_field_2' => 100,
136  'test_field_4' => null,
137  'identifier' => '100000001'
138  ];
139 
140  $finalData = [
141  'test_field_1' => 'test_value_1',
142  'test_field_2' => 100,
143  'test_field_3' => 'test_value_2',
144  'entity_id' => 1
145  ];
146 
147  return [
148  [$inputData, $tableData, $preparedData, $finalData]
149  ];
150  }
151 }
testExecute($inputData, $tableData, $preparedData, $finalData)