/** * @param mixed $messageText * @param array $devices * @return CreateMessageRequest */ protected function constructorMessage($messageText, array $devices) { $message = CreateMessageRequest::create(); $notification = Notification::create()->setContent($messageText); foreach ($devices as $device) { $notification->addDevice($device); } $message->addNotification($notification); return $message; }
/** * Test method for the <tt>toJSON()</tt> function. */ public function testToJSON() { $array = Notification::create()->setSendDate('now')->setIgnoreUserTimezone(true)->setContent(array('en' => 'English', 'ru' => 'Русский', 'de' => 'Deutsch'))->setPageId(39)->setLink('http://google.com')->setMinimizeLink(MinimizeLink::none())->setData(array('custom' => 'json data'))->setPlatforms(array(Platform::iOS(), Platform::blackBerry(), Platform::android(), Platform::nokia(), Platform::windowsPhone7(), Platform::maxOSX(), Platform::windows8(), Platform::amazon(), Platform::safari()))->setDevices(array('dec301908b9ba8df85e57a58e40f96f523f4c2068674f5fe2ba25cdc250a2a41'))->setFilter('FILTER_NAME')->setConditions(array(IntCondition::create('intTag')->gte(10), StringCondition::create('stringTag')->eq('stringValue'), ListCondition::create('listTag')->in(array('value0', 'value1', 'value2'))))->setADM(ADM::create()->setBanner('http://example.com/banner.png')->setCustomIcon('http://example.com/image.png')->setHeader('Header')->setIcon('icon')->setRootParams(array('key' => 'value'))->setSound('push.mp3')->setTtl(3600))->setAndroid(Android::create()->setBanner('http://example.com/banner.png')->setCustomIcon('http://example.com/image.png')->setGcmTtl(3600)->setHeader('Header')->setIcon('icon')->setRootParams(array('key' => 'value'))->setSound('push.mp3'))->setBlackBerry(BlackBerry::create()->setHeader('header'))->setIOS(IOS::create()->setApnsTrimContent(true)->setBadges(5)->setRootParams(array('aps' => array('content-available' => '1')))->setSound('sound file.wav')->setTtl(3600)->setTrimContent(true))->setMac(Mac::create()->setBadges(3)->setRootParams(array('content-available' => '1'))->setSound('sound.caf')->setTtl(3600))->setSafari(Safari::create()->setAction('Click here')->setTitle('Title')->setTtl(3600)->setUrlArgs(array('firstArgument', 'secondArgument')))->setWNS(WNS::create()->setContent(array('en' => 'ENENENEN', 'de' => 'DEDEDEDE'))->setTag('myTag')->setType('Badge'))->setWP(WP::create()->setBackbackground('/Resources/Green.jpg')->setBackcontent('back content')->setBackground('/Resources/Red.jpg')->setBacktitle('back title')->setCount(3)->setType('Tile'))->toJSON(); // Test the generic properties $this->assertCount(49, $array); $this->assertEquals('now', $array['send_date']); $this->assertTrue($array['ignore_user_timezone']); $this->assertCount(3, $array['content']); $this->assertEquals('English', $array['content']['en']); $this->assertEquals('Русский', $array['content']['ru']); $this->assertEquals('Deutsch', $array['content']['de']); $this->assertEquals(39, $array['page_id']); $this->assertEquals('http://google.com', $array['link']); $this->assertEquals(0, $array['minimize_link']); $this->assertCount(1, $array['data']); $this->assertEquals('json data', $array['data']['custom']); $this->assertCount(9, $array['platforms']); $this->assertEquals(1, $array['platforms'][0]); $this->assertEquals(2, $array['platforms'][1]); $this->assertEquals(3, $array['platforms'][2]); $this->assertEquals(4, $array['platforms'][3]); $this->assertEquals(5, $array['platforms'][4]); $this->assertEquals(7, $array['platforms'][5]); $this->assertEquals(8, $array['platforms'][6]); $this->assertEquals(9, $array['platforms'][7]); $this->assertEquals(10, $array['platforms'][8]); $this->assertCount(1, $array['devices']); $this->assertEquals('dec301908b9ba8df85e57a58e40f96f523f4c2068674f5fe2ba25cdc250a2a41', $array['devices'][0]); $this->assertEquals('FILTER_NAME', $array['filter']); $this->assertCount(3, $array['conditions']); $this->assertEquals('intTag', $array['conditions'][0][0]); $this->assertEquals('GTE', $array['conditions'][0][1]); $this->assertEquals(10, $array['conditions'][0][2]); $this->assertEquals('stringTag', $array['conditions'][1][0]); $this->assertEquals('EQ', $array['conditions'][1][1]); $this->assertEquals('stringValue', $array['conditions'][1][2]); $this->assertEquals('listTag', $array['conditions'][2][0]); $this->assertEquals('IN', $array['conditions'][2][1]); $this->assertCount(3, $array['conditions'][2][2]); $this->assertEquals('value0', $array['conditions'][2][2][0]); $this->assertEquals('value1', $array['conditions'][2][2][1]); $this->assertEquals('value2', $array['conditions'][2][2][2]); // Test the ADM parameters $this->assertEquals('http://example.com/banner.png', $array['adm_banner']); $this->assertEquals('http://example.com/image.png', $array['adm_custom_icon']); $this->assertEquals('Header', $array['adm_header']); $this->assertEquals('icon', $array['adm_icon']); $this->assertEquals(array('key' => 'value'), $array['adm_root_params']); $this->assertEquals('push.mp3', $array['adm_sound']); $this->assertEquals(3600, $array['adm_ttl']); // Test Android parameters $this->assertEquals('http://example.com/banner.png', $array['android_banner']); $this->assertEquals('http://example.com/image.png', $array['android_custom_icon']); $this->assertEquals(3600, $array['android_gcm_ttl']); $this->assertEquals('Header', $array['android_header']); $this->assertEquals('icon', $array['android_icon']); $this->assertEquals(array('key' => 'value'), $array['android_root_params']); $this->assertEquals('push.mp3', $array['android_sound']); // Test BlackBerry parameters $this->assertEquals('header', $array['blackberry_header']); // Test IOS parameters $this->assertEquals(1, $array['apns_trim_content']); $this->assertEquals(5, $array['ios_badges']); $this->assertEquals(array('aps' => array('content-available' => '1')), $array['ios_root_params']); $this->assertEquals('sound file.wav', $array['ios_sound']); $this->assertEquals(3600, $array['ios_ttl']); $this->assertEquals(1, $array['ios_trim_content']); // Test Mac parameters $this->assertEquals(3, $array['mac_badges']); $this->assertEquals(array('content-available' => '1'), $array['mac_root_params']); $this->assertEquals('sound.caf', $array['mac_sound']); $this->assertEquals(3600, $array['mac_ttl']); // Test Safari parameters $this->assertEquals('Click here', $array['safari_action']); $this->assertEquals('Title', $array['safari_title']); $this->assertEquals(3600, $array['safari_ttl']); $this->assertEquals(array('firstArgument', 'secondArgument'), $array['safari_url_args']); // Test WNS parameters $this->assertEquals(array('en' => 'ENENENEN', 'de' => 'DEDEDEDE'), $array['wns_content']); $this->assertEquals('myTag', $array['wns_tag']); $this->assertEquals('Badge', $array['wns_type']); // Test WP parameters $this->assertEquals('/Resources/Green.jpg', $array['wp_backbackground']); $this->assertEquals('back content', $array['wp_backcontent']); $this->assertEquals('/Resources/Red.jpg', $array['wp_background']); $this->assertEquals('back title', $array['wp_backtitle']); $this->assertEquals(3, $array['wp_count']); $this->assertEquals('Tile', $array['wp_type']); }
/** * Test method for the <tt>toJSON()</tt> function. */ public function testToJSON() { $createMessageRequest = new CreateMessageRequest(); // Test without the 'application' and 'applicationsGroup' parameters try { $createMessageRequest->toJSON(); $this->fail('Must have thrown a PushwooshException !'); } catch (PushwooshException $pe) { // Expected } // Test with both the 'application' and 'applicationsGroup parameters set $createMessageRequest->setApplication('XXXX-XXXX'); $createMessageRequest->setApplicationsGroup('XXXX-XXXX'); try { $createMessageRequest->toJSON(); $this->fail('Must have thrown a PushwooshException !'); } catch (PushwooshException $pe) { // Expected } // Test without the 'auth' parameter set $createMessageRequest->setApplicationsGroup(null); try { $createMessageRequest->toJSON(); $this->fail('Must have thrown a PushwooshException !'); } catch (PushwooshException $pe) { // Expected } // Test with the 'auth' and 'application' parameters set and no notification $createMessageRequest->setAuth('XXXX'); $json = $createMessageRequest->toJSON(); $this->assertCount(4, $json); $this->assertTrue(array_key_exists('application', $json)); $this->assertTrue(array_key_exists('applicationsGroup', $json)); $this->assertTrue(array_key_exists('auth', $json)); $this->assertTrue(array_key_exists('notifications', $json)); $this->assertEquals('XXXX-XXXX', $json['application']); $this->assertNull($json['applicationsGroup']); $this->assertEquals('XXXX', $json['auth']); $this->assertCount(0, $json['notifications']); // Test with one notificiation with only a 'content' field $notification = Notification::create(); $notification->setContent('CONTENT'); $createMessageRequest->addNotification($notification); $json = $createMessageRequest->toJSON(); $this->assertCount(4, $json); $this->assertTrue(array_key_exists('application', $json)); $this->assertTrue(array_key_exists('applicationsGroup', $json)); $this->assertTrue(array_key_exists('auth', $json)); $this->assertTrue(array_key_exists('notifications', $json)); $this->assertEquals('XXXX-XXXX', $json['application']); $this->assertNull($json['applicationsGroup']); $this->assertEquals('XXXX', $json['auth']); $this->assertCount(1, $json['notifications']); $this->assertCount(3, $json['notifications'][0]); $this->assertTrue(array_key_exists('send_date', $json['notifications'][0])); $this->assertTrue($json['notifications'][0]['ignore_user_timezone']); $this->assertEquals('CONTENT', $json['notifications'][0]['content']); // Test with one notification having additional data $notification->setData(array('DATA_PARAMETER_1' => 'DATA_PARAMETER_1_VALUE', 'DATA_PARAMETER_2' => 'DATA_PARAMETER_2_VALUE', 'DATA_PARAMETER_3' => 'DATA_PARAMETER_3_VALUE')); $json = $createMessageRequest->toJSON(); $this->assertCount(4, $json); $this->assertTrue(array_key_exists('application', $json)); $this->assertTrue(array_key_exists('applicationsGroup', $json)); $this->assertTrue(array_key_exists('auth', $json)); $this->assertTrue(array_key_exists('notifications', $json)); $this->assertEquals('XXXX-XXXX', $json['application']); $this->assertNull($json['applicationsGroup']); $this->assertEquals('XXXX', $json['auth']); $this->assertCount(1, $json['notifications']); $this->assertCount(4, $json['notifications'][0]); $this->assertTrue(array_key_exists('send_date', $json['notifications'][0])); $this->assertTrue($json['notifications'][0]['ignore_user_timezone']); $this->assertEquals('CONTENT', $json['notifications'][0]['content']); $this->assertCount(3, $json['notifications'][0]['data']); $this->assertEquals('DATA_PARAMETER_1_VALUE', $json['notifications'][0]['data']['DATA_PARAMETER_1']); $this->assertEquals('DATA_PARAMETER_2_VALUE', $json['notifications'][0]['data']['DATA_PARAMETER_2']); $this->assertEquals('DATA_PARAMETER_3_VALUE', $json['notifications'][0]['data']['DATA_PARAMETER_3']); // Test with one notification hacing additional data and devices $notification->addDevice('DEVICE_TOKEN_1'); $notification->addDevice('DEVICE_TOKEN_2'); $notification->addDevice('DEVICE_TOKEN_3'); $json = $createMessageRequest->toJSON(); $this->assertCount(4, $json); $this->assertTrue(array_key_exists('application', $json)); $this->assertTrue(array_key_exists('applicationsGroup', $json)); $this->assertTrue(array_key_exists('auth', $json)); $this->assertTrue(array_key_exists('notifications', $json)); $this->assertEquals('XXXX-XXXX', $json['application']); $this->assertNull($json['applicationsGroup']); $this->assertEquals('XXXX', $json['auth']); $this->assertCount(1, $json['notifications']); $this->assertCount(5, $json['notifications'][0]); $this->assertTrue(array_key_exists('send_date', $json['notifications'][0])); $this->assertTrue($json['notifications'][0]['ignore_user_timezone']); $this->assertEquals('CONTENT', $json['notifications'][0]['content']); $this->assertCount(3, $json['notifications'][0]['data']); $this->assertEquals('DATA_PARAMETER_1_VALUE', $json['notifications'][0]['data']['DATA_PARAMETER_1']); $this->assertEquals('DATA_PARAMETER_2_VALUE', $json['notifications'][0]['data']['DATA_PARAMETER_2']); $this->assertEquals('DATA_PARAMETER_3_VALUE', $json['notifications'][0]['data']['DATA_PARAMETER_3']); $this->assertCount(3, $json['notifications'][0]['devices']); $this->assertEquals('DEVICE_TOKEN_1', $json['notifications'][0]['devices'][0]); $this->assertEquals('DEVICE_TOKEN_2', $json['notifications'][0]['devices'][1]); $this->assertEquals('DEVICE_TOKEN_3', $json['notifications'][0]['devices'][2]); }