SetupTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module\Test\Unit;
  7. use \Magento\Framework\Module\Setup;
  8. class SetupTest extends \PHPUnit\Framework\TestCase
  9. {
  10. const CONNECTION_NAME = 'connection';
  11. /**
  12. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $resourceModel;
  15. /**
  16. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $connection;
  19. /**
  20. * @var Setup
  21. */
  22. private $object;
  23. protected function setUp()
  24. {
  25. $this->resourceModel = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  26. $this->connection = $this->getMockForAbstractClass(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  27. $this->resourceModel->expects($this->any())
  28. ->method('getConnection')
  29. ->with(self::CONNECTION_NAME)
  30. ->willReturn($this->connection);
  31. $this->resourceModel->expects($this->any())
  32. ->method('getConnectionByName')
  33. ->with(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION)
  34. ->willReturn($this->connection);
  35. $this->object = new Setup($this->resourceModel, self::CONNECTION_NAME);
  36. }
  37. public function testGetConnection()
  38. {
  39. $this->assertSame($this->connection, $this->object->getConnection());
  40. // Check that new connection is not created every time
  41. $this->assertSame($this->connection, $this->object->getConnection());
  42. }
  43. public function testSetTableName()
  44. {
  45. $tableName = 'table';
  46. $expectedTableName = 'expected_table';
  47. $this->assertEmpty($this->object->getTable($tableName));
  48. $this->object->setTable($tableName, $expectedTableName);
  49. $this->assertSame($expectedTableName, $this->object->getTable($tableName));
  50. }
  51. public function testGetTable()
  52. {
  53. $tableName = 'table';
  54. $expectedTableName = 'expected_table';
  55. $this->resourceModel->expects($this->once())
  56. ->method('getTableName')
  57. ->with($tableName)
  58. ->will($this->returnValue($expectedTableName));
  59. $this->assertSame($expectedTableName, $this->object->getTable($tableName));
  60. // Check that table name is cached
  61. $this->assertSame($expectedTableName, $this->object->getTable($tableName));
  62. }
  63. public function testTableExists()
  64. {
  65. $tableName = 'table';
  66. $this->object->setTable($tableName, $tableName);
  67. $this->connection->expects($this->once())
  68. ->method('isTableExists')
  69. ->with($tableName)
  70. ->will($this->returnValue(true));
  71. $this->assertTrue($this->object->tableExists($tableName));
  72. }
  73. public function testRun()
  74. {
  75. $q = 'SELECT something';
  76. $this->connection->expects($this->once())
  77. ->method('query')
  78. ->with($q);
  79. $this->object->run($q);
  80. }
  81. public function testStartSetup()
  82. {
  83. $this->connection->expects($this->once())
  84. ->method('startSetup');
  85. $this->object->startSetup();
  86. }
  87. public function testEndSetup()
  88. {
  89. $this->connection->expects($this->once())
  90. ->method('endSetup');
  91. $this->object->endSetup();
  92. }
  93. }