Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TimestampTest.php
Go to the documentation of this file.
1 <?php
7 
15 
16 class TimestampTest extends \PHPUnit\Framework\TestCase
17 {
21  private $objectManager;
22 
26  private $timestamp;
27 
31  private $nullableMock;
32 
36  private $commentMock;
37 
41  private $resourceConnectionMock;
42 
46  private $onUpdateMock;
47 
48  protected function setUp()
49  {
50  $this->objectManager = new ObjectManager($this);
51  $this->nullableMock = $this->getMockBuilder(Nullable::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54  $this->commentMock = $this->getMockBuilder(Comment::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->onUpdateMock = $this->getMockBuilder(OnUpdate::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->timestamp = $this->objectManager->getObject(
64  Timestamp::class,
65  [
66  'onUpdate' => $this->onUpdateMock,
67  'nullable' => $this->nullableMock,
68  'comment' => $this->commentMock,
69  'resourceConnection' => $this->resourceConnectionMock
70  ]
71  );
72  }
73 
82  public function testToDefinition($default, $nullable, $onUpdate, $expectedStatement)
83  {
85  $column = $this->getMockBuilder(BooleanColumn::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88  $column->expects($this->any())
89  ->method('getName')
90  ->willReturn('col');
91  $column->expects($this->any())
92  ->method('getType')
93  ->willReturn('DATETIME');
94  $column->expects($this->any())
95  ->method('getDefault')
96  ->willReturn($default);
97  $adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
98  ->disableOriginalConstructor()
99  ->getMock();
100  $this->resourceConnectionMock->expects($this->once())->method('getConnection')->willReturn($adapterMock);
101  $adapterMock->expects($this->once())
102  ->method('quoteIdentifier')
103  ->with('col')
104  ->willReturn('`col`');
105  if ($nullable) {
106  $this->nullableMock->expects($this->any())
107  ->method('toDefinition')
108  ->with($column)
109  ->willReturn('NULL');
110  } else {
111  $this->nullableMock->expects($this->any())
112  ->method('toDefinition')
113  ->with($column)
114  ->willReturn('NOT NULL');
115  }
116  $this->commentMock->expects($this->any())
117  ->method('toDefinition')
118  ->with($column)
119  ->willReturn('COMMENT "Comment"');
120  if ($onUpdate) {
121  $this->onUpdateMock->expects($this->any())
122  ->method('toDefinition')
123  ->with($column)
124  ->willReturn('ON UPDATE CURRENT_TIMESTAMP');
125  }
126  $this->assertEquals(
127  $expectedStatement,
128  $this->timestamp->toDefinition($column)
129  );
130  }
131 
135  public function toDefinitionProvider()
136  {
137  return [
138  [
139  'default' => 'NULL', // xsd replaced for no default value set in xml
140  'nullable' => true,
141  'onUpdate' => 'CURRENT_TIMESTAMP',
142  'expectedStatement' => '`col` DATETIME NULL ON UPDATE CURRENT_TIMESTAMP COMMENT "Comment"',
143  ],
144  [
145  'default' => 'NULL', // xsd replaced for no default value set in xml
146  'nullable' => true,
147  'onUpdate' => 'CURRENT_TIMESTAMP',
148  'expectedStatement' => '`col` DATETIME NULL ON UPDATE CURRENT_TIMESTAMP COMMENT "Comment"',
149  ],
150  [
151  'default' => 'NULL', // xsd replaced for no default value set in xml
152  'nullable' => false,
153  'onUpdate' => 'CURRENT_TIMESTAMP',
154  'expectedStatement' => '`col` DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT "Comment"',
155  ],
156  [
157  'default' => 'CURRENT_TIMESTAMP',
158  'nullable' => false,
159  'onUpdate' => false,
160  'expectedStatement' => '`col` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT "Comment"',
161  ],
162  [
163  'default' => 'CURRENT_TIMESTAMP',
164  'nullable' => true,
165  'onUpdate' => 'CURRENT_TIMESTAMP',
166  'expectedStatement' => '`col` DATETIME NULL DEFAULT CURRENT_TIMESTAMP '
167  . 'ON UPDATE CURRENT_TIMESTAMP COMMENT "Comment"',
168  ]
169  ];
170  }
171 }