Skip to content

Commit a3af610

Browse files
Alexandre Delplacenorkunas
authored andcommitted
Add unit tests + refacto (#74)
Closes #11 Closes #72
1 parent feb5c4c commit a3af610

26 files changed

+1914
-399
lines changed

src/Apps.php

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace OneSignal;
44

5-
use Symfony\Component\OptionsResolver\OptionsResolver;
5+
use OneSignal\Resolver\ResolverFactory;
66

77
class Apps
88
{
99
protected $api;
1010

11-
public function __construct(OneSignal $api)
11+
private $resolverFactory;
12+
13+
public function __construct(OneSignal $api, ResolverFactory $resolverFactory)
1214
{
1315
$this->api = $api;
16+
$this->resolverFactory = $resolverFactory;
1417
}
1518

1619
/**
@@ -54,7 +57,7 @@ public function getAll()
5457
*/
5558
public function add(array $data)
5659
{
57-
$data = $this->resolve($data);
60+
$data = $this->resolverFactory->createAppResolver()->resolve($data);
5861

5962
return $this->api->request('POST', '/apps', [
6063
'Authorization' => 'Basic '.$this->api->getConfig()->getUserAuthKey(),
@@ -73,60 +76,10 @@ public function add(array $data)
7376
*/
7477
public function update($id, array $data)
7578
{
76-
$data = $this->resolve($data);
79+
$data = $this->resolverFactory->createAppResolver()->resolve($data);
7780

7881
return $this->api->request('PUT', '/apps/'.$id, [
7982
'Authorization' => 'Basic '.$this->api->getConfig()->getUserAuthKey(),
8083
], json_encode($data));
8184
}
82-
83-
protected function resolve(array $data)
84-
{
85-
$resolver = new OptionsResolver();
86-
87-
$resolver
88-
->setRequired('name')
89-
->setAllowedTypes('name', 'string')
90-
->setDefined('apns_env')
91-
->setAllowedTypes('apns_env', 'string')
92-
->setAllowedValues('apns_env', ['sandbox', 'production'])
93-
->setDefined('apns_p12')
94-
->setAllowedTypes('apns_p12', 'string')
95-
->setDefined('apns_p12_password')
96-
->setAllowedTypes('apns_p12_password', 'string')
97-
->setDefined('gcm_key')
98-
->setAllowedTypes('gcm_key', 'string')
99-
->setDefined('chrome_key')
100-
->setAllowedTypes('chrome_key', 'string')
101-
->setDefined('safari_apns_p12')
102-
->setAllowedTypes('safari_apns_p12', 'string')
103-
->setDefined('chrome_web_key')
104-
->setAllowedTypes('chrome_web_key', 'string')
105-
->setDefined('safari_apns_p12_password')
106-
->setAllowedTypes('safari_apns_p12_password', 'string')
107-
->setDefined('site_name')
108-
->setAllowedTypes('site_name', 'string')
109-
->setDefined('safari_site_origin')
110-
->setAllowedTypes('safari_site_origin', 'string')
111-
->setDefined('safari_icon_16_16')
112-
->setAllowedTypes('safari_icon_16_16', 'string')
113-
->setDefined('safari_icon_32_32')
114-
->setAllowedTypes('safari_icon_32_32', 'string')
115-
->setDefined('safari_icon_64_64')
116-
->setAllowedTypes('safari_icon_64_64', 'string')
117-
->setDefined('safari_icon_128_128')
118-
->setAllowedTypes('safari_icon_128_128', 'string')
119-
->setDefined('safari_icon_256_256')
120-
->setAllowedTypes('safari_icon_256_256', 'string')
121-
->setDefined('chrome_web_origin')
122-
->setAllowedTypes('chrome_web_origin', 'string')
123-
->setDefined('chrome_web_gcm_sender_id')
124-
->setAllowedTypes('chrome_web_gcm_sender_id', 'string')
125-
->setDefined('chrome_web_default_notification_icon')
126-
->setAllowedTypes('chrome_web_default_notification_icon', 'string')
127-
->setDefined('chrome_web_sub_domain')
128-
->setAllowedTypes('chrome_web_sub_domain', 'string');
129-
130-
return $resolver->resolve($data);
131-
}
13285
}

src/Devices.php

Lines changed: 10 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace OneSignal;
44

5-
use Symfony\Component\OptionsResolver\OptionsResolver;
5+
use OneSignal\Resolver\ResolverFactory;
66

77
class Devices
88
{
@@ -22,9 +22,12 @@ class Devices
2222

2323
protected $api;
2424

25-
public function __construct(OneSignal $api)
25+
private $resolverFactory;
26+
27+
public function __construct(OneSignal $api, ResolverFactory $resolverFactory)
2628
{
2729
$this->api = $api;
30+
$this->resolverFactory = $resolverFactory;
2831
}
2932

3033
/**
@@ -75,24 +78,7 @@ public function getAll($limit = self::DEVICES_LIMIT, $offset = 0)
7578
*/
7679
public function add(array $data)
7780
{
78-
$data = $this->resolve($data, function (OptionsResolver $resolver) {
79-
$resolver
80-
->setRequired('device_type')
81-
->setAllowedTypes('device_type', 'int')
82-
->setAllowedValues('device_type', [
83-
self::IOS,
84-
self::ANDROID,
85-
self::AMAZON,
86-
self::WINDOWS_PHONE,
87-
self::WINDOWS_PHONE_MPNS,
88-
self::CHROME_APP,
89-
self::CHROME_WEB,
90-
self::WINDOWS_PHONE_WNS,
91-
self::SAFARI,
92-
self::FIREFOX,
93-
self::MACOS,
94-
]);
95-
});
81+
$data = $this->resolverFactory->createNewDeviceResolver()->resolve($data);
9682

9783
return $this->api->request('POST', '/players', [], json_encode($data));
9884
}
@@ -107,7 +93,7 @@ public function add(array $data)
10793
*/
10894
public function update($id, array $data)
10995
{
110-
$data = $this->resolve($data);
96+
$data = $this->resolverFactory->createExistingDeviceResolver()->resolve($data);
11197

11298
return $this->api->request('PUT', '/players/'.$id, [], json_encode($data));
11399
}
@@ -122,27 +108,7 @@ public function update($id, array $data)
122108
*/
123109
public function onSession($id, array $data)
124110
{
125-
$data = (new OptionsResolver())
126-
->setDefined('identifier')
127-
->setAllowedTypes('identifier', 'string')
128-
->setDefined('language')
129-
->setAllowedTypes('language', 'string')
130-
->setDefined('timezone')
131-
->setAllowedTypes('timezone', 'int')
132-
->setDefined('game_version')
133-
->setAllowedTypes('game_version', 'string')
134-
->setDefined('device_os')
135-
->setAllowedTypes('device_os', 'string')
136-
// @todo: remove "device_model" later (this option is probably deprecated as it is removed from documentation)
137-
->setDefined('device_model')
138-
->setAllowedTypes('device_model', 'string')
139-
->setDefined('ad_id')
140-
->setAllowedTypes('ad_id', 'string')
141-
->setDefined('sdk')
142-
->setAllowedTypes('sdk', 'string')
143-
->setDefined('tags')
144-
->setAllowedTypes('tags', 'array')
145-
->resolve($data);
111+
$data = $this->resolverFactory->createDeviceSessionResolver()->resolve($data);
146112

147113
return $this->api->request('POST', '/players/'.$id.'/on_session', [], json_encode($data));
148114
}
@@ -157,23 +123,7 @@ public function onSession($id, array $data)
157123
*/
158124
public function onPurchase($id, array $data)
159125
{
160-
$data = (new OptionsResolver())
161-
->setDefined('existing')
162-
->setAllowedTypes('existing', 'bool')
163-
->setRequired('purchases')
164-
->setAllowedTypes('purchases', 'array')
165-
->resolve($data);
166-
167-
foreach ($data['purchases'] as $key => $purchase) {
168-
$data['purchases'][$key] = (new OptionsResolver())
169-
->setRequired('sku')
170-
->setAllowedTypes('sku', 'string')
171-
->setRequired('amount')
172-
->setAllowedTypes('amount', 'float')
173-
->setRequired('iso')
174-
->setAllowedTypes('iso', 'string')
175-
->resolve($purchase);
176-
}
126+
$data = $this->resolverFactory->createDevicePurchaseResolver()->resolve($data);
177127

178128
return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
179129
}
@@ -188,11 +138,7 @@ public function onPurchase($id, array $data)
188138
*/
189139
public function onFocus($id, array $data)
190140
{
191-
$data = (new OptionsResolver())
192-
->setDefault('state', 'ping')
193-
->setRequired('active_time')
194-
->setAllowedTypes('active_time', 'int')
195-
->resolve($data);
141+
$data = $this->resolverFactory->createDeviceFocusResolver()->resolve($data);
196142

197143
return $this->api->request('POST', '/players/'.$id.'/on_focus', [], json_encode($data));
198144
}
@@ -221,54 +167,4 @@ public function csvExport(array $extraFields = [])
221167

222168
return $this->api->request('POST', $url, $headers, json_encode($body));
223169
}
224-
225-
protected function resolve(array $data, callable $callback = null)
226-
{
227-
$resolver = new OptionsResolver();
228-
229-
if (is_callable($callback)) {
230-
$callback($resolver);
231-
}
232-
233-
$resolver
234-
->setDefined('identifier')
235-
->setAllowedTypes('identifier', 'string')
236-
->setDefined('language')
237-
->setAllowedTypes('language', 'string')
238-
->setDefined('timezone')
239-
->setAllowedTypes('timezone', 'int')
240-
->setDefined('game_version')
241-
->setAllowedTypes('game_version', 'string')
242-
->setDefined('device_model')
243-
->setAllowedTypes('device_model', 'string')
244-
->setDefined('device_os')
245-
->setAllowedTypes('device_os', 'string')
246-
->setDefined('ad_id')
247-
->setAllowedTypes('ad_id', 'string')
248-
->setDefined('sdk')
249-
->setAllowedTypes('sdk', 'string')
250-
->setDefined('session_count')
251-
->setAllowedTypes('session_count', 'int')
252-
->setDefined('tags')
253-
->setAllowedTypes('tags', 'array')
254-
->setDefined('amount_spent')
255-
->setAllowedTypes('amount_spent', 'float')
256-
->setDefined('created_at')
257-
->setAllowedTypes('created_at', 'int')
258-
->setDefined('playtime')
259-
->setAllowedTypes('playtime', 'int')
260-
->setDefined('badge_count')
261-
->setAllowedTypes('badge_count', 'int')
262-
->setDefined('last_active')
263-
->setAllowedTypes('last_active', 'int')
264-
->setDefined('notification_types')
265-
->setAllowedTypes('notification_types', 'int')
266-
->setAllowedValues('notification_types', [1, -2])
267-
->setDefined('test_type')
268-
->setAllowedTypes('test_type', 'int')
269-
->setAllowedValues('test_type', [1, 2])
270-
->setDefault('app_id', $this->api->getConfig()->getApplicationId());
271-
272-
return $resolver->resolve($data);
273-
}
274170
}

0 commit comments

Comments
 (0)