[NFC] Fix var declarations in the test folder
[civicrm-core.git] / tests / phpunit / Civi / API / KernelTest.php
1 <?php
2 namespace Civi\API;
3
4 use Symfony\Component\EventDispatcher\EventDispatcher;
5
6 /**
7 */
8 class KernelTest extends \CiviUnitTestCase {
9 const MOCK_VERSION = 3;
10
11 /**
12 * @var array
13 * (int => array('name' => string $eventName, 'type' => string $className))
14 */
15 public $actualEventSequence;
16
17 /**
18 * @var \Symfony\Component\EventDispatcher\EventDispatcher
19 */
20 public $dispatcher;
21
22 /**
23 * @var Kernel
24 */
25 public $kernel;
26
27 protected function setUp() {
28 parent::setUp();
29 $this->actualEventSequence = [];
30 $this->dispatcher = new EventDispatcher();
31 $this->monitorEvents(Events::allEvents());
32 $this->kernel = new Kernel($this->dispatcher);
33 }
34
35 public function testNormalEvents() {
36 $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
37 $result = $this->kernel->run('Widget', 'frobnicate', [
38 'version' => self::MOCK_VERSION,
39 ]);
40
41 $expectedEventSequence = [
42 ['name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'],
43 ['name' => Events::AUTHORIZE, 'class' => 'Civi\API\Event\AuthorizeEvent'],
44 ['name' => Events::PREPARE, 'class' => 'Civi\API\Event\PrepareEvent'],
45 ['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 public function testResolveException() {
52 $test = $this;
53 $this->dispatcher->addListener(Events::RESOLVE, function () {
54 throw new \API_Exception('Oh My God', 'omg', ['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', [
62 'version' => self::MOCK_VERSION,
63 ]);
64
65 $expectedEventSequence = [
66 ['name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'],
67 ['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([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
95 * List of event names.
96 *
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[] = [
103 'name' => $monitoredEvent,
104 'class' => get_class($event),
105 ];
106 }, 2 * Events::W_EARLY);
107 }
108 }
109
110 }