Skip to content

Commit

Permalink
チャットルームのメッセージを取得する部分の実装
Browse files Browse the repository at this point in the history
  • Loading branch information
polidog committed Dec 25, 2014
1 parent 7770e83 commit f5f5858
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Api/Rooms/Messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Polidog\Chatwork\Api\Rooms;


use Polidog\Chatwork\Collection\CollectionInterface;
use Polidog\Chatwork\Entity\Message;

/**
* Class Messages
* @package Polidog\Chatwork\Api\Rooms
*/
class Messages extends AbstractRoomApi
{
/**
* @param bool $force
* @return CollectionInterface
*/
public function show($force = false)
{
return $this->factory->collection(
$this->client->get(
['rooms/{roomId}/messages',['roomId' => $this->roomId]],
[
'query' => [
'force' => (int)$force
]
]
)->json()
);
}

/**
* @param $id
* @param bool $force
* @return Message
*/
public function detail($id, $force = false)
{
return $this->factory->entity(
$this->client->get(
['rooms/{roomId}/messages/{id}',
[
'roomId' => $this->roomId,
'id' => $id
]
],
[
'query' => [
'force' => (int)$force
]
]
)->json()
);
}
}
87 changes: 87 additions & 0 deletions tests/Api/Rooms/MessagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace Polidog\Chatwork\Api\Rooms;


use GuzzleHttp\ClientInterface;
use GuzzleHttp\Message\ResponseInterface;

use Phake;
use Polidog\Chatwork\Entity\Factory\FactoryInterface;

class MessagesTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function 一覧を取得する()
{
$httpClient = Phake::mock(ClientInterface::class);
$response = Phake::mock(ResponseInterface::class);
$factory = Phake::mock(FactoryInterface::class);

Phake::when($httpClient)
->get($this->isType('array'), $this->isType('array'))
->thenReturn($response);

Phake::when($response)
->json()
->thenReturn([]);

$messages = new Messages(1, $httpClient, $factory);
$messages->show();

Phake::verify($httpClient,Phake::times(1))
->get(
['rooms/{roomId}/messages',['roomId' => 1]],
[
'query' => [
'force' => 0
]
]
);

Phake::verify($response,Phake::times(1))
->json();

Phake::verify($factory,Phake::times(1))
->collection($this->isType('array'));

}

/**
* @test
*/
public function idを指定してメッセージを取得する()
{
$httpClient = Phake::mock(ClientInterface::class);
$response = Phake::mock(ResponseInterface::class);
$factory = Phake::mock(FactoryInterface::class);

Phake::when($httpClient)
->get($this->isType('array'), $this->isType('array'))
->thenReturn($response);

Phake::when($response)
->json()
->thenReturn([]);

$messages = new Messages(1, $httpClient, $factory);
$messages->detail(1);

Phake::verify($httpClient,Phake::times(1))
->get(
['rooms/{roomId}/messages/{id}',['roomId' => 1,'id' => 1]],
[
'query' => [
'force' => 0
]
]
);

Phake::verify($response,Phake::times(1))
->json();

Phake::verify($factory,Phake::times(1))
->entity($this->isType('array'));
}
}

0 comments on commit f5f5858

Please sign in to comment.