Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SchemaBuilderTest.php
Go to the documentation of this file.
1 <?php
8 
22 
30 class SchemaBuilderTest extends \PHPUnit\Framework\TestCase
31 {
35  private $model;
36 
40  private $objectManagerHelper;
41 
45  private $elementFactoryMock;
46 
50  private $booleanUtilsMock;
51 
55  private $shardingMock;
56 
60  private $validationCompositeMock;
61 
65  private $resourceConnectionMock;
66 
67  protected function setUp()
68  {
69  $this->elementFactoryMock = $this->getMockBuilder(ElementFactory::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->booleanUtilsMock = $this->getMockBuilder(BooleanUtils::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->shardingMock = $this->getMockBuilder(Sharding::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->validationCompositeMock = $this->getMockBuilder(ValidationComposite::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84 
85  $this->objectManagerHelper = new ObjectManagerHelper($this);
86  $this->model = $this->objectManagerHelper->getObject(
87  \Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder::class,
88  [
89  'elementFactory' => $this->elementFactoryMock,
90  'booleanUtils' => new BooleanUtils(),
91  'sharding' => $this->shardingMock,
92  'validationComposite' => $this->validationCompositeMock,
93  'resourceConnection' => $this->resourceConnectionMock
94  ]
95  );
96  }
97 
101  public function tablesProvider()
102  {
103  return [
104  [
105  [
106  'first_table' => [
107  'name' => 'first_table',
108  'engine' => 'innodb',
109  'resource' => 'default',
110  'column' => [
111  'first_column' => [
112  'name' => 'first_column',
113  'type' => 'int',
114  'padding' => 10,
115  'identity' => true,
116  'nullable' => false
117  ],
118  'foreign_column' => [
119  'name' => 'foreign_column',
120  'type' => 'int',
121  'padding' => 10,
122  'nullable' => false
123  ],
124  'some_disabled_column' => [
125  'name' => 'some_disabled_column',
126  'disabled' => 'true',
127  'type' => 'int',
128  'padding' => 10,
129  'nullable' => false
130  ],
131  'second_column' => [
132  'name' => 'second_column',
133  'type' => 'timestamp',
134  'default' => 'CURRENT_TIMESTAMP',
135  'on_update' => true
136  ],
137  ],
138  'constraint' => [
139  'some_foreign_key' => [
140  'name' => 'some_foreign_key',
141  'type' => 'foreign',
142  'column' => 'foreign_column',
143  'table' => 'first_table',
144  'referenceTable' => 'second_table',
145  'referenceColumn' => 'ref_column'
146  ],
147  'PRIMARY' => [
148  'name' => 'PRIMARY',
149  'type' => 'primary',
150  'column' => [
151  'first_column'
152  ]
153  ]
154  ]
155  ],
156  'second_table' => [
157  'name' => 'second_table',
158  'engine' => 'innodb',
159  'resource' => 'default',
160  'column' => [
161  'ref_column' => [
162  'name' => 'ref_column',
163  'type' => 'int',
164  'padding' => 10,
165  'nullable' => false
166  ],
167  ],
168  'index' => [
169  'FIRST_INDEX' => [
170  'name' => 'FIRST_INDEX',
171  'column' => [
172  'ref_column'
173  ]
174  ]
175  ],
176  ]
177  ]
178  ]
179  ];
180  }
181 
188  private function createTable($name)
189  {
190  return new Table(
191  $name,
192  $name,
193  'table',
194  'default',
195  'resource',
196  'utf-8',
197  'utf-8',
198  ''
199  );
200  }
201 
209  private function createIntegerAIColumn($name, Table $table)
210  {
211  return new Integer(
212  $name,
213  'int',
214  $table,
215  10,
216  true,
217  false,
218  true
219  );
220  }
221 
229  private function createIntegerColumn($name, Table $table)
230  {
231  return new Integer(
232  $name,
233  'int',
234  $table,
235  10
236  );
237  }
238 
247  private function createPrimaryConstraint(Table $table, array $columns, $nameWithoutPrefix = 'PRIMARY')
248  {
249  return new Internal(
250  'PRIMARY',
251  'primary',
252  $table,
253  $nameWithoutPrefix,
254  $columns
255  );
256  }
257 
267  private function createIndex($indexName, Table $table, array $columns, $nameWithoutPrefix = null)
268  {
269  return new Index(
270  $indexName,
271  'index',
272  $table,
273  $columns,
274  'btree',
275  $nameWithoutPrefix ?: $indexName
276  );
277  }
278 
286  private function createTimestampColumn($name, Table $table)
287  {
288  return new Timestamp(
289  $name,
290  'timestamp',
291  $table,
292  'CURRENT_TIMESTAMP',
293  false,
294  true
295  );
296  }
297 
304  public function testBuild(array $tablesData)
305  {
306  $table = $this->createTable('first_table');
307  $refTable = $this->createTable('second_table');
308  $refColumn = $this->createIntegerColumn('ref_column', $refTable);
309  $index = $this->createIndex('PRE_FIRST_INDEX', $table, [$refColumn], 'FIRST_INDEX');
310  $refTable->addColumns([$refColumn]);
311  $refTable->addIndexes([$index]);
312  $firstColumn = $this->createIntegerAIColumn('first_column', $table);
313  $foreignColumn = $this->createIntegerColumn('foreign_column', $table);
314  $timestampColumn = $this->createTimestampColumn('second_column', $table);
315  $primaryKey = $this->createPrimaryConstraint($table, [$firstColumn]);
316  $foreignKey = new Reference(
317  'some_foreign_key',
318  'foreign',
319  $table,
320  'some_foreign_key',
321  $foreignColumn,
322  $refTable,
323  $refColumn,
324  'CASCADE'
325  );
326  $firstTableColumns = [$firstColumn, $foreignColumn, $timestampColumn];
327  $firstTableConstraints = [$foreignKey, $primaryKey];
328  $table->addColumns($firstTableColumns);
329  $table->addConstraints($firstTableConstraints);
330  $this->elementFactoryMock->expects(self::exactly(9))
331  ->method('create')
332  ->willReturnOnConsecutiveCalls(
333  $table,
334  $firstColumn,
335  $foreignColumn,
336  $timestampColumn,
337  $refTable,
338  $refColumn,
339  $index,
340  $foreignKey,
341  $primaryKey
342  );
343  $resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
344  ->disableOriginalConstructor()
345  ->getMock();
347  $schema = $this->objectManagerHelper->getObject(
348  Schema::class,
349  ['resourceConnection' => $resourceConnectionMock]
350  );
351  $this->resourceConnectionMock->expects(self::once())
352  ->method('getTableName')
353  ->willReturn('second_table');
354  $resourceConnectionMock->expects(self::exactly(6))
355  ->method('getTableName')
356  ->withConsecutive(
357  ['first_table'],
358  ['first_table'],
359  ['second_table'],
360  ['second_table'],
361  ['first_table'],
362  ['second_table']
363  )
364  ->willReturnOnConsecutiveCalls(
365  'first_table',
366  'first_table',
367  'second_table',
368  'second_table',
369  'first_table',
370  'second_table'
371  );
372  $this->model->addTablesData($tablesData);
373  $this->model->build($schema);
374  }
375 }
return false
Definition: gallery.phtml:36
$columns
Definition: default.phtml:15
$table
Definition: trigger.php:14
$index
Definition: list.phtml:44
if(!isset($_GET['name'])) $name
Definition: log.php:14