Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SchemaPersistorTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
20 class SchemaPersistorTest extends \PHPUnit\Framework\TestCase
21 {
25  private $model;
26 
30  private $objectManagerHelper;
31 
35  private $componentRegistrarMock;
36 
40  private $xmlPersistor;
41 
42  protected function setUp() : void
43  {
44  $this->componentRegistrarMock = $this->getMockBuilder(ComponentRegistrar::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $this->xmlPersistor = $this->getMockBuilder(XmlPersistor::class)
48  ->getMock();
49  $this->objectManagerHelper = new ObjectManagerHelper($this);
50  $this->model = $this->objectManagerHelper->getObject(
51  \Magento\Framework\Setup\SchemaPersistor::class,
52  [
53  'componentRegistrar' => $this->componentRegistrarMock,
54  'xmlPersistor' => $this->xmlPersistor
55  ]
56  );
57  }
58 
64  public function testPersist(array $tables, $expectedXML) : void
65  {
66  $moduleName = 'First_Module';
68  $schemaListenerMock = $this->getMockBuilder(SchemaListener::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $schemaListenerMock->expects(self::once())
72  ->method('getTables')
73  ->willReturn($tables);
74  $this->componentRegistrarMock->expects(self::once())
75  ->method('getPath')
76  ->with('module', $moduleName)
77  ->willReturn('some-non-existing-path');
78  $simpleXmlElement = new \SimpleXMLElement($expectedXML);
79  $this->xmlPersistor
80  ->expects(self::once())
81  ->method('persist')
82  ->with($simpleXmlElement, 'some-non-existing-path/etc/db_schema.xml');
83 
84  $this->model->persist($schemaListenerMock);
85  }
86 
92  public function schemaListenerTablesDataProvider() : array
93  {
94  return [
95  [
96  'schemaListenerTables' => [
97  'First_Module' => [
98  'first_table' => [
99  'disabled' => false,
100  'name' => 'first_table',
101  'resource' => 'default',
102  'engine' => 'innodb',
103  'columns' => [
104  'first_column' => [
105  'name' => 'first_column',
106  'xsi:type' => 'integer',
107  'nullable' => 1,
108  'unsigned' => '0',
109  ],
110  'second_column' => [
111  'name' => 'second_column',
112  'xsi:type' => 'date',
113  'nullable' => 0,
114  ]
115  ],
116  'indexes' => [
117  'TEST_INDEX' => [
118  'name' => 'TEST_INDEX',
119  'indexType' => 'btree',
120  'columns' => [
121  'first_column'
122  ]
123  ]
124  ],
125  'constraints' => [
126  'foreign' => [
127  'some_foreign_constraint' => [
128  'referenceTable' => 'table',
129  'referenceColumn' => 'column',
130  'table' => 'first_table',
131  'column' => 'first_column'
132  ]
133  ],
134  'primary' => [
135  'PRIMARY' => [
136  'xsi:type' => 'primary',
137  'name' => 'PRIMARY',
138  'columns' => [
139  'second_column'
140  ]
141  ]
142  ]
143  ]
144  ]
145  ]
146  ],
147  // @codingStandardsIgnoreStart
148  'XMLResult' => '<?xml version="1.0"?>
149  <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
150  xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
151  <table name="first_table" resource="default" engine="innodb">
152  <column xmlns:xsi="xsi" xsi:type="integer" name="first_column" nullable="1"
153  unsigned="0"/>
154  <column xmlns:xsi="xsi" xsi:type="date" name="second_column" nullable="0"/>
155  <constraint xmlns:xsi="xsi" xsi:type="foreign" name="some_foreign_constraint"
156  referenceTable="table" referenceColumn="column"
157  table="first_table" column="first_column"/>
158  <constraint xmlns:xsi="xsi" xsi:type="primary" name="PRIMARY">
159  <column name="second_column"/>
160  </constraint>
161  <index name="TEST_INDEX" indexType="btree">
162  <column name="first_column"/>
163  </index>
164  </table>
165  </schema>'
166  // @codingStandardsIgnoreEnd
167  ]
168  ];
169  }
170 }