/** * @param AuthConfig $config * * @return TokenConfig * @throws AuthenticationException */ public function auth(AuthConfig $config) { $response = $this->client->post($config->getBaseUrl() . '/auth', array('body' => array('username' => $config->getUsername(), 'password' => $config->getPassword()))); $data = $response->json(); if (isset($data['error']) && isset($data['code'])) { throw new AuthenticationException($data['error'], $data['code']); } return new TokenConfig($data['token']); }
public function testShouldReturnTokenConfigIfSuccessAuth() { $authConfig = new AuthConfig('username', 'password'); $response = $this->getMock('GuzzleHttp\\Message\\ResponseInterface'); $response->expects($this->once())->method('json')->willReturn(array("token" => "some_token")); $this->client->expects($this->once())->method('post')->with($authConfig->getBaseUrl() . '/auth', array('body' => array('username' => 'username', 'password' => 'password')))->willReturn($response); $tokenConfig = $this->authentication->auth($authConfig); $this->assertInstanceOf('Rosello\\T411\\Config\\TokenConfig', $tokenConfig); $this->assertSame('some_token', $tokenConfig->getToken()); }