Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateRowTest.php
Go to the documentation of this file.
1 <?php
7 
14 
18 class UpdateRowTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $model;
24 
28  protected $metadataPoolMock;
29 
34 
38  protected $connectionMock;
39 
43  protected $metadataMock;
44 
45  protected function setUp()
46  {
47  $this->metadataPoolMock = $this->getMockBuilder(MetadataPool::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
54  ->getMockForAbstractClass();
55  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
56  ->getMockForAbstractClass();
57  $this->metadataMock = $this->getMockBuilder(EntityMetadataInterface::class)
58  ->getMockForAbstractClass();
59 
60  $this->model = (new ObjectManager($this))->getObject(UpdateRow::class, [
61  'metadataPool' => $this->metadataPoolMock,
62  'resourceConnection' => $this->resourceConnectionMock,
63  ]);
64  }
65 
72  public function testExecute(array $data, array $columns, array $preparedColumns)
73  {
74  $primaryKeyName = 'entity_id';
75  $this->metadataPoolMock->expects($this->once())
76  ->method('getMetadata')
77  ->with('test')
78  ->willReturn($this->metadataMock);
79  $this->resourceConnectionMock->expects($this->once())
80  ->method('getConnectionByName')
81  ->willReturn($this->connectionMock);
82  $this->metadataMock->expects($this->once())
83  ->method('getEntityConnectionName')
84  ->willReturn('test_connection_name');
85  $this->metadataMock->expects($this->atLeastOnce())
86  ->method('getEntityTable')
87  ->willReturn('test_entity_table');
88  $this->connectionMock->expects($this->once())
89  ->method('update')
90  ->with('test_entity_table', $preparedColumns, ['test_link_field' . ' = ?' => $data['test_link_field']]);
91  $this->connectionMock->expects($this->once())->method('getIndexList')
92  ->willReturn([$primaryKeyName => ['COLUMNS_LIST' => ['test_link_field']]]);
93  $this->connectionMock->expects($this->once())->method('getPrimaryKeyName')
94  ->willReturn($primaryKeyName);
95  $this->connectionMock->expects($this->once())
96  ->method('describeTable')
97  ->willReturn($columns);
98  $this->metadataMock->expects($this->exactly(2))
99  ->method('getIdentifierField')
100  ->willReturn('test_identified_field');
101  if (empty($data['updated_at'])) {
102  unset($data['updated_at']);
103  }
104  $this->assertSame($data, $this->model->execute('test', $data));
105  }
106 
110  public function columnsDataProvider()
111  {
112  $data = [
113  'test_link_field' => 1,
114  'identified_field' => 'test_identified_field',
115  'test_simple' => 'test_value',
116  ];
117  $columns = [
118  'test_nullable' => [
119  'NULLABLE' => true,
120  'DEFAULT' => false,
121  'IDENTITY' => false,
122  'COLUMN_NAME' => 'test_nullable',
123  ],
124  'test_simple' => [
125  'NULLABLE' => true,
126  'DEFAULT' => false,
127  'IDENTITY' => false,
128  'COLUMN_NAME' => 'test_simple',
129  ],
130  ];
131  $preparedColumns = [
132  'test_identified_field' => null,
133  'test_nullable' => null,
134  'test_simple' => 'test_value',
135  ];
136 
137  return [
138  'default' => [
139  'data' => $data,
140  'columns' => $columns,
141  'preparedColumns' => $preparedColumns,
142  ],
143  'empty timestamp field' => [
144  'data' => array_merge($data, ['updated_at' => '']),
145  'columns' => array_merge(
146  $columns,
147  [
148  'updated_at' => [
149  'NULLABLE' => false,
150  'DEFAULT' => 'CURRENT_TIMESTAMP',
151  'IDENTITY' => false,
152  'COLUMN_NAME' => 'updated_at',
153  ],
154  ]
155  ),
156  'preparedColumns' => $preparedColumns,
157  ],
158  'filled timestamp field' => [
159  'data' => array_merge($data, ['updated_at' => '2016-01-01 00:00:00']),
160  'columns' => array_merge(
161  $columns,
162  [
163  'updated_at' => [
164  'NULLABLE' => false,
165  'DEFAULT' => 'CURRENT_TIMESTAMP',
166  'IDENTITY' => false,
167  'COLUMN_NAME' => 'updated_at',
168  ],
169  ]
170  ),
171  'preparedColumns' => array_merge($preparedColumns, ['updated_at' => '2016-01-01 00:00:00']),
172  ],
173  ];
174  }
175 }
$columns
Definition: default.phtml:15
testExecute(array $data, array $columns, array $preparedColumns)