4 require_once 'CiviTest/CiviUnitTestCase.php';
10 class ResolverTest
extends \CiviUnitTestCase
{
20 protected function setUp() {
21 parent
::setUp(); // TODO: Change the autogenerated stub
22 $this->resolver
= new Resolver();
26 * Test callback with a constant value.
28 public function testConstant() {
29 $cb = $this->resolver
->get('0');
30 $actual = call_user_func($cb, 'foo');
31 $this->assertTrue(0 === $actual);
33 $cb = $this->resolver
->get('1');
34 $actual = call_user_func($cb, 'foo');
35 $this->assertTrue(1 === $actual);
39 * Test callback for a global function.
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);
46 $expected = 'global dummy received foo';
47 $actual = call_user_func($cb, 'foo');
48 $this->assertEquals($expected, $actual);
52 * Test callback for a static function.
54 public function testStatic() {
55 $cb = $this->resolver
->get('Civi\Core\ResolverTest::dummy');
56 $this->assertEquals(array('Civi\Core\ResolverTest', 'dummy'), $cb);
58 $expected = 'static dummy received foo';
59 $actual = call_user_func($cb, 'foo');
60 $this->assertEquals($expected, $actual);
64 * Test callback for an API.
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);
75 * Test callback for an object in the container.
77 public function testCall() {
78 // Note: ResolverTestExampleService is implemented at the bottom of this file.
79 Container
::singleton()->set('callbackTestService', new ResolverTestExampleService());
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);
87 * Test callback for an invalid object in the container.
89 * @expectedException \Symfony\Component\DependencyInjection\Exception\ExceptionInterface
91 public function testCallWithInvalidService() {
92 $this->resolver
->get('call://totallyNonexistentService/ping');
96 * Test callback which returns a global variable.
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']);
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']);
115 * Test object-lookup in the container.
117 public function testObj() {
118 // Note: ResolverTestExampleService is implemented at the bottom of this file.
119 Container
::singleton()->set('callbackTestService', new ResolverTestExampleService());
120 $obj = $this->resolver
->get('obj://callbackTestService');
121 $this->assertTrue($obj instanceof ResolverTestExampleService
);
125 * Test object-lookup in the container (invalid name).
127 * @expectedException \Symfony\Component\DependencyInjection\Exception\ExceptionInterface
129 public function testObjWithInvalidService() {
130 $this->resolver
->get('obj://totallyNonexistentService');
134 * Test default object creation.
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
);
143 * @param string $arg1
144 * Dummy value to pass through.
147 public static function dummy($arg1) {
148 return "static dummy received $arg1";
154 * Class ResolverTestExampleService
158 class ResolverTestExampleService
{
161 * @param string $arg1
162 * Dummy value to pass through.
165 public function ping($arg1) {
166 return "service dummy received $arg1";
174 * @param string $arg1
175 * Dummy value to pass through.
178 function civi_core_callback_dummy($arg1) {
179 return "global dummy received $arg1";
183 * @param array $params
187 function civicrm_api3_resolvertest_ping($params) {
188 return civicrm_api3_create_success("api dummy received " . $params['first']);