/** * Retrieve some User from a search * * @param UserSearch $search The search * * @return PaginatedResponse * * @todo The informations about pagination is lost for the moment. */ public function search(UserSearch $search) { $response = $this->send(new Request('GET', self::INTERCOM_BASE_URL . '/users', $search->format()))->json(); $users = []; foreach ($response['users'] as $userData) { $user = new UserObject($this->accessor->getValue($userData, '[user_id]'), $this->accessor->getValue($userData, '[email]')); $users[] = $this->hydrate($user, $userData); } return new PaginatedResponse($users, $response['pages']['page'], $response['pages']['next'], $response['pages']['total_pages'], $response['total_count']); }
/** * @covers ::search() */ public function testSearch() { $search = new UserSearch(); $clientRequest = $this->getMock('GuzzleHttp\\Message\\RequestInterface'); $apiResponseData = ['users' => [$this->user, $this->user], 'pages' => ['page' => 1, 'next' => 2, 'total_pages' => 10], 'total_count' => 100]; $response = $this->getMock('GuzzleHttp\\Message\\ResponseInterface'); $response->expects(self::once())->method('json')->will(self::returnValue($apiResponseData)); $client = $this->getMockBuilder('GuzzleHttp\\ClientInterface')->disableOriginalConstructor()->getMock(); $client->expects(self::once())->method('createRequest')->with('GET', UserClient::INTERCOM_BASE_URL . '/users', ['headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'], 'query' => $search->format(), 'auth' => [$this->appId, $this->apiKey]])->will(self::returnValue($clientRequest)); $client->expects(self::once())->method('send')->with($clientRequest)->will(self::returnValue($response)); $paginatedResponse = (new UserClient($this->appId, $this->apiKey, $client))->search($search); $this->assertInstanceOf('Intercom\\Request\\PaginatedResponse', $paginatedResponse); $this->assertInstanceOf('Intercom\\Object\\User', $paginatedResponse->getContent()[0]); $this->assertEquals('7902', $paginatedResponse->getContent()[0]->getUserId()); $this->assertInstanceOf('Intercom\\Object\\User', $paginatedResponse->getContent()[1]); $this->assertEquals('7902', $paginatedResponse->getContent()[1]->getUserId()); $this->assertEquals(1, $paginatedResponse->getPage()); $this->assertEquals(2, $paginatedResponse->getNextPage()); $this->assertEquals(10, $paginatedResponse->getTotalPages()); $this->assertEquals(100, $paginatedResponse->getTotalCount()); }