/** * Send command * * @param Client $client Pushy client * * @return null|string null if no receipt, string if there is */ public function send(Client $client) { // Init api params $apiParams = array_merge(['token' => $client->getApiToken(), 'user' => $this->message->getUser()->getId(), 'device' => $this->message->getUser()->getDeviceName(), 'message' => $this->message->getMessage(), 'title' => $this->message->getTitle(), 'url' => $this->message->getUrl(), 'url_title' => $this->message->getUrlTitle(), 'timestamp' => $this->message->getTimestamp()], $this->message->getPriority()->getApiParameters()); // Set optional sound if ($sound = $this->message->getSound()) { $apiParams['sound'] = (string) $sound; } // Create request message $requestMessage = (new RequestMessage())->setMethod('POST')->setPath('messages.json'); // Set API params foreach ($apiParams as $param => $value) { // Skip param if null if ($value === null) { continue; } $requestMessage->setPostBodyField($param, $value); } // Send request $response = $client->getTransport()->sendRequest($requestMessage); // Return receipt if there is one if (isset($response->receipt)) { return $response->receipt; } }
/** * Set up * * @covers \Pushy\Client::__construct */ public function setUp() { // Set up mock user $this->mockUser = Mockery::mock('\\Pushy\\User')->shouldIgnoreMissing(); // Set mock priority $this->mockPriority = Mockery::mock('\\Pushy\\Priority\\PriorityInterface')->shouldIgnoreMissing()->shouldReceive('getApiParameters')->andReturn([])->getMock(); // Set up mock message $this->mockMessage = Mockery::mock('\\Pushy\\Message')->shouldIgnoreMissing(); $this->mockMessage->shouldReceive('getUser')->andReturn($this->mockUser); $this->mockMessage->shouldReceive('getPriority')->andReturn($this->mockPriority); // Set mock transport $this->mockTransport = Mockery::mock('\\Pushy\\Transport\\TransportInterface')->shouldIgnoreMissing(); // Set up client $this->client = new Client('KzGDORePK8gMaC0QOYAMyEEuzJnyUi'); $this->client->setTransport($this->mockTransport); }
/** * Test: Send command * * @covers \Pushy\Command\SendMessage::send */ public function testSend() { // Stub mock client getAPiToken and getTransport $this->mockClient->shouldReceive('getApiToken')->andReturn('abc')->getMock()->shouldReceive('getTransport')->andReturn($this->mockTransport); // Stub mock transport sendRequest and response object $responseObject = (object) ['receipt' => 'receiptid']; $this->mockTransport->shouldReceive('sendRequest')->andReturn($responseObject); // Stub mock message methods $this->mockMessage->shouldReceive('getUser')->andReturn($this->mockUser)->getMock()->shouldReceive('getSound')->andReturn($this->mockSound)->getMock()->shouldReceive('getPriority')->andReturn($this->mockPriority); // Stub sound $this->mockSound->shouldReceive('__toString')->andReturn('soundname'); // Stub mock priority getApiParameters $this->mockPriority->shouldReceive('getApiParameters')->andReturn([]); // Ensure we get a receipt back $this->assertEquals($responseObject->receipt, $this->sendMessageCommand->send($this->mockClient)); }
/** * Test: Get/Set sound * * @covers \Pushy\Message::getSound * @covers \Pushy\Message::setSound */ public function testGetSetSound() { $this->message->setSound($this->mockSound); $this->assertEquals($this->mockSound, $this->message->getSound()); }