Skip to content

Commit 62cd23a

Browse files
committed
[Refactoring] Removed ExpoDatabaseDriver and replaced it with a dataprovider, so that we can keep the ExpoControllerTest.
1 parent 38db418 commit 62cd23a

File tree

3 files changed

+172
-238
lines changed

3 files changed

+172
-238
lines changed

tests/ExpoDatabaseDriverTest.php renamed to tests/ExpoControllerTest.php

Lines changed: 137 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,46 @@
33
namespace NotificationChannels\ExpoPushNotifications\Test;
44

55
use ExponentPhpSDK\Expo;
6+
use ExponentPhpSDK\ExpoRepository;
7+
use Illuminate\Contracts\Validation\Factory;
68
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\Validator;
710
use ExponentPhpSDK\ExpoRegistrar;
811
use Illuminate\Events\Dispatcher;
912
use Illuminate\Support\Facades\Auth;
13+
use ExponentPhpSDK\Repositories\ExpoFileDriver;
1014
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
1115
use NotificationChannels\ExpoPushNotifications\Http\ExpoController;
16+
use NotificationChannels\ExpoPushNotifications\Models\Interest;
1217
use NotificationChannels\ExpoPushNotifications\Repositories\ExpoDatabaseDriver;
1318

14-
class ExpoDatabaseDriverTest extends TestCase
19+
class ExpoControllerTest extends TestCase
1520
{
1621
/**
17-
* @var ExpoChannel
22+
* @var ExpoController
1823
*/
19-
protected $expoChannel;
24+
protected $expoController;
2025

2126
/**
22-
* @var ExpoController
27+
* Sets up the expo controller with the given expo channel.
28+
*
29+
* @param ExpoRepository $expoRepository
30+
*
31+
* @return array
2332
*/
24-
protected $expoController;
33+
protected function setupExpo(ExpoRepository $expoRepository)
34+
{
35+
$expoChannel = new ExpoChannel(new Expo(new ExpoRegistrar($expoRepository)), new Dispatcher);
36+
$expoController = new ExpoController($expoChannel);
37+
return [$expoController, $expoChannel];
38+
}
2539

2640
public function setUp()
2741
{
2842
parent::setUp();
2943

3044
$this->setUpDatabase();
3145

32-
$this->expoChannel = new ExpoChannel(new Expo(new ExpoRegistrar(new ExpoDatabaseDriver())), new Dispatcher());
33-
34-
$this->expoController = new ExpoController($this->expoChannel);
35-
3646
// We will fake an authenticated user
3747
Auth::shouldReceive('user')->andReturn(new User());
3848
}
@@ -44,9 +54,29 @@ public function tearDown()
4454
parent::tearDown();
4555
}
4656

47-
/** @test */
48-
public function aDeviceCanSubscribeToTheSystem()
57+
/**
58+
* Data provider to help test the expo controller with the different repositories
59+
*
60+
* @return array
61+
*/
62+
public function availableRepositories()
63+
{
64+
return [
65+
[new ExpoDatabaseDriver],
66+
[new ExpoFileDriver]
67+
];
68+
}
69+
70+
/** @test
71+
*
72+
* @param $expoRepository
73+
*
74+
* @dataProvider availableRepositories
75+
*/
76+
public function aDeviceCanSubscribeToTheSystem($expoRepository)
4977
{
78+
list($expoController, $expoChannel) = $this->setupExpo($expoRepository);
79+
5080
// We will fake a request with the following data
5181
$data = ['expo_token' => 'ExponentPushToken[fakeToken]'];
5282
$request = $this->mockRequest($data);
@@ -55,25 +85,39 @@ public function aDeviceCanSubscribeToTheSystem()
5585
$this->mockValidator(false);
5686

5787
/** @var Request $request */
58-
$response = $this->expoController->subscribe($request);
88+
$response = $expoController->subscribe($request);
5989
$response = json_decode($response->content());
6090

6191
// The response should contain a succeeded status
6292
$this->assertEquals('succeeded', $response->status);
6393
// The response should return the registered token
6494
$this->assertEquals($data['expo_token'], $response->expo_token);
95+
96+
if ($expoRepository instanceof ExpoDatabaseDriver) {
97+
$this->assertDatabaseHas(config('exponent-push-notifications.interests.database.table_name'), [
98+
'key' => 'NotificationChannels.ExpoPushNotifications.Test.User.'.(new User)->getKey(),
99+
'value' => $data['expo_token']
100+
]);
101+
}
65102
}
66103

67-
/** @test */
68-
public function subscribeReturnsErrorResponseIfTokenInvalid()
104+
/** @test
105+
*
106+
* @param $expoRepository
107+
*
108+
* @dataProvider availableRepositories
109+
*/
110+
public function subscribeReturnsErrorResponseIfTokenInvalid($expoRepository)
69111
{
112+
list($expoController, $expoChannel) = $this->setupExpo($expoRepository);
113+
70114
// We will fake a request with no data
71115
$request = $this->mockRequest([]);
72116

73117
$this->mockValidator(true);
74118

75119
/** @var Request $request */
76-
$response = $this->expoController->subscribe($request);
120+
$response = $expoController->subscribe($request);
77121

78122
// The response should contain a failed status
79123
$this->assertEquals('failed', json_decode($response->content())->status);
@@ -104,9 +148,17 @@ public function subscribeReturnsErrorResponseIfExceptionIsThrown()
104148
$this->assertEquals('failed', $response->status);
105149
}
106150

107-
/** @test */
108-
public function aDeviceCanUnsubscribeSingleTokenFromTheSystem()
151+
/** @test
152+
*
153+
*
154+
* @dataProvider availableRepositories
155+
*
156+
* @param $expoRepository
157+
*/
158+
public function aDeviceCanUnsubscribeSingleTokenFromTheSystem($expoRepository)
109159
{
160+
list($expoController, $expoChannel) = $this->setupExpo($expoRepository);
161+
110162
// We will fake a request with the following data
111163
$data = ['expo_token' => 'ExponentPushToken[fakeToken]'];
112164
$request = $this->mockRequest($data);
@@ -116,19 +168,33 @@ public function aDeviceCanUnsubscribeSingleTokenFromTheSystem()
116168

117169
// We will subscribe an interest to the server.
118170
$token = 'ExponentPushToken[fakeToken]';
119-
$interest = $this->expoChannel->interestName(new User());
120-
$this->expoChannel->expo->subscribe($interest, $token);
171+
$interest = $expoChannel->interestName(new User());
172+
$expoChannel->expo->subscribe($interest, $token);
121173

122-
$response = $this->expoController->unsubscribe($request);
174+
$response = $expoController->unsubscribe($request);
123175
$response = json_decode($response->content());
124176

125177
// The response should contain a deleted property with value true
126178
$this->assertTrue($response->deleted);
179+
180+
if ($expoRepository instanceof ExpoDatabaseDriver) {
181+
$this->assertDatabaseMissing(config('exponent-push-notifications.interests.database.table_name'), [
182+
'key' => 'NotificationChannels.ExpoPushNotifications.Test.User.'.(new User)->getKey(),
183+
'value' => $data['expo_token']
184+
]);
185+
}
127186
}
128187

129-
/** @test */
130-
public function aDeviceCanUnsubscribeFromTheSystem()
188+
/** @test
189+
*
190+
* @param $expoRepository
191+
*
192+
* @dataProvider availableRepositories
193+
*/
194+
public function aDeviceCanUnsubscribeFromTheSystem($expoRepository)
131195
{
196+
list($expoController, $expoChannel) = $this->setupExpo($expoRepository);
197+
132198
// We will fake a request with the following data
133199
$request = $this->mockRequest([]);
134200
$request->shouldReceive('get')->with('expo_token')->andReturn([]);
@@ -137,14 +203,18 @@ public function aDeviceCanUnsubscribeFromTheSystem()
137203

138204
// We will subscribe an interest to the server.
139205
$token = 'ExponentPushToken[fakeToken]';
140-
$interest = $this->expoChannel->interestName(new User());
141-
$this->expoChannel->expo->subscribe($interest, $token);
206+
$interest = $expoChannel->interestName(new User());
207+
$expoChannel->expo->subscribe($interest, $token);
142208

143-
$response = $this->expoController->unsubscribe($request);
209+
$response = $expoController->unsubscribe($request);
144210
$response = json_decode($response->content());
145211

146212
// The response should contain a deleted property with value true
147213
$this->assertTrue($response->deleted);
214+
215+
if ($expoRepository instanceof ExpoDatabaseDriver) {
216+
$this->assertEquals(0, Interest::count());
217+
}
148218
}
149219

150220
/** @test */
@@ -162,4 +232,46 @@ public function unsubscribeReturnsErrorResponseIfExceptionIsThrown()
162232

163233
$this->assertEquals('failed', $response->status);
164234
}
235+
/**
236+
* Mocks a request for the ExpoController.
237+
*
238+
* @param $data
239+
*
240+
* @return \Mockery\MockInterface
241+
*/
242+
public function mockRequest($data)
243+
{
244+
$request = \Mockery::mock(Request::class);
245+
$request->shouldReceive('all')->andReturn($data);
246+
247+
return $request;
248+
}
249+
250+
/**
251+
* @param bool $fails
252+
*
253+
* @return \Mockery\MockInterface
254+
*/
255+
public function mockValidator(bool $fails)
256+
{
257+
$validator = \Mockery::mock(\Illuminate\Validation\Validator::class);
258+
259+
$validation = \Mockery::mock(Factory::class);
260+
261+
$validation->shouldReceive('make')->once()->andReturn($validator);
262+
263+
$validator->shouldReceive('fails')->once()->andReturn($fails);
264+
265+
Validator::swap($validation);
266+
267+
return $validator;
268+
}
269+
}
270+
271+
class User
272+
{
273+
public function getKey()
274+
{
275+
return 1;
276+
}
165277
}

0 commit comments

Comments
 (0)