Page MenuHomec4science

TequilaClientTest.php
No OneTemporary

File Metadata

Created
Thu, Dec 12, 16:22

TequilaClientTest.php

<?php declare(strict_types=1);
// Copyright (C) 2021 Doran Kayoumi
// Copyright (C) 2021 Liip SA <https://www.liip.ch>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public Licensáe
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\ConnectException;
require_once('tequila.php');
require_once("exception.php");
final class TequilaClientTest extends TestCase {
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
private function invokeNonPublicMethod(&$object, $methodName, array $parameters = array()) {
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
public function testCreateRequest() {
$mock = new MockHandler([
new Response(200, ["X-Foo" => "Bar"], "key=veryRandomKey"),
]);
$client = new TequilaClient(
"https://tequila.server.lo",
0,
"Unit testing the client",
"http://localhost/",
1,
$mock
);
$key = $this->invokeNonPublicMethod($client, "createRequest");
$this->assertEquals($key, "veryRandomKey");
}
public function testFetchAttributes() {
$mock = new MockHandler([
new Response(200, ["X-Foo" => "Bar"], "attr1=val1\nattr2=val2\nattr3=val3"),
]);
$client = new TequilaClient(
"https://tequila.server.lo",
0,
"Unit testing the client",
"http://localhost/",
1,
$mock
);
$attributes = $this->invokeNonPublicMethod($client, "fetchAttributes", [
"veryRandomKey"
]);
$this->assertEquals($attributes, [
"attr1" => "val1",
"attr2" => "val2",
"attr3" => "val3",
]);
}
public function testContactServerWithUnknownEndpoint() {
$client = new TequilaClient(
"https://tequila.server.lo",
0,
"Unit testing the client",
"http://localhost/",
1,
);
$this->expectException(TequilaException::class);
$this->expectExceptionMessage("Unknown endpoint");
$this->invokeNonPublicMethod($client, "contactServer", [
"invalid endpoint"
]);
}
public function testContactServerWithUnknownServer() {
$mock = new MockHandler([
new ConnectException('Error Communicating with Server', new Request('POST', 'fetchattributes'))
]);
$client = new TequilaClient(
"https://tequila.server.lo",
0,
"Unit testing the client",
"http://localhost/",
1,
$mock
);
$this->expectException(TequilaException::class);
$this->expectExceptionMessage("Connection to server failed");
$this->invokeNonPublicMethod($client, "contactServer", [
"fetchattributes"
]);
}
public function testContactServerUnexpectedHTTPCode() {
$mock = new MockHandler([
new Response(458, ["X-Foo" => "Bar"], ""),
]);
$client = new TequilaClient(
"https://tequila.server.lo",
0,
"Unit testing the client",
"http://localhost/",
1,
$mock
);
$this->expectException(TequilaException::class);
$this->expectExceptionMessage("Unexpected return from server");
$this->invokeNonPublicMethod($client, "contactServer", [
"fetchattributes"
]);
}
}

Event Timeline