|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NotificationChannels\WebPush\Test; |
| 4 | + |
| 5 | +class HasPushSubscriptionsTest extends TestCase |
| 6 | +{ |
| 7 | + /** @test */ |
| 8 | + public function it_can_get_user_subscriptions() |
| 9 | + { |
| 10 | + $this->createSubscription($this->testUser, 'foo'); |
| 11 | + $this->createSubscription($this->testUser, 'bar'); |
| 12 | + |
| 13 | + $this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'foo')->exists()); |
| 14 | + |
| 15 | + $this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'bar')->exists()); |
| 16 | + |
| 17 | + $this->assertEquals(2, count($this->testUser->routeNotificationForWebPush())); |
| 18 | + } |
| 19 | + |
| 20 | + /** @test */ |
| 21 | + public function it_can_create_a_new_subscription() |
| 22 | + { |
| 23 | + $this->testUser->updatePushSubscription('foo', 'key', 'token'); |
| 24 | + $sub = $this->testUser->pushSubscriptions()->first(); |
| 25 | + |
| 26 | + $this->assertEquals('foo', $sub->endpoint); |
| 27 | + |
| 28 | + $this->assertEquals('key', $sub->public_key); |
| 29 | + |
| 30 | + $this->assertEquals('token', $sub->auth_token); |
| 31 | + } |
| 32 | + |
| 33 | + /** @test */ |
| 34 | + public function it_can_update_an_exsiting_subscription_by_endpoint() |
| 35 | + { |
| 36 | + $this->testUser->updatePushSubscription('foo', 'key', 'token'); |
| 37 | + $this->testUser->updatePushSubscription('foo', 'major-key', 'another-token'); |
| 38 | + $subs = $this->testUser->pushSubscriptions()->where('endpoint', 'foo')->get(); |
| 39 | + |
| 40 | + $this->assertEquals(1, count($subs)); |
| 41 | + |
| 42 | + $this->assertEquals('major-key', $subs[0]->public_key); |
| 43 | + |
| 44 | + $this->assertEquals('another-token', $subs[0]->auth_token); |
| 45 | + } |
| 46 | + |
| 47 | + /** @test */ |
| 48 | + public function it_can_determinte_if_a_subscription_belongs_to_a_user() |
| 49 | + { |
| 50 | + $sub = $this->testUser->updatePushSubscription('foo'); |
| 51 | + |
| 52 | + $this->assertTrue($this->testUser->pushSubscriptionBelongsToUser($sub)); |
| 53 | + } |
| 54 | + |
| 55 | + /** @test */ |
| 56 | + public function it_will_delete_a_subscription_that_belongs_to_another_user_and_save_it_for_the_new_user() |
| 57 | + { |
| 58 | + $otherUser = $this-> createUser([ 'email' => '[email protected]']); |
| 59 | + $otherUser->updatePushSubscription('foo'); |
| 60 | + $this->testUser->updatePushSubscription('foo'); |
| 61 | + |
| 62 | + $this->assertEquals(0, count($otherUser->pushSubscriptions)); |
| 63 | + |
| 64 | + $this->assertEquals(1, count($this->testUser->pushSubscriptions)); |
| 65 | + } |
| 66 | + |
| 67 | + public function it_will_delete_a_subscription_by_endpoint() |
| 68 | + { |
| 69 | + $this->testUser->updatePushSubscription('foo'); |
| 70 | + $this->testUser->deletePushSubscription('foo'); |
| 71 | + |
| 72 | + $this->assertEquals(0, count($this->testUser->pushSubscriptions)); |
| 73 | + } |
| 74 | +} |
0 commit comments