Skip to content

Commit 8548cda

Browse files
committed
Add tests
1 parent 24b9072 commit 8548cda

10 files changed

+439
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
build
3+
tests/temp
34
composer.phar
45
composer.lock

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"require-dev": {
2323
"mockery/mockery": "^0.9.5",
24-
"phpunit/phpunit": "4.*"
24+
"phpunit/phpunit": "4.*",
25+
"orchestra/testbench": "^3.3@dev"
2526
},
2627
"autoload": {
2728
"psr-4": {
@@ -30,11 +31,15 @@
3031
},
3132
"autoload-dev": {
3233
"psr-4": {
33-
"NotificationChannels\\:WebPush\\Test\\": "tests"
34+
"NotificationChannels\\WebPush\\Test\\": "tests"
3435
}
3536
},
3637
"config": {
3738
"sort-packages": true
3839
},
39-
"minimum-stability": "dev"
40+
"minimum-stability": "dev",
41+
"prefer-stable": true,
42+
"scripts": {
43+
"test": "phpunit"
44+
}
4045
}

tests/ChannelTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Test;
4+
5+
use Mockery;
6+
use Minishlink\WebPush\WebPush;
7+
use NotificationChannels\WebPush\WebPushChannel;
8+
9+
class ChannelTest extends TestCase
10+
{
11+
/** @var Mockery\Mock */
12+
protected $webPush;
13+
14+
/** @var \NotificationChannels\WebPush\WebPushChannel */
15+
protected $channel;
16+
17+
public function setUp()
18+
{
19+
parent::setUp();
20+
21+
$this->webPush = Mockery::mock(WebPush::class);
22+
23+
$this->channel = new WebPushChannel($this->webPush);
24+
}
25+
26+
public function tearDown()
27+
{
28+
Mockery::close();
29+
parent::tearDown();
30+
}
31+
32+
/** @test */
33+
public function it_can_send_a_notification()
34+
{
35+
$this->webPush->shouldReceive('sendNotification')
36+
->once()
37+
->with('endpoint', $this->getPayload(), 'key', 'token')
38+
->andReturn(true);
39+
40+
$this->webPush->shouldReceive('flush')
41+
->once()
42+
->andReturn(true);
43+
44+
$this->testUser->updatePushSubscription('endpoint', 'key', 'token');
45+
46+
$this->channel->send($this->testUser, new TestNotification);
47+
}
48+
49+
/** @test */
50+
public function it_will_delete_invalid_subscriptions()
51+
{
52+
$this->webPush->shouldReceive('sendNotification')
53+
->once()
54+
->with('valid_endpoint', $this->getPayload(), null, null)
55+
->andReturn(true);
56+
57+
$this->webPush->shouldReceive('sendNotification')
58+
->once()
59+
->with('invalid_endpoint', $this->getPayload(), null, null)
60+
->andReturn(true);
61+
62+
$this->webPush->shouldReceive('flush')
63+
->once()
64+
->andReturn([
65+
['success' => true],
66+
['success' => false]
67+
]);
68+
69+
$this->testUser->updatePushSubscription('valid_endpoint');
70+
$this->testUser->updatePushSubscription('invalid_endpoint');
71+
72+
$this->channel->send($this->testUser, new TestNotification);
73+
74+
$this->assertFalse($this->testUser->pushSubscriptions()->where('endpoint', 'invalid_endpoint')->exists());
75+
76+
$this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'valid_endpoint')->exists());
77+
}
78+
79+
/**
80+
* @return string
81+
*/
82+
protected function getPayload()
83+
{
84+
return '{"id":1,"title":"Title","body":"Body","actions":[{"title":"Title","action":"Action"}],"icon":"Icon"}';
85+
}
86+
}

tests/HasPushSubscriptionsTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

tests/MessageTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Test;
4+
5+
use NotificationChannels\WebPush\WebPushMessage;
6+
7+
class MessageTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/** @var \NotificationChannels\WebPush\WebPushMessage */
10+
protected $message;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->message = new WebPushMessage;
17+
}
18+
19+
/** @test */
20+
public function it_can_accept_a_body_when_constructing_a_message()
21+
{
22+
$message = new WebPushMessage('Message body');
23+
24+
$this->assertEquals('Message body', $message->toArray()['body']);
25+
}
26+
27+
/** @test */
28+
public function it_provides_a_create_method()
29+
{
30+
$message = WebPushMessage::create('Message body');
31+
32+
$this->assertEquals('Message body', $message->toArray()['body']);
33+
}
34+
35+
/** @test */
36+
public function it_can_set_the_title()
37+
{
38+
$this->message->title('Message title');
39+
40+
$this->assertEquals('Message title', $this->message->toArray()['title']);
41+
}
42+
43+
/** @test */
44+
public function it_can_set_the_body()
45+
{
46+
$this->message->body('Message body');
47+
48+
$this->assertEquals('Message body', $this->message->toArray()['body']);
49+
}
50+
51+
/** @test */
52+
public function it_can_set_the_icon()
53+
{
54+
$this->message->icon('Icon');
55+
56+
$this->assertEquals('Icon', $this->message->toArray()['icon']);
57+
}
58+
59+
/** @test */
60+
public function it_can_set_an_action()
61+
{
62+
$this->message->action('Title', 'Action');
63+
64+
$this->assertEquals([['title' => 'Title', 'action' => 'Action']], $this->message->toArray()['actions']);
65+
}
66+
67+
/** @test */
68+
public function it_can_set_the_id()
69+
{
70+
$this->message->id(1);
71+
72+
$this->assertEquals(1, $this->message->toArray()['id']);
73+
}
74+
}

tests/Notifiable.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/PushSubscriptionTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Test;
4+
5+
use NotificationChannels\WebPush\PushSubscription;
6+
7+
class PushSubscriptionTest extends TestCase
8+
{
9+
/** @test */
10+
public function it_has_fillable_attributes()
11+
{
12+
$sub = new PushSubscription([
13+
'endpoint' => 'endpoint',
14+
'public_key' => 'key',
15+
'auth_token' => 'token',
16+
]);
17+
18+
$this->assertEquals('endpoint', $sub->endpoint);
19+
$this->assertEquals('key', $sub->public_key);
20+
$this->assertEquals('token', $sub->auth_token);
21+
}
22+
23+
/** @test */
24+
public function it_can_find_subscription_by_endpoint()
25+
{
26+
$this->testUser->updatePushSubscription('endpoint');
27+
$sub = PushSubscription::findByEndpoint('endpoint');
28+
29+
$this->assertEquals('endpoint', $sub->endpoint);
30+
}
31+
32+
/** @test */
33+
public function it_can_get_user()
34+
{
35+
$this->testUser->updatePushSubscription('endpoint');
36+
$sub = PushSubscription::findByEndpoint('endpoint');
37+
38+
$this->assertEquals($this->testUser->id, $sub->user->id);
39+
}
40+
}

0 commit comments

Comments
 (0)