ConnectionFactoryTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\ReportXml;
  7. use Magento\Analytics\ReportXml\ConnectionFactory;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\Pdo\Mysql as MysqlPdoAdapter;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\DB\Adapter\AdapterInterface;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  13. class ConnectionFactoryTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $resourceConnectionMock;
  19. /**
  20. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $objectManagerMock;
  23. /**
  24. * @var ConnectionFactory|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $connectionNewMock;
  27. /**
  28. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $connectionMock;
  31. /**
  32. * @var ObjectManagerHelper
  33. */
  34. private $objectManagerHelper;
  35. /**
  36. * @var ConnectionFactory
  37. */
  38. private $connectionFactory;
  39. /**
  40. * @return void
  41. */
  42. protected function setUp()
  43. {
  44. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->connectionMock = $this->getMockBuilder(MysqlPdoAdapter::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->connectionNewMock = $this->getMockBuilder(MysqlPdoAdapter::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->objectManagerHelper = new ObjectManagerHelper($this);
  57. $this->connectionFactory = $this->objectManagerHelper->getObject(
  58. ConnectionFactory::class,
  59. [
  60. 'resourceConnection' => $this->resourceConnectionMock,
  61. 'objectManager' => $this->objectManagerMock,
  62. ]
  63. );
  64. }
  65. public function testGetConnection()
  66. {
  67. $connectionName = 'read';
  68. $this->resourceConnectionMock
  69. ->expects($this->once())
  70. ->method('getConnection')
  71. ->with($connectionName)
  72. ->willReturn($this->connectionMock);
  73. $this->connectionMock
  74. ->expects($this->once())
  75. ->method('getConfig')
  76. ->with()
  77. ->willReturn(['persistent' => 1]);
  78. $this->objectManagerMock
  79. ->expects($this->once())
  80. ->method('create')
  81. ->with(get_class($this->connectionMock), ['config' => ['use_buffered_query' => false]])
  82. ->willReturn($this->connectionNewMock);
  83. $this->assertSame($this->connectionNewMock, $this->connectionFactory->getConnection($connectionName));
  84. }
  85. }