Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetupTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Module\Setup;
10 
11 class SetupTest extends \PHPUnit\Framework\TestCase
12 {
13  const CONNECTION_NAME = 'connection';
14 
18  private $resourceModel;
19 
23  private $connection;
24 
28  private $object;
29 
30  protected function setUp()
31  {
32  $this->resourceModel = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
33  $this->connection = $this->getMockForAbstractClass(\Magento\Framework\DB\Adapter\AdapterInterface::class);
34  $this->resourceModel->expects($this->any())
35  ->method('getConnection')
36  ->with(self::CONNECTION_NAME)
37  ->willReturn($this->connection);
38  $this->resourceModel->expects($this->any())
39  ->method('getConnectionByName')
41  ->willReturn($this->connection);
42  $this->object = new Setup($this->resourceModel, self::CONNECTION_NAME);
43  }
44 
45  public function testGetConnection()
46  {
47  $this->assertSame($this->connection, $this->object->getConnection());
48  // Check that new connection is not created every time
49  $this->assertSame($this->connection, $this->object->getConnection());
50  }
51 
52  public function testSetTableName()
53  {
54  $tableName = 'table';
55  $expectedTableName = 'expected_table';
56 
57  $this->assertEmpty($this->object->getTable($tableName));
58  $this->object->setTable($tableName, $expectedTableName);
59  $this->assertSame($expectedTableName, $this->object->getTable($tableName));
60  }
61 
62  public function testGetTable()
63  {
64  $tableName = 'table';
65  $expectedTableName = 'expected_table';
66 
67  $this->resourceModel->expects($this->once())
68  ->method('getTableName')
69  ->with($tableName)
70  ->will($this->returnValue($expectedTableName));
71 
72  $this->assertSame($expectedTableName, $this->object->getTable($tableName));
73  // Check that table name is cached
74  $this->assertSame($expectedTableName, $this->object->getTable($tableName));
75  }
76 
77  public function testTableExists()
78  {
79  $tableName = 'table';
80  $this->object->setTable($tableName, $tableName);
81  $this->connection->expects($this->once())
82  ->method('isTableExists')
83  ->with($tableName)
84  ->will($this->returnValue(true));
85  $this->assertTrue($this->object->tableExists($tableName));
86  }
87 
88  public function testRun()
89  {
90  $q = 'SELECT something';
91  $this->connection->expects($this->once())
92  ->method('query')
93  ->with($q);
94  $this->object->run($q);
95  }
96 
97  public function testStartSetup()
98  {
99  $this->connection->expects($this->once())
100  ->method('startSetup');
101  $this->object->startSetup();
102  }
103 
104  public function testEndSetup()
105  {
106  $this->connection->expects($this->once())
107  ->method('endSetup');
108  $this->object->endSetup();
109  }
110 }
$tableName
Definition: trigger.php:13