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