Merge pull request #10650 from jitendrapurohit/CRM-20861
[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(int => array('name' => string $eventName, 'type' => string $className))
13 */
14 var $actualEventSequence;
15
16 /**
17 * @var EventDispatcher
18 */
19 var $dispatcher;
20
21 /**
22 * @var Kernel
23 */
24 var $kernel;
25
26 protected function setUp() {
27 parent::setUp();
28 $this->actualEventSequence = array();
29 $this->dispatcher = new EventDispatcher();
30 $this->monitorEvents(Events::allEvents());
31 $this->kernel = new Kernel($this->dispatcher);
32 }
33
34 public function testNormalEvents() {
35 $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
36 $result = $this->kernel->run('Widget', 'frobnicate', array(
37 'version' => self::MOCK_VERSION,
38 ));
39
40 $expectedEventSequence = array(
41 array('name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'),
42 array('name' => Events::AUTHORIZE, 'class' => 'Civi\API\Event\AuthorizeEvent'),
43 array('name' => Events::PREPARE, 'class' => 'Civi\API\Event\PrepareEvent'),
44 array('name' => Events::RESPOND, 'class' => 'Civi\API\Event\RespondEvent'),
45 );
46 $this->assertEquals($expectedEventSequence, $this->actualEventSequence);
47 $this->assertEquals('frob', $result['values'][98]);
48 }
49
50 public function testResolveException() {
51 $test = $this;
52 $this->dispatcher->addListener(Events::RESOLVE, function () {
53 throw new \API_Exception('Oh My God', 'omg', array('the' => 'badzes'));
54 }, Events::W_EARLY);
55 $this->dispatcher->addListener(Events::EXCEPTION, function (\Civi\API\Event\ExceptionEvent $event) use ($test) {
56 $test->assertEquals('Oh My God', $event->getException()->getMessage());
57 });
58
59 $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
60 $result = $this->kernel->run('Widget', 'frobnicate', array(
61 'version' => self::MOCK_VERSION,
62 ));
63
64 $expectedEventSequence = array(
65 array('name' => Events::RESOLVE, 'class' => 'Civi\API\Event\ResolveEvent'),
66 array('name' => Events::EXCEPTION, 'class' => 'Civi\API\Event\ExceptionEvent'),
67 );
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) {
84 return civicrm_api3_create_success(array(98 => 'frob'));
85 });
86 return $provider;
87 }
88
89 /**
90 * Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
91 * in $this->actualEventSequence.
92 *
93 * @param array $monitoredEvents
94 * List of event names.
95 *
96 */
97 public function monitorEvents($monitoredEvents) {
98 foreach ($monitoredEvents as $monitoredEvent) {
99 $test = $this;
100 $this->dispatcher->addListener($monitoredEvent, function ($event) use ($monitoredEvent, &$test) {
101 $test->actualEventSequence[] = array(
102 'name' => $monitoredEvent,
103 'class' => get_class($event),
104 );
105 }, 2 * Events::W_EARLY);
106 }
107 }
108
109 }