firefly-iii/tests/Traits/TestHelpers.php

147 lines
5.3 KiB
PHP
Raw Normal View History

2020-07-30 14:16:14 -05:00
<?php
/*
* TestHelpers.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Traits;
2021-03-13 05:01:01 -06:00
2020-07-30 14:16:14 -05:00
/**
* Trait TestHelpers
*/
trait TestHelpers
{
2021-03-19 00:12:28 -05:00
/**
* @param string $route
* @param array $content
*/
2021-03-20 01:21:13 -05:00
protected function assertPOST(string $route, array $content): void
2021-03-19 00:12:28 -05:00
{
$submission = $content['submission'];
$expected = $content['expected'];
$ignore = $content['ignore'];
// submit body
$response = $this->post($route, $submission, ['Accept' => 'application/json']);
$responseBody = $response->content();
$responseJson = json_decode($responseBody, true);
$status = $response->getStatusCode();
$this->assertEquals($status, 200, sprintf("Submission:\n%s\nResponse: %s", json_encode($submission), $responseBody));
$response->assertHeader('Content-Type', 'application/vnd.api+json');
// get return and compare each field
$responseAttributes = $responseJson['data']['attributes'];
2021-03-20 01:21:13 -05:00
$this->comparePOSTArray($submission, $responseAttributes, $expected, $ignore);
2021-03-19 00:12:28 -05:00
// ignore fields too!
}
/**
* @param array $submission
* @param array $response
* @param array $expected
* @param array $ignore
*/
2021-03-20 01:21:13 -05:00
private function comparePOSTArray(array $submission, array $response, array $expected, array $ignore): void
2021-03-19 00:12:28 -05:00
{
foreach ($response as $key => $value) {
if (is_array($value) && array_key_exists($key, $expected) && is_array($expected[$key])) {
2021-03-20 01:21:13 -05:00
$this->comparePOSTArray($submission, $value, $expected[$key], $ignore[$key] ?? []);
2021-03-19 00:12:28 -05:00
}
if (isset($expected[$key])) {
if (in_array($key, $ignore, true)) {
continue;
}
if (!in_array($key, $ignore, true)) {
$message = sprintf(
"Field '%s' with value %s is expected to be %s.\nSubmitted:\n%s\nIgnored: %s\nReturned\n%s",
$key,
var_export($value, true),
var_export($expected[$key], true),
json_encode($submission),
json_encode($ignore),
json_encode($response)
);
$this->assertEquals($value, $expected[$key], $message);
}
}
}
}
/**
* @param string $route
* @param array $content
*/
2021-03-20 01:21:13 -05:00
protected function assertPUT(string $route, array $content): void
2021-03-20 01:02:06 -05:00
{
$submission = $content['submission'];
$ignore = $content['ignore'];
$response = $this->put($route, $submission, ['Accept' => 'application/json']);
$responseBody = $response->content();
$responseJson = json_decode($responseBody, true);
$status = $response->getStatusCode();
$this->assertEquals($status, 200, sprintf("Submission:\n%s\nResponse: %s", json_encode($submission), $responseBody));
$response->assertHeader('Content-Type', 'application/vnd.api+json');
// get return and compare each field
$responseAttributes = $responseJson['data']['attributes'];
2021-03-20 01:21:13 -05:00
$this->comparePUTArray($route, $submission, $responseAttributes, $ignore);
2021-03-20 01:02:06 -05:00
}
/**
* @param string $url
2021-03-20 01:21:13 -05:00
* @param array $submission
* @param array $response
* @param array $ignore
2021-03-20 01:02:06 -05:00
*/
2021-03-20 01:21:13 -05:00
private function comparePUTArray(string $url, array $submission, array $response, array $ignore): void
2021-03-20 01:02:06 -05:00
{
foreach ($response as $key => $value) {
if (is_array($value) && array_key_exists($key, $submission) && is_array($submission[$key])) {
2021-03-20 01:21:13 -05:00
$this->comparePUTArray($url, $submission[$key], $value, $ignore[$key] ?? []);
2021-03-20 01:02:06 -05:00
}
if (isset($submission[$key])) {
if (in_array($key, $ignore, true)) {
continue;
}
if (!in_array($key, $ignore, true)) {
$message = sprintf(
"Field '%s' with value %s is expected to be %s.\nSubmitted:\n%s\nIgnored: %s\nReturned\n%s\nURL: %s",
$key,
var_export($value, true),
var_export($submission[$key], true),
json_encode($submission),
json_encode($ignore),
json_encode($response),
$url
);
$this->assertEquals($value, $submission[$key], $message);
}
}
}
}
2020-07-30 14:16:14 -05:00
}