Merge pull request #17197 from eileenmcnaughton/act_r_test
[civicrm-core.git] / tests / phpunit / Civi / Core / ResolverTest.php
CommitLineData
c8074a93
TO
1<?php
2
3namespace Civi\Core {
c8074a93
TO
4
5 /**
6 * Class ResolverTest
7 * @package Civi\Core
8 */
9 class ResolverTest extends \CiviUnitTestCase {
10
11 /**
12 * @var Resolver
13 */
14 protected $resolver;
15
16 /**
17 * Test setup.
18 */
19 protected function setUp() {
39b959db
SL
20 // TODO: Change the autogenerated stub
21 parent::setUp();
c8074a93
TO
22 $this->resolver = new Resolver();
23 }
24
25 /**
26 * Test callback with a constant value.
27 */
28 public function testConstant() {
29 $cb = $this->resolver->get('0');
30 $actual = call_user_func($cb, 'foo');
31 $this->assertTrue(0 === $actual);
32
33 $cb = $this->resolver->get('1');
34 $actual = call_user_func($cb, 'foo');
35 $this->assertTrue(1 === $actual);
36 }
37
38 /**
39 * Test callback for a global function.
40 */
41 public function testGlobalFunc() {
42 // Note: civi_core_callback_dummy is implemented at the bottom of this file.
43 $cb = $this->resolver->get('civi_core_callback_dummy');
44 $this->assertEquals('civi_core_callback_dummy', $cb);
45
46 $expected = 'global dummy received foo';
47 $actual = call_user_func($cb, 'foo');
48 $this->assertEquals($expected, $actual);
49 }
50
51 /**
52 * Test callback for a static function.
53 */
54 public function testStatic() {
55 $cb = $this->resolver->get('Civi\Core\ResolverTest::dummy');
9099cab3 56 $this->assertEquals(['Civi\Core\ResolverTest', 'dummy'], $cb);
c8074a93
TO
57
58 $expected = 'static dummy received foo';
59 $actual = call_user_func($cb, 'foo');
60 $this->assertEquals($expected, $actual);
61 }
62
63 /**
64 * Test callback for an API.
65 */
66 public function testApi3() {
67 // Note: The Resolvertest.Ping API is implemented at the bottom of this file.
68 $cb = $this->resolver->get('api3://Resolvertest/ping?first=@1');
69 $expected = 'api dummy received foo';
70 $actual = call_user_func($cb, 'foo');
71 $this->assertEquals($expected, $actual);
72 }
73
74 /**
75 * Test callback for an object in the container.
76 */
77 public function testCall() {
78 // Note: ResolverTestExampleService is implemented at the bottom of this file.
048222df 79 \Civi::container()->set('callbackTestService', new ResolverTestExampleService());
c8074a93
TO
80 $cb = $this->resolver->get('call://callbackTestService/ping');
81 $expected = 'service dummy received foo';
82 $actual = call_user_func($cb, 'foo');
83 $this->assertEquals($expected, $actual);
84 }
85
86 /**
87 * Test callback for an invalid object in the container.
88 *
89 * @expectedException \Symfony\Component\DependencyInjection\Exception\ExceptionInterface
90 */
91 public function testCallWithInvalidService() {
92 $this->resolver->get('call://totallyNonexistentService/ping');
93 }
94
36781411
TO
95 /**
96 * Test callback which returns a global variable.
97 */
98 public function testGlobalGetter() {
99 $_GET['resolverTest'] = 123;
100 $cb = $this->resolver->get('global://_GET/resolverTest?getter');
101 $_GET['resolverTest'] = 456;
102 $this->assertEquals(456, call_user_func($cb, 'side-effect-free'));
103 $this->assertEquals(456, $_GET['resolverTest']);
104 unset($_GET['resolverTest']);
105 }
106
107 public function testGlobalSetter() {
108 $GLOBALS['resolverTest2'] = 78;
109 $cb = $this->resolver->get('global://resolverTest2?setter');
110 call_user_func($cb, 90);
111 $this->assertEquals(90, $GLOBALS['resolverTest2']);
112 }
113
c8074a93
TO
114 /**
115 * Test object-lookup in the container.
116 */
117 public function testObj() {
118 // Note: ResolverTestExampleService is implemented at the bottom of this file.
048222df 119 \Civi::container()->set('callbackTestService', new ResolverTestExampleService());
c8074a93
TO
120 $obj = $this->resolver->get('obj://callbackTestService');
121 $this->assertTrue($obj instanceof ResolverTestExampleService);
122 }
123
124 /**
125 * Test object-lookup in the container (invalid name).
126 *
127 * @expectedException \Symfony\Component\DependencyInjection\Exception\ExceptionInterface
128 */
129 public function testObjWithInvalidService() {
130 $this->resolver->get('obj://totallyNonexistentService');
131 }
132
133 /**
134 * Test default object creation.
135 */
136 public function testClass() {
137 // Note: ResolverTestExampleService is implemented at the bottom of this file.
138 $obj = $this->resolver->get('Civi\Core\ResolverTestExampleService');
139 $this->assertTrue($obj instanceof ResolverTestExampleService);
140 }
141
142 /**
143 * @param string $arg1
144 * Dummy value to pass through.
145 * @return array
146 */
147 public static function dummy($arg1) {
148 return "static dummy received $arg1";
149 }
150
151 }
152
153 /**
154 * Class ResolverTestExampleService
155 *
156 * @package Civi\Core
157 */
158 class ResolverTestExampleService {
159
160 /**
161 * @param string $arg1
162 * Dummy value to pass through.
163 * @return string
164 */
165 public function ping($arg1) {
166 return "service dummy received $arg1";
167 }
168
169 }
170}
171
172namespace {
39b959db 173
c8074a93
TO
174 /**
175 * @param string $arg1
176 * Dummy value to pass through.
177 * @return string
178 */
179 function civi_core_callback_dummy($arg1) {
180 return "global dummy received $arg1";
181 }
182
183 /**
184 * @param array $params
185 * API parameters.
186 * @return array
187 */
188 function civicrm_api3_resolvertest_ping($params) {
189 return civicrm_api3_create_success("api dummy received " . $params['first']);
190 }
39b959db 191
c8074a93 192}