4 use \Symfony\Component\EventDispatcher\EventDispatcher
;
6 require_once 'CiviTest/CiviUnitTestCase.php';
10 class KernelTest
extends \CiviUnitTestCase
{
11 const MOCK_VERSION
= 99;
14 * @var array(int => array('name' => string $eventName, 'type' => string $className))
16 var $actualEventSequence;
19 * @var EventDispatcher
28 protected function setUp() {
30 $this->actualEventSequence
= array();
31 $this->dispatcher
= new EventDispatcher();
32 $this->monitorEvents(Events
::allEvents());
33 $this->kernel
= new Kernel($this->dispatcher
);
36 public function testNormalEvents() {
37 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
38 $result = $this->kernel
->run('Widget', 'frobnicate', array(
39 'version' => self
::MOCK_VERSION
,
42 $expectedEventSequence = array(
43 array('name' => Events
::RESOLVE
, 'class' => 'Civi\API\Event\ResolveEvent'),
44 array('name' => Events
::AUTHORIZE
, 'class' => 'Civi\API\Event\AuthorizeEvent'),
45 array('name' => Events
::PREPARE
, 'class' => 'Civi\API\Event\PrepareEvent'),
46 array('name' => Events
::RESPOND
, 'class' => 'Civi\API\Event\RespondEvent'),
48 $this->assertEquals($expectedEventSequence, $this->actualEventSequence
);
49 $this->assertEquals('frob', $result['values'][98]);
52 public function testResolveException() {
54 $this->dispatcher
->addListener(Events
::RESOLVE
, function () {
55 throw new \
API_Exception('Oh My God', 'omg', array('the' => 'badzes'));
57 $this->dispatcher
->addListener(Events
::EXCEPTION
, function (\Civi\API\Event\ExceptionEvent
$event) use ($test) {
58 $test->assertEquals('Oh My God', $event->getException()->getMessage());
61 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
62 $result = $this->kernel
->run('Widget', 'frobnicate', array(
63 'version' => self
::MOCK_VERSION
,
66 $expectedEventSequence = array(
67 array('name' => Events
::RESOLVE
, 'class' => 'Civi\API\Event\ResolveEvent'),
68 array('name' => Events
::EXCEPTION
, 'class' => 'Civi\API\Event\ExceptionEvent'),
70 $this->assertEquals($expectedEventSequence, $this->actualEventSequence
);
71 $this->assertEquals('Oh My God', $result['error_message']);
72 $this->assertEquals('omg', $result['error_code']);
73 $this->assertEquals('badzes', $result['the']);
76 // TODO testAuthorizeException, testPrepareException, testRespondException, testExceptionException
79 * Create an API provider for entity "Widget" with action "frobnicate".
81 * @return Provider\ProviderInterface
83 public function createWidgetFrobnicateProvider() {
84 $provider = new \Civi\API\Provider\
AdhocProvider(self
::MOCK_VERSION
, 'Widget');
85 $provider->addAction('frobnicate', 'access CiviCRM', function ($apiRequest) {
86 return civicrm_api3_create_success(array(98 => 'frob'));
92 * Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
93 * in $this->actualEventSequence.
95 * @param array $monitoredEvents
96 * List of event names.
99 public function monitorEvents($monitoredEvents) {
100 foreach ($monitoredEvents as $monitoredEvent) {
102 $this->dispatcher
->addListener($monitoredEvent, function ($event) use ($monitoredEvent, &$test) {
103 $test->actualEventSequence
[] = array(
104 'name' => $monitoredEvent,
105 'class' => get_class($event),
107 }, 2 * Events
::W_EARLY
);