Issues (3627)

Tests/Controller/CommonApiControllerTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ApiBundle\Tests\Controller;
13
14
use Mautic\ApiBundle\Controller\CommonApiController;
15
use Mautic\CampaignBundle\Tests\CampaignTestAbstract;
16
use Symfony\Component\HttpFoundation\Request;
17
18
class CommonApiControllerTest extends CampaignTestAbstract
19
{
20
    public function testAddAliasIfNotPresentWithOneColumnWithoutAlias()
21
    {
22
        $result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['dateAdded', 'f']);
23
24
        $this->assertEquals('f.dateAdded', $result);
25
    }
26
27
    public function testAddAliasIfNotPresentWithOneColumnWithDifferentAlias()
28
    {
29
        $result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['s.date_submitted', 'fs']);
30
31
        $this->assertEquals('s.date_submitted', $result);
32
    }
33
34
    public function testAddAliasIfNotPresentWithOneColumnWithAlias()
35
    {
36
        $result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['f.dateAdded', 'f']);
37
38
        $this->assertEquals('f.dateAdded', $result);
39
    }
40
41
    public function testAddAliasIfNotPresentWithTwoColumnsWithAlias()
42
    {
43
        $result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['f.dateAdded, f.dateModified', 'f']);
44
45
        $this->assertEquals('f.dateAdded,f.dateModified', $result);
46
    }
47
48
    public function testAddAliasIfNotPresentWithTwoColumnsWithoutAlias()
49
    {
50
        $result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['dateAdded, dateModified', 'f']);
51
52
        $this->assertEquals('f.dateAdded,f.dateModified', $result);
53
    }
54
55
    public function testgetWhereFromRequestWithNoWhere()
56
    {
57
        $request = $this->getMockBuilder(Request::class)
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
61
        $result = $this->getResultFromProtectedMethod('getWhereFromRequest', [], $request);
62
63
        $this->assertEquals([], $result);
64
    }
65
66
    public function testgetWhereFromRequestWithSomeWhere()
67
    {
68
        $where = [
69
            [
70
                'col'  => 'id',
71
                'expr' => 'eq',
72
                'val'  => 5,
73
            ],
74
        ];
75
76
        $request = $this->getMockBuilder(Request::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

76
        $request = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(Request::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
77
            ->setMethods(['get'])
78
            ->disableOriginalConstructor()
79
            ->getMock();
80
81
        $request->method('get')
82
            ->willReturn($where);
83
84
        $result = $this->getResultFromProtectedMethod('getWhereFromRequest', [], $request);
85
86
        $this->assertEquals($where, $result);
87
    }
88
89
    protected function getResultFromProtectedMethod($method, array $args, Request $request = null)
90
    {
91
        $controller = new CommonApiController();
92
93
        if ($request) {
94
            $controller->setRequest($request);
95
        }
96
97
        $controllerReflection = new \ReflectionClass(CommonApiController::class);
98
        $method               = $controllerReflection->getMethod($method);
99
        $method->setAccessible(true);
100
101
        return $method->invokeArgs($controller, $args);
102
    }
103
}
104