Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inrel 5868 tests #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/TipserClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(ClientInterface $httpClient, ConfigFactoryInterface
* @return \Psr\Http\Message\ResponseInterface
* @throws GuzzleException
*/
protected function callAPI($params, $items, &$messages) {
public function callAPI($params, $items, &$messages) {
$tipser_pos = $this->config->get('tipser_pos');

if (isset($params['product_id'])) {
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Mocks/kleid.json

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions tests/Unit/TipserClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\Tests\tipser_client\Unit;

use Drupal\Component\Serialization\Json;
use Drupal\Tests\UnitTestCase;
use Drupal\tipser_client\TipserClient;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;


/**
* @file
* PHPUnit tests for the Tipser Client.
*/
class TipserClientTest extends UnitTestCase {

/**
* Mock of http_client service.
*
* @var \GuzzleHttp\Client
*/
protected $mockHttp;

/**
* Mock handler.
*
* @var \Drupal\Core\State\State
*/
protected $mockhandler;

/**
* Mock of config.factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $mockConfigFactory;

/**
* String translation mock.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stringTranslation;

/**
* Set up test environment.
*/
public function setUp() {
parent::setUp();

$this->mockHandler = new MockHandler();
$handler = HandlerStack::create($this->mockHandler);
$this->mockHttp = new HttpClient(['handler' => $handler]);
$this->stringTranslation = $this->getStringTranslationStub();

$config = [
'tipser_client.config' => [
'tipser_activated' => true,
'tipser_shopping_cart_icon_activated' => true,
'shop_url' => 'https => //www.tipser.com',
'tipser_api' => 'https => //t3-prod-api.tipser.com',
'tipser_pos' => 'foo',
'tipser_apikey' => 'very_long_key',
'tipser_env' => 'prod',
'vocabulary' => 'tipser_categories',
],
];
$mockConfigFactory = $this->getConfigFactoryStub($config);

$this->mockConfigFactory = $mockConfigFactory;
}

/**
* Test the callAPI method.
*/
public function testCallAPI() {

$body = file_get_contents(__DIR__ . '/Mocks/kleid.json');
// This sets up the mock client to respond to the first request it gets
// with an HTTP 200 containing your mock json body.
$this->mockHandler->append(new Response(200, [], $body));

$tipser = new TipserClient(
$this->mockHttp,
$this->mockConfigFactory,
);

$params = $items = $messages = [];
$params['product_id'] = '1234';
$tipser_response = $tipser->callAPI($params, $items, $messages);
$tipser_result = Json::decode($tipser_response->getBody());

$this->assertNotEmpty($tipser_result);
$this->assertNotEmpty($messages['url']);
$this->assertEquals($tipser_result, Json::decode($body));
}
}