Skip to content

Commit dcc3b2c

Browse files
committed
Refactor tests
1 parent f32e186 commit dcc3b2c

8 files changed

+104
-169
lines changed

src/PushSubscription.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7+
/**
8+
* @property string $endpoint
9+
* @property string|null $public_key
10+
* @property string|null $auth_token
11+
* @property string|null $content_encoding
12+
* @property \Illuminate\Database\Eloquent\Model $subscribable
13+
*/
714
class PushSubscription extends Model
815
{
916
/**

tests/ChannelTest.php

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
use Mockery;
66
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
78
use Minishlink\WebPush\WebPush;
89
use Minishlink\WebPush\Subscription;
910
use Minishlink\WebPush\MessageSentReport;
10-
use NotificationChannels\WebPush\WebPushChannel;
1111
use NotificationChannels\WebPush\ReportHandler;
12-
use GuzzleHttp\Psr7\Response;
12+
use NotificationChannels\WebPush\WebPushChannel;
1313

1414
class ChannelTest extends TestCase
1515
{
16-
/** @var Mockery\Mock */
16+
/** @var \Mockery\MockInterface */
1717
protected $webPush;
1818

1919
/** @var \NotificationChannels\WebPush\WebPushChannel */
@@ -24,28 +24,24 @@ public function setUp(): void
2424
parent::setUp();
2525

2626
$this->webPush = Mockery::mock(WebPush::class);
27-
2827
$this->channel = new WebPushChannel($this->webPush, new ReportHandler);
2928
}
3029

31-
public function tearDown(): void
32-
{
33-
Mockery::close();
34-
parent::tearDown();
35-
}
36-
3730
/** @test */
38-
public function it_can_send_a_notification()
31+
public function notification_can_be_sent()
3932
{
33+
$message = ($notification = new TestNotification)->toWebPush(null, null);
34+
4035
$this->webPush->shouldReceive('sendNotification')
4136
->once()
42-
->withArgs(function (Subscription $subscription, string $payload, bool $flush = false, array $options = []) {
43-
$this->assertSame($this->getPayload(), $payload);
37+
->withArgs(function (Subscription $subscription, string $payload, bool $flush, array $options = []) use ($message) {
4438
$this->assertInstanceOf(Subscription::class, $subscription);
4539
$this->assertEquals('endpoint', $subscription->getEndpoint());
4640
$this->assertEquals('key', $subscription->getPublicKey());
4741
$this->assertEquals('token', $subscription->getAuthToken());
4842
$this->assertEquals('aesgcm', $subscription->getContentEncoding());
43+
$this->assertSame($message->getOptions(), $options);
44+
$this->assertSame(json_encode($message->toArray()), $payload);
4945

5046
return true;
5147
})
@@ -59,78 +55,31 @@ public function it_can_send_a_notification()
5955

6056
$this->testUser->updatePushSubscription('endpoint', 'key', 'token', 'aesgcm');
6157

62-
$this->channel->send($this->testUser, new TestNotification);
63-
64-
$this->assertTrue(true);
58+
$this->channel->send($this->testUser, $notification);
6559
}
6660

6761
/** @test */
68-
public function it_can_send_a_notification_with_options()
62+
public function subscriptions_with_invalid_endpoint_are_deleted()
6963
{
7064
$this->webPush->shouldReceive('sendNotification')
71-
->once()
72-
->withArgs(function ($subscription, $payload, $flush, array $options = []) {
73-
$this->assertSame(['ttl' => 60], $options);
74-
75-
return true;
76-
})
77-
->andReturn(true);
78-
79-
$this->webPush->shouldReceive('flush')
80-
->once()
81-
->andReturn((function () {
82-
yield new MessageSentReport(new Request('POST', 'endpoint'), null, true);
83-
})());
84-
85-
$this->testUser->updatePushSubscription('endpoint', 'key', 'token');
86-
87-
$this->channel->send($this->testUser, new TestNotificationWithOptions);
88-
89-
$this->assertTrue(true);
90-
}
91-
92-
/** @test */
93-
public function it_will_delete_invalid_subscriptions()
94-
{
95-
$this->webPush->shouldReceive('sendNotification')
96-
->once()
97-
// ->with('valid_endpoint', $this->getPayload(), null, null, [])
98-
->andReturn(true);
99-
100-
$this->webPush->shouldReceive('sendNotification')
101-
->once()
102-
// ->with('invalid_endpoint', $this->getPayload(), null, null, [])
103-
->andReturn(true);
65+
->times(3);
10466

10567
$this->webPush->shouldReceive('flush')
10668
->once()
10769
->andReturn((function () {
10870
yield new MessageSentReport(new Request('POST', 'valid_endpoint'), new Response(200), true);
109-
yield new MessageSentReport(new Request('POST', 'invalid_endpoint'), new Response(404), false);
71+
yield new MessageSentReport(new Request('POST', 'invalid_endpoint2'), new Response(404), false);
72+
yield new MessageSentReport(new Request('POST', 'invalid_endpoint1'), new Response(410), false);
11073
})());
11174

11275
$this->testUser->updatePushSubscription('valid_endpoint');
113-
$this->testUser->updatePushSubscription('invalid_endpoint');
76+
$this->testUser->updatePushSubscription('invalid_endpoint1');
77+
$this->testUser->updatePushSubscription('invalid_endpoint2');
11478

11579
$this->channel->send($this->testUser, new TestNotification);
11680

117-
$this->assertFalse($this->testUser->pushSubscriptions()->where('endpoint', 'invalid_endpoint')->exists());
11881
$this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'valid_endpoint')->exists());
119-
}
120-
121-
/**
122-
* @return string
123-
*/
124-
protected function getPayload()
125-
{
126-
return json_encode([
127-
'title' => 'Title',
128-
'actions' => [
129-
['title' => 'Title', 'action' => 'Action'],
130-
],
131-
'body' => 'Body',
132-
'icon' => 'Icon',
133-
'data' => ['id' => 1],
134-
]);
82+
$this->assertFalse($this->testUser->pushSubscriptions()->where('endpoint', 'invalid_endpoint1')->exists());
83+
$this->assertFalse($this->testUser->pushSubscriptions()->where('endpoint', 'invalid_endpoint2')->exists());
13584
}
13685
}

tests/HasPushSubscriptionsTest.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,61 @@
55
class HasPushSubscriptionsTest extends TestCase
66
{
77
/** @test */
8-
public function it_can_get_user_subscriptions()
8+
public function model_has_subscriptions()
99
{
1010
$this->createSubscription($this->testUser, 'foo');
1111
$this->createSubscription($this->testUser, 'bar');
1212

13+
$this->assertEquals(2, count($this->testUser->routeNotificationForWebPush()));
1314
$this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'foo')->exists());
14-
1515
$this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'bar')->exists());
16-
17-
$this->assertEquals(2, count($this->testUser->routeNotificationForWebPush()));
1816
}
1917

2018
/** @test */
21-
public function it_can_create_a_new_subscription()
19+
public function subscription_can_be_created()
2220
{
23-
$this->testUser->updatePushSubscription('foo', 'key', 'token');
24-
$sub = $this->testUser->pushSubscriptions()->first();
25-
26-
$this->assertEquals('foo', $sub->endpoint);
21+
$this->testUser->updatePushSubscription('foo', 'key', 'token', 'aesgcm');
22+
$subscription = $this->testUser->pushSubscriptions()->first();
2723

28-
$this->assertEquals('key', $sub->public_key);
29-
30-
$this->assertEquals('token', $sub->auth_token);
24+
$this->assertEquals('foo', $subscription->endpoint);
25+
$this->assertEquals('key', $subscription->public_key);
26+
$this->assertEquals('token', $subscription->auth_token);
27+
$this->assertEquals('aesgcm', $subscription->content_encoding);
3128
}
3229

3330
/** @test */
34-
public function it_can_update_an_exsiting_subscription_by_endpoint()
31+
public function exiting_subscription_can_be_updated_by_endpoint()
3532
{
3633
$this->testUser->updatePushSubscription('foo', 'key', 'token');
3734
$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);
35+
$subscriptions = $this->testUser->pushSubscriptions()->where('endpoint', 'foo')->get();
4336

44-
$this->assertEquals('another-token', $subs[0]->auth_token);
37+
$this->assertEquals(1, count($subscriptions));
38+
$this->assertEquals('major-key', $subscriptions[0]->public_key);
39+
$this->assertEquals('another-token', $subscriptions[0]->auth_token);
4540
}
4641

4742
/** @test */
48-
public function it_can_determinte_if_a_subscription_belongs_to_a_user()
43+
public function determinte_if_model_owns_subscription()
4944
{
50-
$sub = $this->testUser->updatePushSubscription('foo');
45+
$subscription = $this->testUser->updatePushSubscription('foo');
5146

52-
$this->assertTrue($this->testUser->ownsPushSubscription($sub));
47+
$this->assertTrue($this->testUser->ownsPushSubscription($subscription));
5348
}
5449

5550
/** @test */
56-
public function it_will_delete_a_subscription_that_belongs_to_another_user_and_save_it_for_the_new_user()
51+
public function subscription_owned_by_another_model_is_deleted_and_saved_for_the_new_model()
5752
{
5853
$otherUser = $this->createUser(['email' => '[email protected]']);
5954
$otherUser->updatePushSubscription('foo');
6055
$this->testUser->updatePushSubscription('foo');
6156

6257
$this->assertEquals(0, count($otherUser->pushSubscriptions));
63-
6458
$this->assertEquals(1, count($this->testUser->pushSubscriptions));
6559
}
6660

67-
public function it_will_delete_a_subscription_by_endpoint()
61+
/** @test */
62+
public function subscription_can_be_deleted_by_endpoint()
6863
{
6964
$this->testUser->updatePushSubscription('foo');
7065
$this->testUser->deletePushSubscription('foo');

tests/MessageTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,111 +18,111 @@ public function setUp(): void
1818
}
1919

2020
/** @test */
21-
public function can_set_title()
21+
public function title_can_be_set()
2222
{
2323
$this->message->title('Message title');
2424

2525
$this->assertEquals('Message title', $this->message->toArray()['title']);
2626
}
2727

2828
/** @test */
29-
public function can_set_an_action()
29+
public function action_can_be_set()
3030
{
3131
$this->message->action('Some Action', 'some_action');
3232

3333
$this->assertEquals([['title' => 'Some Action', 'action' => 'some_action']], $this->message->toArray()['actions']);
3434
}
3535

3636
/** @test */
37-
public function can_set_badge()
37+
public function badge_can_be_set()
3838
{
3939
$this->message->badge('/badge.jpg');
4040

4141
$this->assertEquals('/badge.jpg', $this->message->toArray()['badge']);
4242
}
4343

4444
/** @test */
45-
public function can_set_body()
45+
public function body_can_be_set()
4646
{
4747
$this->message->body('Message body');
4848

4949
$this->assertEquals('Message body', $this->message->toArray()['body']);
5050
}
5151

5252
/** @test */
53-
public function can_set_direction()
53+
public function direction_can_be_set()
5454
{
5555
$this->message->dir('rtl');
5656

5757
$this->assertEquals('rtl', $this->message->toArray()['dir']);
5858
}
5959

6060
/** @test */
61-
public function can_set_icon()
61+
public function icon_can_be_set()
6262
{
6363
$this->message->icon('/icon.jpg');
6464

6565
$this->assertEquals('/icon.jpg', $this->message->toArray()['icon']);
6666
}
6767

6868
/** @test */
69-
public function can_set_image()
69+
public function image_can_be_set()
7070
{
7171
$this->message->image('/image.jpg');
7272

7373
$this->assertEquals('/image.jpg', $this->message->toArray()['image']);
7474
}
7575

7676
/** @test */
77-
public function can_set_lang()
77+
public function lang_can_be_set()
7878
{
7979
$this->message->lang('en');
8080

8181
$this->assertEquals('en', $this->message->toArray()['lang']);
8282
}
8383

8484
/** @test */
85-
public function can_set_renotify()
85+
public function renotify_can_be_set()
8686
{
8787
$this->message->renotify();
8888

8989
$this->assertTrue($this->message->toArray()['renotify']);
9090
}
9191

9292
/** @test */
93-
public function can_set_requireInteraction()
93+
public function requireInteraction_can_be_set()
9494
{
9595
$this->message->requireInteraction();
9696

9797
$this->assertTrue($this->message->toArray()['requireInteraction']);
9898
}
9999

100100
/** @test */
101-
public function can_set_tag()
101+
public function tag_can_be_set()
102102
{
103103
$this->message->tag('tag1');
104104

105105
$this->assertEquals('tag1', $this->message->toArray()['tag']);
106106
}
107107

108108
/** @test */
109-
public function can_set_vibration_pattern()
109+
public function vibration_pattern_can_be_set()
110110
{
111111
$this->message->vibrate([1, 2, 3]);
112112

113113
$this->assertEquals([1, 2, 3], $this->message->toArray()['vibrate']);
114114
}
115115

116116
/** @test */
117-
public function can_set_arbitrary_data()
117+
public function arbitrary_data_can_be_set()
118118
{
119119
$this->message->data(['id' => 1]);
120120

121121
$this->assertEquals(['id' => 1], $this->message->toArray()['data']);
122122
}
123123

124124
/** @test */
125-
public function can_set_options()
125+
public function options_can_be_set()
126126
{
127127
$this->message->options(['ttl' => 60]);
128128

0 commit comments

Comments
 (0)