ConditionResolverTest.php 3.52 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 101 102 103 104 105
<?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\ConditionResolver;
use Magento\Analytics\ReportXml\DB\SelectBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Sql\Expression;

class ConditionResolverTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
     */
    private $resourceConnectionMock;

    /**
     * @var ConditionResolver
     */
    private $conditionResolver;

    /**
     * @var SelectBuilder|\PHPUnit_Framework_MockObject_MockObject
     */
    private $selectBuilderMock;

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

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

        $this->selectBuilderMock = $this->getMockBuilder(SelectBuilder::class)
            ->disableOriginalConstructor()
            ->getMock();

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

        $this->conditionResolver = new ConditionResolver($this->resourceConnectionMock);
    }

    public function testGetFilter()
    {
        $condition = ["type" => "variable", "_value" => "1", "attribute" => "id", "operator" => "neq"];
        $valueCondition = ["type" => "value", "_value" => "2", "attribute" => "first_name", "operator" => "eq"];
        $identifierCondition = [
            "type" => "identifier",
            "_value" => "other_field",
            "attribute" => "last_name",
            "operator" => "eq"];
        $filter = [["glue" => "AND", "condition" => [$valueCondition]]];
        $filterConfig = [
            ["glue" => "OR", "condition" => [$condition], 'filter' => $filter],
            ["glue" => "OR", "condition" => [$identifierCondition]],
        ];
        $aliasName = 'n';
        $this->selectBuilderMock->expects($this->any())
            ->method('setParams')
            ->with(array_merge([], [$condition['_value']]));

        $this->selectBuilderMock->expects($this->once())
            ->method('getParams')
            ->willReturn([]);

        $this->selectBuilderMock->expects($this->any())
            ->method('getColumns')
            ->willReturn(['price' => new Expression("(n.price = 400)")]);

        $this->resourceConnectionMock->expects($this->once())
            ->method('getConnection')
            ->willReturn($this->connectionMock);

        $this->connectionMock->expects($this->any())
            ->method('quote')
            ->willReturn("'John'");
        $this->connectionMock->expects($this->exactly(4))
            ->method('quoteIdentifier')
            ->willReturnMap([
                ['n.id', false, '`n`.`id`'],
                ['n.first_name', false, '`n`.`first_name`'],
                ['n.last_name', false, '`n`.`last_name`'],
                ['other_field', false, '`other_field`'],
            ]);

        $result = "(`n`.`id` != 1 OR ((`n`.`first_name` = 'John'))) OR (`n`.`last_name` = `other_field`)";
        $this->assertEquals(
            $result,
            $this->conditionResolver->getFilter($this->selectBuilderMock, $filterConfig, $aliasName)
        );
    }
}