Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
StringBinaryTest.php
Go to the documentation of this file.
1 <?php
7 
14 
20 class StringBinaryTest extends \PHPUnit\Framework\TestCase
21 {
25  private $objectManager;
26 
30  private $stringBinary;
31 
35  private $nullableMock;
36 
40  private $commentMock;
41 
45  private $resourceConnectionMock;
46 
47  protected function setUp()
48  {
49  $this->objectManager = new ObjectManager($this);
50  $this->nullableMock = $this->getMockBuilder(Nullable::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53  $this->commentMock = $this->getMockBuilder(Comment::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59  $this->stringBinary = $this->objectManager->getObject(
60  StringBinary::class,
61  [
62  'nullable' => $this->nullableMock,
63  'comment' => $this->commentMock,
64  'resourceConnection' => $this->resourceConnectionMock
65  ]
66  );
67  }
68 
72  public function testToDefinition()
73  {
75  $column = $this->getMockBuilder(StringBinaryColumn::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $column->expects($this->any())
79  ->method('getName')
80  ->willReturn('col');
81  $column->expects($this->any())
82  ->method('getType')
83  ->willReturn('varchar');
84  $column->expects($this->any())
85  ->method('getLength')
86  ->willReturn(50);
87  $column->expects($this->any())
88  ->method('getDefault')
89  ->willReturn('test');
90  $adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($adapterMock);
94  $adapterMock->expects($this->once())
95  ->method('quoteIdentifier')
96  ->with('col')
97  ->willReturn('`col`');
98  $this->nullableMock->expects($this->any())
99  ->method('toDefinition')
100  ->with($column)
101  ->willReturn('NULL');
102  $this->commentMock->expects($this->any())
103  ->method('toDefinition')
104  ->with($column)
105  ->willReturn('COMMENT "Comment"');
106  $this->assertEquals(
107  '`col` varchar(50) NULL DEFAULT "test" COMMENT "Comment"',
108  $this->stringBinary->toDefinition($column)
109  );
110  }
111 
119  public function testFromDefinition($definition, $expectedLength = false)
120  {
121  $expectedData = [
122  'definition' => $definition,
123  ];
124  if ($expectedLength) {
125  $expectedData['length'] = $expectedLength;
126  }
127  $result = $this->stringBinary->fromDefinition(['definition' => $definition]);
128  $this->assertEquals($expectedData, $result);
129  }
130 
134  public function definitionDataProvider()
135  {
136  return [
137  ['char'],
138  ['char(30)', 30],
139  ['varchar'],
140  ['varchar (30)', 30],
141  ['binary'],
142  ['binary(20)', 20],
143  ['varbinary'],
144  ['varbinary(44)', 44],
145  ];
146  }
147 }