4 use Symfony\Component\EventDispatcher\EventDispatcher
;
8 class KernelTest
extends \CiviUnitTestCase
{
9 const MOCK_VERSION
= 3;
13 * (int => array('name' => string $eventName, 'type' => string $className))
15 public $actualEventSequence;
18 * @var \Symfony\Component\EventDispatcher\EventDispatcher
27 protected function setUp() {
29 $this->actualEventSequence
= [];
30 $this->dispatcher
= new EventDispatcher();
31 $this->monitorEvents(Events
::allEvents());
32 $this->kernel
= new Kernel($this->dispatcher
);
35 public function testNormalEvents() {
36 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
37 $result = $this->kernel
->run('Widget', 'frobnicate', [
38 'version' => self
::MOCK_VERSION
,
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'],
47 $this->assertEquals($expectedEventSequence, $this->actualEventSequence
);
48 $this->assertEquals('frob', $result['values'][98]);
51 public function testResolveException() {
53 $this->dispatcher
->addListener(Events
::RESOLVE
, function () {
54 throw new \
API_Exception('Oh My God', 'omg', ['the' => 'badzes']);
56 $this->dispatcher
->addListener(Events
::EXCEPTION
, function (\Civi\API\Event\ExceptionEvent
$event) use ($test) {
57 $test->assertEquals('Oh My God', $event->getException()->getMessage());
60 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
61 $result = $this->kernel
->run('Widget', 'frobnicate', [
62 'version' => self
::MOCK_VERSION
,
65 $expectedEventSequence = [
66 ['name' => Events
::RESOLVE
, 'class' => 'Civi\API\Event\ResolveEvent'],
67 ['name' => Events
::EXCEPTION
, 'class' => 'Civi\API\Event\ExceptionEvent'],
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']);
75 // TODO testAuthorizeException, testPrepareException, testRespondException, testExceptionException
78 * Create an API provider for entity "Widget" with action "frobnicate".
80 * @return Provider\ProviderInterface
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']);
91 * Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
92 * in $this->actualEventSequence.
94 * @param array $monitoredEvents
95 * List of event names.
98 public function monitorEvents($monitoredEvents) {
99 foreach ($monitoredEvents as $monitoredEvent) {
101 $this->dispatcher
->addListener($monitoredEvent, function ($event) use ($monitoredEvent, &$test) {
102 $test->actualEventSequence
[] = [
103 'name' => $monitoredEvent,
104 'class' => get_class($event),
106 }, 2 * Events
::W_EARLY
);