2017-07-08 01:06:38 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-07-08 01:06:38 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\Test;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-08 01:06:38 +00:00
|
|
|
use JKingWeb\Arsse\Exception;
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
2018-01-04 04:13:08 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
|
|
|
use Zend\Diactoros\Response\EmptyResponse;
|
2017-07-08 01:06:38 +00:00
|
|
|
|
2017-07-20 22:36:03 +00:00
|
|
|
/** @coversNothing */
|
2017-07-08 01:06:38 +00:00
|
|
|
abstract class AbstractTest extends \PHPUnit\Framework\TestCase {
|
2017-08-29 14:50:31 +00:00
|
|
|
public function assertException(string $msg = "", string $prefix = "", string $type = "Exception") {
|
|
|
|
if (func_num_args()) {
|
2017-07-08 01:06:38 +00:00
|
|
|
$class = \JKingWeb\Arsse\NS_BASE . ($prefix !== "" ? str_replace("/", "\\", $prefix) . "\\" : "") . $type;
|
|
|
|
$msgID = ($prefix !== "" ? $prefix . "/" : "") . $type. ".$msg";
|
2017-08-29 14:50:31 +00:00
|
|
|
if (array_key_exists($msgID, Exception::CODES)) {
|
2017-07-08 01:06:38 +00:00
|
|
|
$code = Exception::CODES[$msgID];
|
|
|
|
} else {
|
|
|
|
$code = 0;
|
|
|
|
}
|
|
|
|
$this->expectException($class);
|
|
|
|
$this->expectExceptionCode($code);
|
|
|
|
} else {
|
|
|
|
// expecting a standard PHP exception
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:13:08 +00:00
|
|
|
protected function assertResponse(ResponseInterface $exp, ResponseInterface $act, string $text = null) {
|
|
|
|
$this->assertEquals($exp->getHeaders(), $act->getHeaders(), $text);
|
|
|
|
$this->assertEquals($exp->getStatusCode(), $act->getStatusCode(), $text);
|
|
|
|
$this->assertInstanceOf(get_class($exp), $act);
|
|
|
|
if ($exp instanceof JsonResponse) {
|
|
|
|
$this->assertEquals($exp->getPayload(), $act->getPayload(), $text);
|
|
|
|
$this->assertSame($exp->getPayload(), $act->getPayload(), $text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 21:00:23 +00:00
|
|
|
public function approximateTime($exp, $act) {
|
|
|
|
if (is_null($act)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$target = Date::normalize($exp)->getTimeStamp();
|
|
|
|
$value = Date::normalize($act)->getTimeStamp();
|
|
|
|
if ($value >= ($target - 1) && $value <= ($target + 1)) {
|
|
|
|
// if the actual time is off by no more than one second, it's acceptable
|
|
|
|
return $exp;
|
|
|
|
} else {
|
|
|
|
return $act;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 02:25:45 +00:00
|
|
|
public function assertTime($exp, $test, string $msg = null) {
|
2017-12-08 21:00:23 +00:00
|
|
|
$test = $this->approximateTime($exp, $test);
|
2017-07-17 11:47:57 +00:00
|
|
|
$exp = Date::transform($exp, "iso8601");
|
|
|
|
$test = Date::transform($test, "iso8601");
|
2017-09-28 02:25:45 +00:00
|
|
|
$this->assertSame($exp, $test, $msg);
|
2017-07-08 01:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function clearData(bool $loadLang = true): bool {
|
2017-12-16 23:30:45 +00:00
|
|
|
date_default_timezone_set("America/Toronto");
|
2017-07-17 11:47:57 +00:00
|
|
|
$r = new \ReflectionClass(\JKingWeb\Arsse\Arsse::class);
|
2017-07-08 01:06:38 +00:00
|
|
|
$props = array_keys($r->getStaticProperties());
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($props as $prop) {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$$prop = null;
|
2017-07-08 01:06:38 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($loadLang) {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$lang = new \JKingWeb\Arsse\Lang();
|
2017-07-08 01:06:38 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|