public function testCondition() { $xml = '<event key="core/test" subject="foo"> <call>Jarves\\Tests\\Configuration\\EventTest::fireEvent</call> <condition> <rule key="field1" type="equal">50</rule> <or/> <rule key="field1" type="greater">200</rule> </condition> </event>'; $event = new Event($xml, $this->getJarves()); $this->assertEquals($xml, $event->toXml()); $this->getJarvesEventDispatcher()->detachEvents(); $this->getJarvesEventDispatcher()->attachEvent($event); self::$fired = 0; $this->getEventDispatcher()->dispatch('core/test', new GenericEvent('foo')); $this->assertEquals(0, self::$fired, 'no args, therefore condition not satisfied'); $this->getEventDispatcher()->dispatch('core/test', new GenericEvent('foo', ['field1' => 50])); $this->assertEquals(1, self::$fired); $this->getEventDispatcher()->dispatch('core/test', new GenericEvent('foo', ['field1' => 201])); $this->assertEquals(2, self::$fired); $this->getEventDispatcher()->dispatch('core/test', new GenericEvent('notFoo', ['field1' => 201])); $this->assertEquals(2, self::$fired); $this->getEventDispatcher()->dispatch('core/test', new GenericEvent('foo', ['field1' => 200])); $this->assertEquals(2, self::$fired); $this->getEventDispatcher()->dispatch('core/test', new GenericEvent()); $this->assertEquals(2, self::$fired); $this->getEventDispatcher()->dispatch('core/notTest', new GenericEvent()); $this->assertEquals(2, self::$fired); $this->getJarvesEventDispatcher()->detachEvents(); }
/** * Checks whether a eventConfig is appropriate to be called (subject fits, condition fits) * * @param Event $eventConfig * @param GenericEvent $event * @return bool */ public function isCallable(Event $eventConfig, GenericEvent $event) { if ($eventConfig->getSubject() && $event->getSubject() != $eventConfig->getSubject()) { return false; } if ($eventConfig->getCondition()) { $args = $event->getArguments() ?: []; if ($eventConfig->getCondition() && !$this->conditionOperator->satisfy($eventConfig->getCondition(), $args)) { return false; } } return true; }