adrema/tests/TestCase.php

83 lines
2.1 KiB
PHP
Raw Normal View History

2020-04-10 20:32:12 +02:00
<?php
namespace Tests;
use App\Member\Member;
2021-11-23 01:02:18 +01:00
use App\Setting\GeneralSettings;
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;
2021-07-17 15:58:38 +02:00
use Illuminate\Testing\TestResponse;
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;
2021-11-17 22:44:07 +01:00
use Zoomyboy\LaravelNami\Backend\FakeBackend;
use Zoomyboy\LaravelNami\Nami;
2020-04-10 20:32:12 +02:00
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
2022-02-12 00:41:52 +01:00
use TestsInertia;
2021-06-13 11:30:31 +02:00
2022-02-19 15:18:24 +01:00
protected User $me;
public function setUp(): void
2022-02-12 15:33:16 +01:00
{
2022-02-19 15:18:24 +01:00
parent::setUp();
Auth::fake();
2022-01-03 01:17:24 +01:00
}
2022-02-19 15:18:24 +01:00
public function loginNami(int $mglnr = 12345, string $password = 'password'): self
{
Auth::success($mglnr, $password);
NamiSettings::fake([
'mglnr' => $mglnr,
'password' => $password,
]);
return $this;
}
public function failedNami(int $mglnr = 12345, string $password = 'password'): self
2022-01-03 01:17:24 +01:00
{
2022-02-19 15:18:24 +01:00
Auth::failed($mglnr, $password);
NamiSettings::fake([
'mglnr' => $mglnr,
'password' => $password,
2021-06-18 23:36:06 +02:00
]);
return $this;
}
2022-02-19 15:18:24 +01:00
public function login(): self
{
$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-02-19 15:18:24 +01:00
public function assertErrors(array $errors, TestResponse $response) {
$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) {
$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));
}
return $this;
}
2021-06-18 23:36:06 +02:00
2020-04-10 20:32:12 +02:00
}