Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OperationsExecutorTest.php
Go to the documentation of this file.
1 <?php
8 
23 
31 class OperationsExecutorTest extends \PHPUnit\Framework\TestCase
32 {
36  private $model;
37 
41  private $objectManagerHelper;
42 
46  private $shardingMock;
47 
51  private $resourceConnectionMock;
52 
56  private $statementFactoryMock;
57 
61  private $dbSchemaWriterMock;
62 
66  private $statementAggregatorFactoryMock;
67 
71  private $createTableOperation;
72 
76  private $dropElement;
77 
78  protected function setUp()
79  {
80  $this->shardingMock = $this->getMockBuilder(Sharding::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->statementFactoryMock = $this->getMockBuilder(StatementFactory::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->dbSchemaWriterMock = $this->getMockBuilder(DbSchemaWriterInterface::class)
90  ->getMockForAbstractClass();
91  $this->statementAggregatorFactoryMock = $this->getMockBuilder(StatementAggregatorFactory::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94  $this->createTableOperation = $this->getMockBuilder(CreateTable::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97  $this->createTableOperation->expects(self::exactly(2))
98  ->method('getOperationName')
99  ->willReturn('create_table');
100  $this->dropElement = $this->getMockBuilder(DropElement::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103  $this->objectManagerHelper = new ObjectManagerHelper($this);
104  $this->model = $this->objectManagerHelper->getObject(
105  \Magento\Framework\Setup\Declaration\Schema\OperationsExecutor::class,
106  [
107  'operations' => [
108  'create_table' => $this->createTableOperation,
109  'drop_element' => $this->dropElement
110  ],
111  'dataSaviorsCollection' => [],
112  'sharding' => $this->shardingMock,
113  'resourceConnection' => $this->resourceConnectionMock,
114  'statementFactory' => $this->statementFactoryMock,
115  'dbSchemaWriter' => $this->dbSchemaWriterMock,
116  'statementAggregatorFactory' => $this->statementAggregatorFactoryMock
117  ]
118  );
119  }
120 
124  private function prepareTable()
125  {
126  $table = new Table(
127  'table',
128  'table',
129  'table',
130  'default',
131  'innodb',
132  'utf-8',
133  'utf-8',
134  ''
135  );
136  $column = new Integer(
137  'int',
138  'int',
139  $table,
140  11,
141  false,
142  false,
143  false
144  );
145  $table->addColumns([$column]);
146  return $table;
147  }
148 
149  public function testExecute()
150  {
152  $diff = $this->getMockBuilder(DiffInterface::class)
153  ->getMock();
154  $this->shardingMock->expects(self::exactly(2))
155  ->method('getResources')
156  ->willReturn(['default']);
157  $connectionMock = $this->getMockBuilder(Mysql::class)
158  ->disableOriginalConstructor()
159  ->getMock();
160  $this->resourceConnectionMock->expects(self::exactly(3))
161  ->method('getConnection')
162  ->with('default')
163  ->willReturn($connectionMock);
164  $statementAggregator = $this->getMockBuilder(StatementAggregator::class)
165  ->disableOriginalConstructor()
166  ->getMock();
167  $this->statementAggregatorFactoryMock->expects(self::once())
168  ->method('create')
169  ->willReturn($statementAggregator);
170  $elementHistory = new ElementHistory($this->prepareTable());
171  $tablesHistories = [
172  'table' => [
173  'create_table' => [$elementHistory]
174  ]
175  ];
176  $this->createTableOperation->expects(self::once())
177  ->method('doOperation')
178  ->with($elementHistory)
179  ->willReturn(['TABLE table (`int` INT(11))']);
180  $statementAggregator->expects(self::once())
181  ->method('addStatements')
182  ->with(['TABLE table (`int` INT(11))']);
183  $this->dbSchemaWriterMock->expects(self::once())
184  ->method('compile')
185  ->with($statementAggregator);
186  $diff->expects(self::once())
187  ->method('getAll')
188  ->willReturn($tablesHistories);
189  $this->dropElement->expects(self::at(0))
190  ->method('doOperation');
191  $this->model->execute($diff, []);
192  }
193 }
$table
Definition: trigger.php:14