<commit_before><?php

namespace Lenddo\tests\cases;

use Lenddo\tests\mocks\ServiceClientMock;

class ServiceClientTest extends \PHPUnit_Framework_TestCase
{
	const API_USER = 'foo';
	const API_SECRET = 'bar';
	const CLIENT_ID = 'CLIENT_ID_123';

	protected function _buildServiceClient($options = array())
	{
		return new ServiceClientMock(static::API_USER, static::API_SECRET, $options);
	}

	public function testClientInstantiation()
	{
		$endpoint = 'http://foo.bar';
		$guzzle_request_options = array(
			'verify' => false // disable ssl verification
		);

		$client = $this->_buildServiceClient(array(
			'hosts' => array(
				'score_service' => $endpoint
			),
			'guzzle_request_options' => $guzzle_request_options
		));

		$this->assertInstanceOf('Lenddo\ServiceClient', $client);
		$this->assertEquals(static::API_USER, $client->getApiAppId());
		$this->assertEquals(static::API_SECRET, $client->getApiSecret());
		$this->assertEquals($endpoint, $client->getHosts()['score_service']);
		$this->assertEquals($guzzle_request_options, $client->getGuzzleRequestOptions());
	}

	public function testClientScore()
	{
		$expect_path = '/ClientScore/' . static::CLIENT_ID;

		$mock_result = $this->_buildServiceClient()->clientScore(static::CLIENT_ID);
		$request_options = $this->_testResultGetRequestOptions($mock_result, 'GET', $expect_path);

		// Analyze Headers
		$this->assertEquals(array(
			'headers' => array(
				'Authorization' => 'LENDDO foo:<KEY>
				'Content-Type' => 'application/json',
				'Date' => 'Sun Oct 4 21:45:10 CEST 2015',
				'Connection' => 'close'
			)
		), $request_options);
	}

	public function testClientVerification()
	{
		$expect_path = '/ClientVerification/' . static::CLIENT_ID;
		$mock_result = $this->_buildServiceClient()->clientVerification(static::CLIENT_ID);
		$request_options = $this->_testResultGetRequestOptions($mock_result, 'GET', $expect_path);

		$this->assertEquals(array(
			'headers' => array(
				'Authorization' => 'LENDDO foo:zj+<KEY>
				'Content-Type' => 'application/json',
				'Date' => 'Sun Oct 4 21:45:10 CEST 2015',
				'Connection' => 'close'
			)
		), $request_options);
	}

	/**
	 * Exists for purposes of DRY code. This doesn't change from test to test so no reason to keep re-writing it.
	 *
	 * @param $mock_result \Lenddo\tests\mocks\GuzzleClientMock
	 * @param $expect_method
	 * @param $expect_path
	 * @return mixed
	 */
	protected function _testResultGetRequestOptions($mock_result, $expect_method, $expect_path)
	{
		list($method, $path, $request_options) = $mock_result->getRequestArgs();<commit_msg>WhiteLabelClient - adding $client_id to the partnerToken service call. README - updating documentation to reflect the latest functionality.<commit_after><?php

namespace Lenddo\tests\cases;

use Lenddo\tests\mocks\ServiceClientMock;

class ServiceClientTest extends \PHPUnit_Framework_TestCase
{
	const API_USER = 'foo';
	const API_SECRET = 'bar';
	const CLIENT_ID = 'CLIENT_ID_123';

	protected function _buildServiceClient($options = array())
	{
		return new ServiceClientMock(static::API_USER, static::API_SECRET, $options);
	}

	public function testClientInstantiation()
	{
		$endpoint = 'http://foo.bar';
		$guzzle_request_options = array(
			'verify' => false // disable ssl verification
		);

		$client = $this->_buildServiceClient(array(
			'hosts' => array(
				'score_service' => $endpoint
			),
			'guzzle_request_options' => $guzzle_request_options
		));

		$this->assertInstanceOf('Lenddo\ServiceClient', $client);
		$this->assertEquals(static::API_USER, $client->getApiAppId());
		$this->assertEquals(static::API_SECRET, $client->getApiSecret());
		$this->assertEquals($endpoint, $client->getHosts()['score_service']);
		$this->assertEquals($guzzle_request_options, $client->getGuzzleRequestOptions());
	}

	public function testClientScore()
	{
		$expect_path = '/ClientScore/' . static::CLIENT_ID;

		$mock_result = $this->_buildServiceClient()->clientScore(static::CLIENT_ID);
		$request_options = $this->_testResultGetRequestOptions($mock_result, 'GET', $expect_path);

		// Analyze Headers
		$this->assertEquals(array(
			'headers' => array(
				'Authorization' => 'LENDDO foo:4hVTKBeF+2ZSc7jxhfQ/iF8jj2w=',
				'Content-Type' => 'application/json',
				'Date' => 'Sun Oct 4 21:45:10 CEST 2015',
				'Connection' => 'close'
			),
			'body' => null
		), $request_options);
	}

	public function testClientVerification()
	{
		$expect_path = '/ClientVerification/' . static::CLIENT_ID;
		$mock_result = $this->_buildServiceClient()->clientVerification(static::CLIENT_ID);
		$request_options = $this->_testResultGetRequestOptions($mock_result, 'GET', $expect_path);

		$this->assertEquals(array(
			'headers' => array(
				'Authorization' => 'LENDDO foo:zj+dYWKbSox8AOPv1MiLt91hUDo=',
				'Content-Type' => 'application/json',
				'Date' => 'Sun Oct 4 21:45:10 CEST 2015',
				'Connection' => 'close'
			),
			'body' => null
		), $request_options);
	}

	/**
	 * Exists for purposes of DRY code. This doesn't change from test to test so no reason to keep re-writing it.
	 *
	 * @param $mock_result \Lenddo\tests\mocks\GuzzleClientMock
	 * @param $expect_method
	 * @param $expect_path
	 * @return mixed
	 */
	protected function _testResultGetRequestOptions($mock_result, $expect_method, $expect_path)
	{
		list($method, $path, $request_options) = $mock_result->getRequestArgs();
{'fraction_non_alphanumeric': 0.15067982864593035, 'fraction_numerical': 0.01098901098901099, 'mean_word_length': 5.193771626297578, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988396', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1751, 'n_words': 332}