/** * Test reopen connection */ public function testReopenConnection() { $connection = $this->getMock('Apple\\ApnPush\\Notification\\Connection', array('isReadyRead', 'create', 'read', 'write', 'close')); $connectionResource = new \ReflectionProperty($connection, 'resource'); $connectionResource->setAccessible(true); $connection->expects($this->any())->method('create')->will($this->returnCallback(function () use($connection, $connectionResource) { $connectionResource->setValue($connection, true); })); $connection->expects($this->any())->method('close')->will($this->returnCallback(function () use($connection, $connectionResource) { $connectionResource->setValue($connection, null); })); $connection->expects($this->any())->method('write')->will($this->returnCallback(function ($data) { return strlen($data); })); $connection->expects($this->any())->method('isReadyRead')->will($this->returnCallback(function () use($connection) { return !empty($connection->__IsError__); })); $connection->expects($this->any())->method('read')->with(6)->will($this->returnValue(null)); $notification = new Notification($connection); // Send message without error $status = $notification->sendMessage(str_repeat('af', 32), 'Hello world'); $this->assertTrue($status); $this->assertTrue($notification->getConnection()->is()); // Send message with error and check close connection $connection->__IsError__ = true; try { $notification->sendMessage(str_repeat('af', 32), 'Hello world'); } catch (SendException $e) { } $this->assertFalse($notification->getConnection()->is()); // Send message without error and check reopen connection $connection->__IsError__ = false; $status = $notification->sendMessage(str_repeat('af', 32), 'Hello world'); $this->assertTrue($status); $this->assertTrue($notification->getConnection()->is()); }
<?php include_once __DIR__ . '/../autoload.php'; if (!interface_exists('Psr\\Log\\LoggerInterface')) { \Demo::error('Please install "psr/log" for run this demo (Notification with logger system).'); } include_once __DIR__ . '/CustomLogger.php'; use Apple\ApnPush\Certificate\Certificate; use Apple\ApnPush\Notification\Notification; use Apple\ApnPush\Notification\Connection; use Apple\ApnPush\Notification\SendException; // Create connection $certificate = new Certificate(CERTIFICATE_FILE, PASS_PHRASE); $connection = new Connection($certificate, SANDBOX_MODE); // Create custom logger $logger = new CustomLogger(); // Create notification $notification = new Notification(); $notification->setLogger($logger)->setConnection($connection); // Send to correct device token $notification->sendMessage(DEVICE_TOKEN, '[Correct] Hello'); try { // Send to incorrect device token $notification->sendMessage(str_repeat('a', 64), '[Incorrect] Hello'); } catch (SendException $e) { } $notification->sendMessage(DEVICE_TOKEN, '[Correct] Hello 2 ;)');