SelectBuilderTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\DB;
  7. use Magento\Analytics\ReportXml\DB\SelectBuilder;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\AdapterInterface;
  10. use Magento\Framework\DB\Select;
  11. class SelectBuilderTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var SelectBuilder
  15. */
  16. private $selectBuilder;
  17. /**
  18. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $resourceConnectionMock;
  21. /**
  22. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $connectionMock;
  25. /**
  26. * @var Select|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $selectMock;
  29. /**
  30. * @return void
  31. */
  32. protected function setUp()
  33. {
  34. $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->selectMock = $this->getMockBuilder(Select::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->selectBuilder = new SelectBuilder($this->resourceConnectionMock);
  44. }
  45. public function testCreate()
  46. {
  47. $connectionName = 'MySql';
  48. $from = ['customer c'];
  49. $columns = ['id', 'name', 'price'];
  50. $filter = 'filter';
  51. $joins = [
  52. ['link-type' => 'left', 'table' => 'customer', 'condition' => 'in'],
  53. ['link-type' => 'inner', 'table' => 'price', 'condition' => 'eq'],
  54. ['link-type' => 'right', 'table' => 'attribute', 'condition' => 'neq'],
  55. ];
  56. $groups = ['id', 'name'];
  57. $this->selectBuilder->setConnectionName($connectionName)
  58. ->setFrom($from)
  59. ->setColumns($columns)
  60. ->setFilters([$filter])
  61. ->setJoins($joins)
  62. ->setGroup($groups);
  63. $this->resourceConnectionMock->expects($this->once())
  64. ->method('getConnection')
  65. ->with($connectionName)
  66. ->willReturn($this->connectionMock);
  67. $this->connectionMock->expects($this->once())
  68. ->method('select')
  69. ->willReturn($this->selectMock);
  70. $this->selectMock->expects($this->once())
  71. ->method('from')
  72. ->with($from, []);
  73. $this->selectMock->expects($this->once())
  74. ->method('columns')
  75. ->with($columns);
  76. $this->selectMock->expects($this->once())
  77. ->method('where')
  78. ->with($filter);
  79. $this->selectMock->expects($this->once())
  80. ->method('joinLeft')
  81. ->with($joins[0]['table'], $joins[0]['condition'], []);
  82. $this->selectMock->expects($this->once())
  83. ->method('joinInner')
  84. ->with($joins[1]['table'], $joins[1]['condition'], []);
  85. $this->selectMock->expects($this->once())
  86. ->method('joinRight')
  87. ->with($joins[2]['table'], $joins[2]['condition'], []);
  88. $this->selectBuilder->create();
  89. }
  90. }