SelectBuilderTest.php 3.2 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Analytics\Test\Unit\ReportXml\DB;

use Magento\Analytics\ReportXml\DB\SelectBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;

class SelectBuilderTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var SelectBuilder
     */
    private $selectBuilder;

    /**
     * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
     */
    private $resourceConnectionMock;

    /**
     * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $connectionMock;

    /**
     * @var Select|\PHPUnit_Framework_MockObject_MockObject
     */
    private $selectMock;

    /**
     * @return void
     */
    protected function setUp()
    {
        $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->selectMock = $this->getMockBuilder(Select::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->selectBuilder = new SelectBuilder($this->resourceConnectionMock);
    }

    public function testCreate()
    {
        $connectionName = 'MySql';
        $from = ['customer c'];
        $columns = ['id', 'name', 'price'];
        $filter = 'filter';
        $joins = [
            ['link-type' => 'left', 'table' => 'customer', 'condition' => 'in'],
            ['link-type' => 'inner', 'table' => 'price', 'condition' => 'eq'],
            ['link-type' => 'right', 'table' => 'attribute', 'condition' => 'neq'],
        ];
        $groups = ['id', 'name'];
        $this->selectBuilder->setConnectionName($connectionName)
            ->setFrom($from)
            ->setColumns($columns)
            ->setFilters([$filter])
            ->setJoins($joins)
            ->setGroup($groups);
        $this->resourceConnectionMock->expects($this->once())
            ->method('getConnection')
            ->with($connectionName)
            ->willReturn($this->connectionMock);
        $this->connectionMock->expects($this->once())
            ->method('select')
            ->willReturn($this->selectMock);
        $this->selectMock->expects($this->once())
            ->method('from')
            ->with($from, []);
        $this->selectMock->expects($this->once())
            ->method('columns')
            ->with($columns);
        $this->selectMock->expects($this->once())
            ->method('where')
            ->with($filter);
        $this->selectMock->expects($this->once())
            ->method('joinLeft')
            ->with($joins[0]['table'], $joins[0]['condition'], []);
        $this->selectMock->expects($this->once())
            ->method('joinInner')
            ->with($joins[1]['table'], $joins[1]['condition'], []);
        $this->selectMock->expects($this->once())
            ->method('joinRight')
            ->with($joins[2]['table'], $joins[2]['condition'], []);
        $this->selectBuilder->create();
    }
}