* @author Graham Campbell */ class SubscriberTest extends AbstractApiTestCase { public function testGetSubscribersUnauthenticated() { $this->get('/api/v1/subscribers'); $this->assertResponseStatus(401); $this->seeHeader('Content-Type', 'application/json'); } public function testGetSubscribers() { $this->beUser(); $subscriber = factory('CachetHQ\Cachet\Models\Subscriber')->create(); $this->get('/api/v1/subscribers'); $this->seeHeader('Content-Type', 'application/json'); $this->assertResponseOk(); } public function testCreateSubscriber() { $this->beUser(); Notification::fake(); $this->expectsEvents('CachetHQ\Cachet\Bus\Events\Subscriber\SubscriberHasSubscribedEvent'); $this->post('/api/v1/subscribers', [ 'email' => 'support@alt-three.com', ]); $this->assertResponseOk(); $this->seeHeader('Content-Type', 'application/json'); $this->seeJsonContains(['email' => 'support@alt-three.com']); } public function testCreateSubscriberAutoVerified() { $this->beUser(); Notification::fake(); $this->expectsEvents('CachetHQ\Cachet\Bus\Events\Subscriber\SubscriberHasSubscribedEvent'); $this->post('/api/v1/subscribers', [ 'email' => 'support@alt-three.com', 'verify' => true, ]); $this->assertResponseOk(); $this->seeHeader('Content-Type', 'application/json'); $this->seeJsonContains(['email' => 'support@alt-three.com']); } public function testCreateSubscriberWithSubscriptions() { $this->beUser(); factory('CachetHQ\Cachet\Models\Component', 3)->create(); $this->post('/api/v1/subscribers', [ 'email' => 'support@alt-three.com', 'verify' => true, 'components' => [ 1, 3, ], ]); $this->assertResponseOk(); $this->seeHeader('Content-Type', 'application/json'); $this->seeJsonContains(['email' => 'support@alt-three.com']); $this->seeJsonStructure(['data' => ['subscriptions' => []]]); $data = $this->decodeResponseJson(); $this->assertCount(2, $data['data']['subscriptions']); $this->assertEquals(1, $data['data']['subscriptions'][0]['component_id']); $this->assertEquals(3, $data['data']['subscriptions'][1]['component_id']); } public function testDeleteSubscriber() { $this->beUser(); $subscriber = factory('CachetHQ\Cachet\Models\Subscriber')->create(); $this->delete("/api/v1/subscribers/{$subscriber->id}"); $this->assertResponseStatus(204); } public function testDeleteSubscription() { $this->beUser(); $subscription = factory('CachetHQ\Cachet\Models\Subscription')->create(); $this->delete("/api/v1/subscriptions/{$subscription->id}"); $this->assertResponseStatus(204); } }