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