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