4 use \Symfony\Component\EventDispatcher\EventDispatcher
;
8 class KernelTest
extends \CiviUnitTestCase
{
9 const MOCK_VERSION
= 3;
12 * @var array(int => array('name' => string $eventName, 'type' => string $className))
14 var $actualEventSequence;
17 * @var EventDispatcher
26 protected function setUp() {
28 $this->actualEventSequence
= array();
29 $this->dispatcher
= new EventDispatcher();
30 $this->monitorEvents(Events
::allEvents());
31 $this->kernel
= new Kernel($this->dispatcher
);
34 public function testNormalEvents() {
35 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
36 $result = $this->kernel
->run('Widget', 'frobnicate', array(
37 'version' => self
::MOCK_VERSION
,
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'),
46 $this->assertEquals($expectedEventSequence, $this->actualEventSequence
);
47 $this->assertEquals('frob', $result['values'][98]);
50 public function testResolveException() {
52 $this->dispatcher
->addListener(Events
::RESOLVE
, function () {
53 throw new \
API_Exception('Oh My God', 'omg', array('the' => 'badzes'));
55 $this->dispatcher
->addListener(Events
::EXCEPTION
, function (\Civi\API\Event\ExceptionEvent
$event) use ($test) {
56 $test->assertEquals('Oh My God', $event->getException()->getMessage());
59 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
60 $result = $this->kernel
->run('Widget', 'frobnicate', array(
61 'version' => self
::MOCK_VERSION
,
64 $expectedEventSequence = array(
65 array('name' => Events
::RESOLVE
, 'class' => 'Civi\API\Event\ResolveEvent'),
66 array('name' => Events
::EXCEPTION
, 'class' => 'Civi\API\Event\ExceptionEvent'),
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']);
74 // TODO testAuthorizeException, testPrepareException, testRespondException, testExceptionException
77 * Create an API provider for entity "Widget" with action "frobnicate".
79 * @return Provider\ProviderInterface
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'));
90 * Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
91 * in $this->actualEventSequence.
93 * @param array $monitoredEvents
94 * List of event names.
97 public function monitorEvents($monitoredEvents) {
98 foreach ($monitoredEvents as $monitoredEvent) {
100 $this->dispatcher
->addListener($monitoredEvent, function ($event) use ($monitoredEvent, &$test) {
101 $test->actualEventSequence
[] = array(
102 'name' => $monitoredEvent,
103 'class' => get_class($event),
105 }, 2 * Events
::W_EARLY
);