2017-08-16 06:16:00 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 04:43:01 +02:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-16 06:16:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Assertions;
|
|
|
|
|
2017-09-02 07:21:15 +02:00
|
|
|
use Illuminate\View\View;
|
2017-09-03 23:41:03 +02:00
|
|
|
use Illuminate\Http\Response;
|
2017-09-16 08:45:56 +02:00
|
|
|
use PHPUnit\Framework\Assert;
|
2017-09-03 23:41:03 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-09-02 07:21:15 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2017-08-16 06:16:00 +02:00
|
|
|
|
|
|
|
trait ControllerAssertionsTrait
|
|
|
|
{
|
2017-09-03 01:56:15 +02:00
|
|
|
/**
|
|
|
|
* Assert that a response is an instance of Illuminate View.
|
|
|
|
*
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertIsViewResponse($response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertInstanceOf(View::class, $response);
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a response is an instance of Illuminate Redirect Response.
|
|
|
|
*
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertIsRedirectResponse($response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertInstanceOf(RedirectResponse::class, $response);
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a response is an instance of Illuminate Json Response.
|
|
|
|
*
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertIsJsonResponse($response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertInstanceOf(JsonResponse::class, $response);
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a response is an instance of Illuminate Response.
|
|
|
|
*
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertIsResponse($response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertInstanceOf(Response::class, $response);
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 06:16:00 +02:00
|
|
|
/**
|
|
|
|
* Assert that a view name equals the passed name.
|
|
|
|
*
|
2017-09-03 01:56:15 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $view
|
2017-08-16 06:16:00 +02:00
|
|
|
*/
|
|
|
|
public function assertViewNameEquals($name, $view)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals($name, $view->getName());
|
2017-08-16 06:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a view name does not equal a provided name.
|
|
|
|
*
|
2017-09-03 01:56:15 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $view
|
2017-08-16 06:16:00 +02:00
|
|
|
*/
|
|
|
|
public function assertViewNameNotEquals($name, $view)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertNotEquals($name, $view->getName());
|
2017-08-16 06:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a view has an attribute passed into it.
|
|
|
|
*
|
2017-09-03 01:56:15 +02:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $view
|
2017-08-16 06:16:00 +02:00
|
|
|
*/
|
|
|
|
public function assertViewHasKey($attribute, $view)
|
|
|
|
{
|
2017-09-02 07:21:15 +02:00
|
|
|
if (str_contains($attribute, '.')) {
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertNotEquals(
|
2017-09-02 07:21:15 +02:00
|
|
|
'__TEST__FAIL',
|
|
|
|
array_get($view->getData(), $attribute, '__TEST__FAIL')
|
|
|
|
);
|
|
|
|
} else {
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertArrayHasKey($attribute, $view->getData());
|
2017-09-02 07:21:15 +02:00
|
|
|
}
|
2017-08-16 06:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a view does not have a specific attribute passed in.
|
|
|
|
*
|
2017-09-03 01:56:15 +02:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $view
|
2017-08-16 06:16:00 +02:00
|
|
|
*/
|
|
|
|
public function assertViewNotHasKey($attribute, $view)
|
|
|
|
{
|
2017-09-02 07:21:15 +02:00
|
|
|
if (str_contains($attribute, '.')) {
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals(
|
2017-09-02 07:21:15 +02:00
|
|
|
'__TEST__PASS',
|
|
|
|
array_get($view->getData(), $attribute, '__TEST__PASS')
|
|
|
|
);
|
|
|
|
} else {
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertArrayNotHasKey($attribute, $view->getData());
|
2017-09-02 07:21:15 +02:00
|
|
|
}
|
2017-08-16 06:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a view attribute equals a given parameter.
|
|
|
|
*
|
2017-09-03 01:56:15 +02:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @param mixed $view
|
2017-08-16 06:16:00 +02:00
|
|
|
*/
|
|
|
|
public function assertViewKeyEquals($attribute, $value, $view)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
|
2017-09-02 07:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a view attribute does not equal a given parameter.
|
|
|
|
*
|
2017-09-03 01:56:15 +02:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @param mixed $view
|
2017-09-02 07:21:15 +02:00
|
|
|
*/
|
|
|
|
public function assertViewKeyNotEquals($attribute, $value, $view)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertNotEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
|
2017-08-16 06:16:00 +02:00
|
|
|
}
|
2017-08-31 04:11:14 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-03 01:56:15 +02:00
|
|
|
* Assert that a route redirect equals a given route name.
|
|
|
|
*
|
|
|
|
* @param string $route
|
|
|
|
* @param mixed $response
|
2017-09-05 01:12:13 +02:00
|
|
|
* @param array $args
|
2017-08-31 04:11:14 +02:00
|
|
|
*/
|
2017-09-05 01:12:13 +02:00
|
|
|
public function assertRedirectRouteEquals($route, $response, array $args = [])
|
2017-08-31 04:11:14 +02:00
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals(route($route, $args), $response->getTargetUrl());
|
2017-08-31 04:11:14 +02:00
|
|
|
}
|
2017-09-03 01:56:15 +02:00
|
|
|
|
2017-09-03 23:32:52 +02:00
|
|
|
/**
|
|
|
|
* Assert that a route redirect URL equals as passed URL.
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertRedirectUrlEquals($url, $response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals($url, $response->getTargetUrl());
|
2017-09-03 23:32:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 01:56:15 +02:00
|
|
|
/**
|
|
|
|
* Assert that a response code equals a given code.
|
|
|
|
*
|
|
|
|
* @param int $code
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertResponseCodeEquals($code, $response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals($code, $response->getStatusCode());
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a response code does not equal a given code.
|
|
|
|
*
|
|
|
|
* @param int $code
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertResponseCodeNotEquals($code, $response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertNotEquals($code, $response->getStatusCode());
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a response is in a JSON format.
|
|
|
|
*
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertResponseHasJsonHeaders($response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals('application/json', $response->headers->get('content-type'));
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that response JSON matches a given JSON string.
|
|
|
|
*
|
|
|
|
* @param array|string $json
|
|
|
|
* @param mixed $response
|
|
|
|
*/
|
|
|
|
public function assertResponseJsonEquals($json, $response)
|
|
|
|
{
|
2017-09-16 08:45:56 +02:00
|
|
|
Assert::assertEquals(is_array($json) ? json_encode($json) : $json, $response->getContent());
|
2017-09-03 01:56:15 +02:00
|
|
|
}
|
2017-08-16 06:16:00 +02:00
|
|
|
}
|