/**
  * Tests the type detector methods
  *
  * @dataProvider typesProvider
  * @return void
  */
 public function testDetectors($type)
 {
     $event = new SendgridEvent();
     $event->set(array('event' => $type, 'email' => '*****@*****.**'));
     $this->assertTrue($event->{'is' . ucFirst($type)}());
     $event->type = 'foo';
     $this->assertFalse($event->{'is' . ucFirst($type)}());
 }
    /**
     * Tests handling a batch json request
     *
     * @return void
     */
    public function testHandleMultiple()
    {
        $dispatcher = new SendgridWebhookDispatcher();
        $request = $this->getMock('CakeRequest', array('is', 'input'));
        $response = new CakeResponse();
        $callback = $this->getMock('stdClass', array('__invoke'));
        Configure::write('Dispatcher.filters.GridHook', array('handler' => $callback));
        $data = array('event' => 'delivered', 'email' => '*****@*****.**', 'timestamp' => 1322000095, 'unique_arg' => 'my unique arg');
        $builtEvent1 = new SendgridEvent();
        $builtEvent1->set($data);
        $data = array('event' => 'open', 'email' => '*****@*****.**', 'timestamp' => 1322000096, 'unique_arg' => 'my unique arg');
        $builtEvent2 = new SendgridEvent();
        $builtEvent2->set($data);
        $raw = <<<e
\t\t{"email":"*****@*****.**","timestamp":1322000095,"unique_arg":"my unique arg","event":"delivered"}
\t\t{"email":"*****@*****.**","timestamp":1322000096,"unique_arg":"my unique arg","event":"open"}
e;
        $callback->expects($this->at(0))->method('__invoke')->with($builtEvent1);
        $callback->expects($this->at(1))->method('__invoke')->with($builtEvent2);
        $request->expects($this->once())->method('is')->with('post')->will($this->returnValue(true));
        $request->expects($this->once())->method('input')->will($this->returnValue($raw));
        $request->url = 'webhook/sendgrid';
        $_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';
        $event = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response'));
        $this->assertSame($response, $dispatcher->beforeDispatch($event));
        $this->assertEquals(200, $response->statusCode());
        $this->assertTrue($event->isStopped());
    }