Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InternalTest.php
Go to the documentation of this file.
1 <?php
7 
14 
20 class InternalTest extends \PHPUnit\Framework\TestCase
21 {
25  private $objectManager;
26 
30  private $internal;
31 
35  private $resourceConnectionMock;
36 
37  protected function setUp()
38  {
39  $this->objectManager = new ObjectManager($this);
40  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
41  ->disableOriginalConstructor()
42  ->getMock();
43  $this->internal = $this->objectManager->getObject(
44  Internal::class,
45  [
46  'resourceConnection' => $this->resourceConnectionMock
47  ]
48  );
49  }
50 
56  public function testToDefinition($name, $type, $columns, $expectedExpression)
57  {
59  $constraint = $this->getMockBuilder(InternalConstraintDto::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62  $adapterMock = $this->getMockBuilder(AdapterInterface::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $tableMock = $this->getMockBuilder(Table::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $constraint->expects($this->any())->method('getTable')->willReturn($tableMock);
70  $tableMock->expects($this->any())->method('getResource')->willReturn('default');
71  $this->resourceConnectionMock->expects($this->once())
72  ->method('getConnection')
73  ->with('default')
74  ->willReturn($adapterMock);
75  $constraint->expects($this->any())->method('getName')->willReturn($name);
76  $constraint->expects($this->any())->method('getType')->willReturn($type);
77  $constraint->expects($this->any())->method('getColumnNames')->willReturn($columns);
78  $adapterMock->expects($this->any())
79  ->method('quoteIdentifier')
80  ->willReturnCallback(
81  function ($name) {
82  return '`' . $name . '`';
83  }
84  );
85 
86  $this->assertEquals(
87  $expectedExpression,
88  $this->internal->toDefinition($constraint)
89  );
90  }
91 
95  public function toDefinitionDataProvider()
96  {
97  return [
98  [
99  'name' => 'constraint_name_primary',
100  'type' => 'primary',
101  'columns' => ['id', 'parent_id'],
102  'expectedExpression' => "CONSTRAINT PRIMARY KEY (`id`,`parent_id`)"
103  ],
104  [
105  'name' => 'constraint_name_unique',
106  'type' => 'unique',
107  'columns' => ['id', 'parent_id'],
108  'expectedExpression' => "CONSTRAINT `constraint_name_unique` UNIQUE KEY (`id`,`parent_id`)"
109  ],
110  ];
111  }
112 
120  public function testFromDefinition($definition, $expectedDefinition)
121  {
122  $result = $this->internal->fromDefinition($definition);
123  $this->assertEquals($expectedDefinition, $result);
124  }
125 
129  public function definitionDataProvider()
130  {
131  return [
132  [
133  'definition' => [
134  'Key_name' => 'PRIMARY',
135  'Column_name' => 'id',
136  ],
137  'excpectedDefiniton' => [
138  'name' => 'PRIMARY',
139  'column' => ['id' => 'id'],
140  'type' => 'primary',
141  ],
142  ],
143  [
144  'definition' => [
145  'Key_name' => 'unique_key_1',
146  'Column_name' => 'parent_id',
147  ],
148  'excpectedDefiniton' => [
149  'name' => 'unique_key_1',
150  'column' => ['parent_id' => 'parent_id'],
151  'type' => 'unique',
152  ],
153  ],
154  ];
155  }
156 }
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
if(!isset($_GET['name'])) $name
Definition: log.php:14