Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IntegerTest.php
Go to the documentation of this file.
1 <?php
7 
17 
18 class IntegerTest extends \PHPUnit\Framework\TestCase
19 {
23  private $objectManager;
24 
28  private $integer;
29 
33  private $nullableMock;
34 
38  private $commentMock;
39 
43  private $resourceConnectionMock;
44 
48  private $identityMock;
49 
53  private $unsignedMock;
54 
58  private $booleanMock;
59 
60  protected function setUp()
61  {
62  $this->objectManager = new ObjectManager($this);
63  $this->nullableMock = $this->getMockBuilder(Nullable::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->commentMock = $this->getMockBuilder(Comment::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->identityMock = $this->getMockBuilder(Identity::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->unsignedMock = $this->getMockBuilder(Unsigned::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->booleanMock = $this->getMockBuilder(Boolean::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->integer = $this->objectManager->getObject(
82  Integer::class,
83  [
84  'unsigned' => $this->unsignedMock,
85  'boolean' => $this->booleanMock,
86  'nullable' => $this->nullableMock,
87  'identity' => $this->identityMock,
88  'comment' => $this->commentMock,
89  'resourceConnection' => $this->resourceConnectionMock
90  ]
91  );
92  }
93 
97  public function testToDefinition()
98  {
100  $column = $this->getMockBuilder(IntegerColumnDto::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103  $column->expects($this->any())
104  ->method('getName')
105  ->willReturn('int_column');
106  $column->expects($this->any())
107  ->method('getType')
108  ->willReturn('int');
109  $column->expects($this->any())
110  ->method('getPadding')
111  ->willReturn(10);
112  $column->expects($this->any())
113  ->method('getDefault')
114  ->willReturn(0);
115  $this->unsignedMock->expects($this->any())
116  ->method('toDefinition')
117  ->with($column)
118  ->willReturn('UNSIGNED');
119  $this->nullableMock->expects($this->any())
120  ->method('toDefinition')
121  ->with($column)
122  ->willReturn('NOT NULL');
123  $this->identityMock->expects($this->any())
124  ->method('toDefinition')
125  ->willReturn('AUTO_INCREMENT');
126  $adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
127  ->disableOriginalConstructor()
128  ->getMock();
129  $this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($adapterMock);
130  $adapterMock->expects($this->once())
131  ->method('quoteIdentifier')
132  ->with('int_column')
133  ->willReturn('`int_column`');
134  $this->nullableMock->expects($this->any())
135  ->method('toDefinition')
136  ->with($column)
137  ->willReturn('NULL');
138  $this->commentMock->expects($this->any())
139  ->method('toDefinition')
140  ->with($column)
141  ->willReturn('COMMENT "Comment"');
142  $this->assertEquals(
143  '`int_column` int(10) UNSIGNED NOT NULL DEFAULT 0 AUTO_INCREMENT COMMENT "Comment"',
144  $this->integer->toDefinition($column)
145  );
146  }
147 
155  public function testFromDefinition($definition, $expectedLength = false)
156  {
157  $expectedData = [
158  'definition' => $definition,
159  ];
160  if ($expectedLength) {
161  $expectedData['padding'] = $expectedLength;
162  }
163  $this->unsignedMock->expects($this->any())->method('fromDefinition')->willReturnArgument(0);
164  $this->identityMock->expects($this->any())->method('fromDefinition')->willReturnArgument(0);
165  $this->nullableMock->expects($this->any())->method('fromDefinition')->willReturnArgument(0);
166  $this->booleanMock->expects($this->any())->method('fromDefinition')->willReturnArgument(0);
167  $result = $this->integer->fromDefinition(['definition' => $definition]);
168  $this->assertEquals($expectedData, $result);
169  }
170 
174  public function definitionDataProvider()
175  {
176  return [
177  ['int'],
178  ['int(10)', 10],
179  ['tinyint'],
180  ['mediumint(5)', 5],
181  ['mediumint'],
182  ['smallint(3)', 3],
183  ['smallint'],
184  ['bigint(10)', 10],
185  ['bigint'],
186  ];
187  }
188 }