-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTipserClientTest.php
More file actions
100 lines (83 loc) · 2.52 KB
/
TipserClientTest.php
File metadata and controls
100 lines (83 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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));
}
}