1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Unified push notification services abstraction (http://github.com/juliangut/tify). |
5
|
|
|
* |
6
|
|
|
* @license BSD-3-Clause |
7
|
|
|
* @link https://github.com/juliangut/tify |
8
|
|
|
* @author Julián Gutiérrez <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Jgut\Tify\Tests; |
12
|
|
|
|
13
|
|
|
use Jgut\Tify\Adapter\AbstractAdapter; |
14
|
|
|
use Jgut\Tify\Adapter\ApnsAdapter; |
15
|
|
|
use Jgut\Tify\Adapter\GcmAdapter; |
16
|
|
|
use Jgut\Tify\Notification; |
17
|
|
|
use Jgut\Tify\Result; |
18
|
|
|
use Jgut\Tify\Service; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Notification service tests. |
22
|
|
|
*/ |
23
|
|
|
class ServiceTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Service |
27
|
|
|
*/ |
28
|
|
|
protected $service; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function setUp() |
34
|
|
|
{ |
35
|
|
|
/* @var AbstractAdapter $adapter */ |
36
|
|
|
$adapter = $this->getMockForAbstractClass(AbstractAdapter::class, [], '', false); |
37
|
|
|
|
38
|
|
|
/* @var Notification $notification */ |
39
|
|
|
$notification = $this->getMockBuilder(Notification::class) |
40
|
|
|
->disableOriginalConstructor() |
41
|
|
|
->getMock(); |
42
|
|
|
|
43
|
|
|
$this->service = new Service($adapter, $notification); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testAccesorsMutators() |
47
|
|
|
{ |
48
|
|
|
$service = new Service; |
49
|
|
|
|
50
|
|
|
self::assertEmpty($service->getNotifications()); |
51
|
|
|
|
52
|
|
|
/* @var Notification $notification */ |
53
|
|
|
$notification = $this->getMockBuilder(Notification::class) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
$service->addNotification($notification); |
57
|
|
|
self::assertCount(1, $service->getNotifications()); |
58
|
|
|
|
59
|
|
|
$service->clearNotifications(); |
60
|
|
|
self::assertCount(0, $service->getNotifications()); |
61
|
|
|
|
62
|
|
|
self::assertEmpty($service->getAdapters()); |
63
|
|
|
|
64
|
|
|
/* @var GcmAdapter $adapter */ |
65
|
|
|
$adapter = $this->getMockBuilder(GcmAdapter::class) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->getMock(); |
68
|
|
|
$service->addAdapter($adapter); |
69
|
|
|
self::assertCount(1, $service->getAdapters()); |
70
|
|
|
|
71
|
|
|
$service->clearAdapters(); |
72
|
|
|
self::assertCount(0, $service->getAdapters()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testSend() |
76
|
|
|
{ |
77
|
|
|
$result = new Result('aaa', new \DateTime('now', new \DateTimeZone('UTC'))); |
78
|
|
|
|
79
|
|
|
$adapter = $this->getMockBuilder(GcmAdapter::class) |
80
|
|
|
->disableOriginalConstructor() |
81
|
|
|
->getMock(); |
82
|
|
|
$adapter->expects(self::once()) |
83
|
|
|
->method('push') |
84
|
|
|
->will(self::returnValue([$result])); |
85
|
|
|
/* @var GcmAdapter $adapter */ |
86
|
|
|
$this->service->addAdapter($adapter); |
87
|
|
|
|
88
|
|
|
/* @var Notification $notification */ |
89
|
|
|
$notification = $this->getMockBuilder(Notification::class) |
90
|
|
|
->disableOriginalConstructor() |
91
|
|
|
->getMock(); |
92
|
|
|
$this->service->clearNotifications(); |
93
|
|
|
$this->service->addNotification($notification); |
94
|
|
|
|
95
|
|
|
$results = $this->service->push(); |
96
|
|
|
self::assertCount(1, $results); |
97
|
|
|
self::assertEquals($result, $results[0]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testFeedback() |
101
|
|
|
{ |
102
|
|
|
$result = new Result('aaa', new \DateTime('now', new \DateTimeZone('UTC'))); |
103
|
|
|
|
104
|
|
|
$adapter = $this->getMockBuilder(ApnsAdapter::class) |
105
|
|
|
->disableOriginalConstructor() |
106
|
|
|
->getMock(); |
107
|
|
|
$adapter->expects(self::once()) |
108
|
|
|
->method('feedback') |
109
|
|
|
->will(self::returnValue([$result])); |
110
|
|
|
/* @var GcmAdapter $adapter */ |
111
|
|
|
$this->service->addAdapter($adapter); |
112
|
|
|
|
113
|
|
|
$results = $this->service->feedback(); |
114
|
|
|
self::assertCount(1, $results); |
115
|
|
|
self::assertEquals($result, $results[0]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|