Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RealTest.php
Go to the documentation of this file.
1 <?php
7 
18 
19 class RealTest extends \PHPUnit\Framework\TestCase
20 {
24  private $objectManager;
25 
29  private $real;
30 
34  private $nullableMock;
35 
39  private $commentMock;
40 
44  private $resourceConnectionMock;
45 
49  private $unsignedMock;
50 
51  protected function setUp()
52  {
53  $this->objectManager = new ObjectManager($this);
54  $this->nullableMock = $this->getMockBuilder(Nullable::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->commentMock = $this->getMockBuilder(Comment::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->unsignedMock = $this->getMockBuilder(Unsigned::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->real = $this->objectManager->getObject(
67  Real::class,
68  [
69  'nullable' => $this->nullableMock,
70  'unsigned' => $this->unsignedMock,
71  'resourceConnection' => $this->resourceConnectionMock,
72  'comment' => $this->commentMock,
73  ]
74  );
75  }
76 
80  public function testToDefinitionNoScale()
81  {
83  $column = $this->getMockBuilder(RealColumnDto::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $column->expects($this->any())
87  ->method('getName')
88  ->willReturn('col');
89  $column->expects($this->any())
90  ->method('getType')
91  ->willReturn('float');
92  $column->expects($this->any())
93  ->method('getPrecision')
94  ->willReturn(0);
95  $column->expects($this->any())
96  ->method('getScale')
97  ->willReturn(0);
98  $column->expects($this->any())
99  ->method('getDefault')
100  ->willReturn(0);
101  $this->unsignedMock->expects($this->any())
102  ->method('toDefinition')
103  ->with($column)
104  ->willReturn('UNSIGNED');
105  $this->nullableMock->expects($this->any())
106  ->method('toDefinition')
107  ->with($column)
108  ->willReturn('NOT NULL');
109  $adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
110  ->disableOriginalConstructor()
111  ->getMock();
112  $this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($adapterMock);
113  $adapterMock->expects($this->once())
114  ->method('quoteIdentifier')
115  ->with('col')
116  ->willReturn('`col`');
117  $this->commentMock->expects($this->any())
118  ->method('toDefinition')
119  ->with($column)
120  ->willReturn('COMMENT "Comment"');
121  $this->assertEquals(
122  '`col` float UNSIGNED NOT NULL DEFAULT 0 COMMENT "Comment"',
123  $this->real->toDefinition($column)
124  );
125  }
126 
130  public function testToDefinition()
131  {
133  $column = $this->getMockBuilder(RealColumnDto::class)
134  ->disableOriginalConstructor()
135  ->getMock();
136  $column->expects($this->any())
137  ->method('getName')
138  ->willReturn('col');
139  $column->expects($this->any())
140  ->method('getType')
141  ->willReturn('float');
142  $column->expects($this->any())
143  ->method('getPrecision')
144  ->willReturn(10);
145  $column->expects($this->any())
146  ->method('getScale')
147  ->willReturn(4);
148  $column->expects($this->any())
149  ->method('getDefault')
150  ->willReturn(0);
151  $this->unsignedMock->expects($this->any())
152  ->method('toDefinition')
153  ->with($column)
154  ->willReturn('UNSIGNED');
155  $this->nullableMock->expects($this->any())
156  ->method('toDefinition')
157  ->with($column)
158  ->willReturn('NOT NULL');
159  $adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
160  ->disableOriginalConstructor()
161  ->getMock();
162  $this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($adapterMock);
163  $adapterMock->expects($this->once())
164  ->method('quoteIdentifier')
165  ->with('col')
166  ->willReturn('`col`');
167  $this->commentMock->expects($this->any())
168  ->method('toDefinition')
169  ->with($column)
170  ->willReturn('COMMENT "Comment"');
171  $this->assertEquals(
172  '`col` float(10, 4) UNSIGNED NOT NULL DEFAULT 0 COMMENT "Comment"',
173  $this->real->toDefinition($column)
174  );
175  }
176 
180  public function testToDefinitionNoDefault()
181  {
183  $column = $this->getMockBuilder(RealColumnDto::class)
184  ->disableOriginalConstructor()
185  ->getMock();
186  $column->expects($this->any())
187  ->method('getName')
188  ->willReturn('col');
189  $column->expects($this->any())
190  ->method('getType')
191  ->willReturn('float');
192  $column->expects($this->any())
193  ->method('getPrecision')
194  ->willReturn(10);
195  $column->expects($this->any())
196  ->method('getScale')
197  ->willReturn(4);
198  $column->expects($this->any())
199  ->method('getDefault')
200  ->willReturn(null);
201  $this->unsignedMock->expects($this->any())
202  ->method('toDefinition')
203  ->with($column)
204  ->willReturn('UNSIGNED');
205  $this->nullableMock->expects($this->any())
206  ->method('toDefinition')
207  ->with($column)
208  ->willReturn('NOT NULL');
209  $adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
210  ->disableOriginalConstructor()
211  ->getMock();
212  $this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($adapterMock);
213  $adapterMock->expects($this->once())
214  ->method('quoteIdentifier')
215  ->with('col')
216  ->willReturn('`col`');
217  $this->commentMock->expects($this->any())
218  ->method('toDefinition')
219  ->with($column)
220  ->willReturn('COMMENT "Comment"');
221  $this->assertEquals(
222  '`col` float(10, 4) UNSIGNED NOT NULL COMMENT "Comment"',
223  $this->real->toDefinition($column)
224  );
225  }
226 
234  public function testFromDefinition($definition, $expectedPrecision = false, $expectedScale = false)
235  {
236  $expectedData = [
237  'definition' => $definition,
238  ];
239  if ($expectedPrecision) {
240  $expectedData['precision'] = $expectedPrecision;
241  $expectedData['scale'] = $expectedScale;
242  }
243  $this->unsignedMock->expects($this->any())->method('fromDefinition')->willReturnArgument(0);
244  $this->nullableMock->expects($this->any())->method('fromDefinition')->willReturnArgument(0);
245  $result = $this->real->fromDefinition(['definition' => $definition]);
246  $this->assertEquals($expectedData, $result);
247  }
248 
252  public function definitionDataProvider()
253  {
254  return [
255  ['float'],
256  ['float(10,4)', 10, 4],
257  ['float(10)', false, false],
258  ['decimal(10)', 10, 0],
259  ['decimal(10, 6)', 10, 6],
260  ['double(10, 6)', 10, 6],
261  ['double', false, false],
262  ['double(10)', false, false],
263  ];
264  }
265 }
testFromDefinition($definition, $expectedPrecision=false, $expectedScale=false)
Definition: RealTest.php:234