-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
CommonApiControllerTest.php
103 lines (78 loc) · 3.09 KB
/
CommonApiControllerTest.php
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
<?php
/*
* @copyright 2016 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
*
* @link https://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Mautic\ApiBundle\Tests\Controller;
use Mautic\ApiBundle\Controller\CommonApiController;
use Mautic\CampaignBundle\Tests\CampaignTestAbstract;
use Symfony\Component\HttpFoundation\Request;
class CommonApiControllerTest extends CampaignTestAbstract
{
public function testAddAliasIfNotPresentWithOneColumnWithoutAlias()
{
$result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['dateAdded', 'f']);
$this->assertEquals('f.dateAdded', $result);
}
public function testAddAliasIfNotPresentWithOneColumnWithDifferentAlias()
{
$result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['s.date_submitted', 'fs']);
$this->assertEquals('s.date_submitted', $result);
}
public function testAddAliasIfNotPresentWithOneColumnWithAlias()
{
$result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['f.dateAdded', 'f']);
$this->assertEquals('f.dateAdded', $result);
}
public function testAddAliasIfNotPresentWithTwoColumnsWithAlias()
{
$result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['f.dateAdded, f.dateModified', 'f']);
$this->assertEquals('f.dateAdded,f.dateModified', $result);
}
public function testAddAliasIfNotPresentWithTwoColumnsWithoutAlias()
{
$result = $this->getResultFromProtectedMethod('addAliasIfNotPresent', ['dateAdded, dateModified', 'f']);
$this->assertEquals('f.dateAdded,f.dateModified', $result);
}
public function testgetWhereFromRequestWithNoWhere()
{
$request = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$result = $this->getResultFromProtectedMethod('getWhereFromRequest', [], $request);
$this->assertEquals([], $result);
}
public function testgetWhereFromRequestWithSomeWhere()
{
$where = [
[
'col' => 'id',
'expr' => 'eq',
'val' => 5,
],
];
$request = $this->getMockBuilder(Request::class)
->onlyMethods(['get'])
->disableOriginalConstructor()
->getMock();
$request->method('get')
->willReturn($where);
$result = $this->getResultFromProtectedMethod('getWhereFromRequest', [], $request);
$this->assertEquals($where, $result);
}
protected function getResultFromProtectedMethod($method, array $args, Request $request = null)
{
$controller = new CommonApiController();
if ($request) {
$controller->setRequest($request);
}
$controllerReflection = new \ReflectionClass(CommonApiController::class);
$method = $controllerReflection->getMethod($method);
$method->setAccessible(true);
return $method->invokeArgs($controller, $args);
}
}