3 use \Symfony\Component\EventDispatcher\EventDispatcher
;
5 require_once 'CiviTest/CiviUnitTestCase.php';
9 class KernelTest
extends \CiviUnitTestCase
{
10 const MOCK_VERSION
= 99;
13 * @var array(int => array('name' => string $eventName, 'type' => string $className))
15 var $actualEventSequence;
18 * @var EventDispatcher
27 protected function setUp() {
29 $this->actualEventSequence
= array();
30 $this->dispatcher
= new EventDispatcher();
31 $this->monitorEvents(Events
::allEvents());
32 $this->kernel
= new Kernel($this->dispatcher
);
35 function testNormalEvents() {
36 $this->kernel
->registerApiProvider($this->createWidgetFrobnicateProvider());
37 $result = $this->kernel
->run('Widget', 'frobnicate', array(
38 'version' => self
::MOCK_VERSION
,
41 $expectedEventSequence = array(
42 array('name' => Events
::RESOLVE
, 'class' => 'Civi\API\Event\ResolveEvent'),
43 array('name' => Events
::AUTHORIZE
, 'class' => 'Civi\API\Event\AuthorizeEvent'),
44 array('name' => Events
::PREPARE
, 'class' => 'Civi\API\Event\PrepareEvent'),
45 array('name' => Events
::RESPOND
, 'class' => 'Civi\API\Event\RespondEvent'),
47 $this->assertEquals($expectedEventSequence, $this->actualEventSequence
);
48 $this->assertEquals('frob', $result['values'][98]);
51 function testResolveException() {
53 $this->dispatcher
->addListener(Events
::RESOLVE
, function () {
54 throw new \
API_Exception('Oh My God', 'omg', array('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', array(
62 'version' => self
::MOCK_VERSION
,
65 $expectedEventSequence = array(
66 array('name' => Events
::RESOLVE
, 'class' => 'Civi\API\Event\ResolveEvent'),
67 array('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(array(98 => 'frob'));
91 * Add listeners to $this->dispatcher which record each invocation of $monitoredEvents
92 * in $this->actualEventSequence.
94 * @param EventDispatcher $this->dispatcher
95 * @param array $monitoredEvents 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
);