Merge pull request #3749 from civicrm/4.4
[civicrm-core.git] / tests / phpunit / Civi / API / KernelTest.php
1 <?php
2 namespace Civi\API;
3 use \Symfony\Component\EventDispatcher\EventDispatcher;
4
5 require_once 'CiviTest/CiviUnitTestCase.php';
6
7 /**
8 */
9 class KernelTest extends \CiviUnitTestCase {
10 const MOCK_VERSION = 99;
11
12 /**
13 * @var array(int => array('name' => string $eventName, 'type' => string $className))
14 */
15 var $actualEventSequence;
16
17 /**
18 * @var EventDispatcher
19 */
20 var $dispatcher;
21
22 /**
23 * @var Kernel
24 */
25 var $kernel;
26
27 protected function setUp() {
28 parent::setUp();
29 $this->actualEventSequence = array();
30 $this->dispatcher = new EventDispatcher();
31 $this->monitorEvents(Events::allEvents());
32 $this->kernel = new Kernel($this->dispatcher);
33 }
34
35 function testNormalEvents() {
36 $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
37 $result = $this->kernel->run('Widget', 'frobnicate', array(
38 'version' => self::MOCK_VERSION,
39 ));
40
41 $expectedEventSequence = array(
42 array('name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'),
43 array('name' => Events::AUTHORIZE, 'class' => 'Civi\API\Event\AuthorizeEvent'),
44 array('name' => Events::PREPARE, 'class' => 'Civi\API\Event\PrepareEvent'),
45 array('name' => Events::RESPOND, 'class' => 'Civi\API\Event\RespondEvent'),
46 );
47 $this->assertEquals($expectedEventSequence, $this->actualEventSequence);
48 $this->assertEquals('frob', $result['values'][98]);
49 }
50
51 function testResolveException() {
52 $test = $this;
53 $this->dispatcher->addListener(Events::RESOLVE, function () {
54 throw new \API_Exception('Oh My God', 'omg', array('the' => 'badzes'));
55 }, Events::W_EARLY);
56 $this->dispatcher->addListener(Events::EXCEPTION, function(\Civi\API\Event\ExceptionEvent $event) use ($test) {
57 $test->assertEquals('Oh My God', $event->getException()->getMessage());
58 });
59
60 $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
61 $result = $this->kernel->run('Widget', 'frobnicate', array(
62 'version' => self::MOCK_VERSION,
63 ));
64
65 $expectedEventSequence = array(
66 array('name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'),
67 array('name' => Events::EXCEPTION, 'class' => 'Civi\API\Event\ExceptionEvent'),
68 );
69 $this->assertEquals($expectedEventSequence, $this->actualEventSequence);
70 $this->assertEquals('Oh My God', $result['error_message']);
71 $this->assertEquals('omg', $result['error_code']);
72 $this->assertEquals('badzes', $result['the']);
73 }
74
75 // TODO testAuthorizeException, testPrepareException, testRespondException, testExceptionException
76
77 /**
78 * Create an API provider for entity "Widget" with action "frobnicate".
79 *
80 * @return Provider\ProviderInterface
81 */
82 public function createWidgetFrobnicateProvider() {
83 $provider = new \Civi\API\Provider\AdhocProvider(self::MOCK_VERSION, 'Widget');
84 $provider->addAction('frobnicate', 'access CiviCRM', function ($apiRequest) {
85 return civicrm_api3_create_success(array(98 => 'frob'));
86 });
87 return $provider;
88 }
89
90 /**
91 * Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
92 * in $this->actualEventSequence.
93 *
94 * @param array $monitoredEvents list of event names
95 *
96 * @internal param \Symfony\Component\EventDispatcher\EventDispatcher $this ->dispatcher
97 */
98 public function monitorEvents($monitoredEvents) {
99 foreach ($monitoredEvents as $monitoredEvent) {
100 $test = $this;
101 $this->dispatcher->addListener($monitoredEvent, function ($event) use ($monitoredEvent, &$test) {
102 $test->actualEventSequence[] = array(
103 'name' => $monitoredEvent,
104 'class' => get_class($event),
105 );
106 }, 2 * Events::W_EARLY);
107 }
108 }
109 }