adrema/tests/TestCase.php

186 lines
5.6 KiB
PHP
Raw Normal View History

2020-04-10 20:32:12 +02:00
<?php
namespace Tests;
2022-08-12 22:07:59 +02:00
use App\Group;
use App\Member\Member;
2022-02-19 15:18:24 +01:00
use App\Setting\NamiSettings;
use App\User;
2020-04-10 20:32:12 +02:00
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
2022-02-19 15:18:24 +01:00
use Illuminate\Http\RedirectResponse;
2024-04-26 23:20:03 +02:00
use Illuminate\Support\Arr;
2023-02-05 23:35:08 +01:00
use Illuminate\Support\Facades\Http;
2023-12-16 01:13:49 +01:00
use Illuminate\Testing\AssertableJsonString;
2021-07-17 15:58:38 +02:00
use Illuminate\Testing\TestResponse;
2024-10-20 18:25:17 +02:00
use Livewire\Features\SupportTesting\Testable;
2022-10-18 15:04:47 +02:00
use Phake;
2023-12-16 01:13:49 +01:00
use PHPUnit\Framework\Assert;
2023-02-23 22:43:13 +01:00
use Tests\Lib\MakesHttpCalls;
2022-02-12 00:41:52 +01:00
use Tests\Lib\TestsInertia;
2022-02-19 15:18:24 +01:00
use Zoomyboy\LaravelNami\Authentication\Auth;
2024-07-16 00:05:27 +02:00
use Zoomyboy\TableDocument\TestsExcelDocuments;
2020-04-10 20:32:12 +02:00
2024-09-21 18:29:47 +02:00
class TestCase extends BaseTestCase
2020-04-10 20:32:12 +02:00
{
2022-02-12 00:41:52 +01:00
use TestsInertia;
2023-02-23 22:43:13 +01:00
use MakesHttpCalls;
2024-07-16 00:05:27 +02:00
use TestsExcelDocuments;
2021-06-13 11:30:31 +02:00
2022-02-19 15:18:24 +01:00
protected User $me;
2024-09-21 18:29:47 +02:00
protected function setUp(): void
2022-02-12 15:33:16 +01:00
{
2022-02-19 15:18:24 +01:00
parent::setUp();
Auth::fake();
2023-05-16 17:19:56 +02:00
Member::disableGeolocation();
2023-12-16 01:13:49 +01:00
$this->initInertiaTestcase();
2022-01-03 01:17:24 +01:00
}
2024-06-27 10:52:45 +02:00
public function loginNami(int $mglnr = 12345, string $password = 'password', int|Group $groupId = 55): static
2022-02-19 15:18:24 +01:00
{
Auth::success($mglnr, $password);
2023-04-25 00:28:44 +02:00
$group = is_int($groupId)
? Group::factory()->create(['nami_id' => $groupId])
: $groupId;
2023-04-25 00:42:31 +02:00
$this->withNamiSettings($mglnr, $password, $group->nami_id);
2022-08-30 23:39:02 +02:00
return $this;
}
public function withNamiSettings(int $mglnr = 12345, string $password = 'password', int $groupId = 55): self
2022-08-30 23:39:02 +02:00
{
2022-02-19 15:18:24 +01:00
NamiSettings::fake([
'mglnr' => $mglnr,
'password' => $password,
'default_group_id' => $groupId,
2022-02-19 15:18:24 +01:00
]);
return $this;
}
2024-06-27 10:52:45 +02:00
public function login(): static
2022-02-19 15:18:24 +01:00
{
$this->be($user = User::factory()->create());
$this->me = $user;
return $this;
}
public function init(): self
{
Member::factory()->defaults()->create();
return $this;
2021-06-18 23:36:06 +02:00
}
2022-03-06 02:56:22 +01:00
2022-11-16 21:23:31 +01:00
/**
* @param array<string, string> $errors
*/
2022-03-06 02:56:22 +01:00
public function assertErrors(array $errors, TestResponse $response): self
{
2022-02-19 15:18:24 +01:00
$response->assertSessionHas('errors');
$this->assertInstanceOf(RedirectResponse::class, $response->baseResponse);
/** @var RedirectResponse */
$response = $response;
$sessionErrors = $response->getSession()->get('errors')->getBag('default');
foreach ($errors as $key => $value) {
2023-11-30 23:54:16 +01:00
$this->assertTrue($sessionErrors->has($key), "Cannot find key {$key} in errors '" . print_r($sessionErrors, true));
$this->assertEquals($value, $sessionErrors->get($key)[0], "Failed to validate value for session error key {$key}. Actual value: " . print_r($sessionErrors, true));
2022-02-19 15:18:24 +01:00
}
return $this;
}
2022-09-06 17:51:18 +02:00
/**
2024-06-29 11:18:40 +02:00
* @template M of object
* @param class-string<M> $class
2022-09-06 17:51:18 +02:00
*/
public function stubIo(string $class, callable $mocker): self
{
2022-10-18 15:04:47 +02:00
$mock = Phake::mock($class);
2022-09-06 17:51:18 +02:00
$mocker($mock);
app()->instance($class, $mock);
return $this;
}
2023-02-05 23:35:08 +01:00
public function fakeAllHttp(): self
{
Http::fake(['*' => Http::response('', 200)]);
return $this;
}
2023-11-30 23:54:16 +01:00
2023-12-16 01:13:49 +01:00
public function initInertiaTestcase(): void
{
TestResponse::macro('assertInertiaPath', function ($path, $value) {
/** @var TestResponse */
$response = $this;
$props = data_get($response->viewData('page'), 'props');
Assert::assertNotNull($props);
$json = new AssertableJsonString($props);
$json->assertPath($path, $value);
return $this;
});
2023-12-17 22:33:29 +01:00
2024-01-28 11:42:32 +01:00
TestResponse::macro('assertInertiaCount', function ($path, $count) {
/** @var TestResponse */
$response = $this;
$props = data_get($response->viewData('page'), 'props');
Assert::assertNotNull($props);
$json = new AssertableJsonString($props);
$json->assertCount($count, $path);
return $this;
});
2024-06-27 17:50:18 +02:00
TestResponse::macro('assertComponent', function (string $component) {
/** @var TestResponse */
$response = $this;
Assert::assertEquals($component, data_get($response->viewData('page'), 'component'));
return $this;
});
2023-12-17 22:33:29 +01:00
TestResponse::macro('assertPdfPageCount', function (int $count) {
/** @var TestResponse */
$response = $this;
$file = $response->getFile();
Assert::assertTrue(file_exists($file->getPathname()));
exec('pdfinfo ' . escapeshellarg($file->getPathname()) . ' | grep ^Pages | sed "s/Pages:\s*//"', $output, $returnVar);
Assert::assertSame(0, $returnVar, 'Failed to get Pages of PDF File ' . $file->getPathname());
Assert::assertCount(1, $output, 'Failed to parse output format of pdfinfo');
Assert::assertEquals($count, $output[0]);
return $this;
});
TestResponse::macro('assertPdfName', function (string $filename) {
/** @var TestResponse */
$response = $this;
Assert::assertEquals($filename, $response->getFile()->getFilename());
return $this;
});
2024-04-26 23:20:03 +02:00
TestResponse::macro('assertHasJsonPath', function (string $path) {
/** @var TestResponse */
$response = $this;
Assert::assertTrue(Arr::has($response->json(), $path), 'Failed that key ' . $path . ' is in Response.');
return $this;
});
2024-10-20 18:25:17 +02:00
Testable::macro('setArray', function ($attributes) {
$self = $this;
foreach ($attributes as $key => $value) {
$self = $this->set($key, $value);
}
return $self;
});
2023-12-16 01:13:49 +01:00
}
2020-04-10 20:32:12 +02:00
}