Answers.Body: <p>I believe events can be raised programmatically by calling them from services. You could mimic this behavior by creating an Event object (<code>$event</code>) and calling calling <code>craft()-&gt;entries-&gt;onBeforeSaveEntry($event)</code> yourself, for example. You would need to set up the event context and create a new Event object as the code does in the Entries Service below.</p>
  
  <pre><code>$event = new Event($this, array(
    'entry'      =&gt; $entry,
    'isNewEntry' =&gt; $isNewEntry
  ));
  
  $this-&gt;onBeforeSaveEntry($event);
  </code></pre>
  
  <p>Check out the <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/EntriesService.php">Entries Service</a> around lines 165 to see how Craft uses events in that context. </p>
  
  <p>For each event call, the code is calling <code>$this-&gt;raiseEvent('eventName', $event);</code></p>
  
  <p>I bet with some minimal setup, you could write a test controller that can call these events at will. Good question - I've been going down the manual path for all my testing too! </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T00:44:47.230Z
Answers.Text: Yeah, I've been doing something very similar to this for some native events. We actually have some custom events raised by our **Sprout Commerce** plugin like `onCheckoutStart` and `onCheckoutEnd` for which this technique gets too involved since you have to fake a lot of models (orders, payments, addresses, etc.) before we can call `SproutCommerce()->onCheckoutStart(Event $event)`.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T00:48:06.670Z
Answers.Text: You know what, it just occurred to me that one could use something like **Faker** https://github.com/fzaninotto/Faker to aid in the process.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T00:49:48.477Z
Answers.Text: Totally! I was thinking of something like that, or writing some assertion testing with PHP Unit. I often use libraries like Faker to spoof AJAX result and database calls.
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T00:38:30.207Z
Answers.LastActivityDate: 2015-02-03T00:38:30.207Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>We use <strong>event</strong> integration (native and custom) in a lot of the plugins we build and I was wondering if anyone has come up with a simple way to raise those events at will, without having to follow a specific flow of execution.</p>
  
  <p>For example, if you are listening for <code>entries.saveEntry</code> and don't want to manually save an entry in order for that event to be raised/triggered.</p>
  
  <p>I think it would be nice to have a simple way of doing this and interested in seeing if anyone has found the need for such thing.</p>
  
  <p>With enough patience, one could fake many events and call the service that raises them but I'm sure that there other developer way more clever than me that may have solved this already.</p>
  
Tags: <plugin-development><events>
Title: How do you raise/fake an event so that you can test handlers listening for it?
ThreadText: # How do you raise/fake an event so that you can test handlers listening for it?
  
  (Asked by: username_0 on 2015-02-03)
  
  We use **event** integration (native and custom) in a lot of the plugins we build and I was wondering if anyone has come up with a simple way to raise those events at will, without having to follow a specific flow of execution.
  
  For example, if you are listening for `entries.saveEntry` and don't want to manually save an entry in order for that event to be raised/triggered.
  
  I think it would be nice to have a simple way of doing this and interested in seeing if anyone has found the need for such thing.
  
  With enough patience, one could fake many events and call the service that raises them but I'm sure that there other developer way more clever than me that may have solved this already.
  
  **Tags:** plugin-development, events
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-02-03. Score: 5
  
  I believe events can be raised programmatically by calling them from services. You could mimic this behavior by creating an Event object (`$event`) and calling calling `craft()->entries->onBeforeSaveEntry($event)` yourself, for example. You would need to set up the event context and create a new Event object as the code does in the Entries Service below.
  
  ```
  $event = new Event($this, array(
    'entry'      => $entry,
    'isNewEntry' => $isNewEntry
  ));
  
  $this->onBeforeSaveEntry($event);
  
  ```
  
  Check out the [Entries Service](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/EntriesService.php) around lines 165 to see how Craft uses events in that context.
  
  For each event call, the code is calling `$this->raiseEvent('eventName', $event);`
  
  I bet with some minimal setup, you could write a test controller that can call these events at will. Good question - I've been going down the manual path for all my testing too!
  
  > **Comments:**
  > **username_0**: Yeah, I've been doing something very similar to this for some native events. We actually have some custom events raised by our **Sprout Commerce** plugin like `onCheckoutStart` and `onCheckoutEnd` for which this technique gets too involved since you have to fake a lot of models (orders, payments, addresses, etc.) before we can call `SproutCommerce()->onCheckoutStart(Event $event)`.  
  > **username_0**: You know what, it just occurred to me that one could use something like **Faker** https://github.com/fzaninotto/Faker to aid in the process.  
  > **username_1**: Totally! I was thinking of something like that, or writing some assertion testing with PHP Unit. I often use libraries like Faker to spoof AJAX result and database calls.  
  
  ---
  
  