adrema/tests/TestCase.php

107 lines
2.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;
2021-07-17 15:58:38 +02:00
use Illuminate\Testing\TestResponse;
2022-10-18 15:04:47 +02:00
use Phake;
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;
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);
2022-08-30 23:39:02 +02:00
$this->withNamiSettings($mglnr, $password);
Group::factory()->create(['nami_id' => 55]);
return $this;
}
public function withNamiSettings(int $mglnr = 12345, string $password = 'password'): self
{
2022-02-19 15:18:24 +01:00
NamiSettings::fake([
'mglnr' => $mglnr,
'password' => $password,
2022-08-12 22:07:59 +02:00
'default_group_id' => 55,
2022-02-19 15:18:24 +01:00
]);
return $this;
}
public function failedNami(int $mglnr = 12345, string $password = 'password'): self
2022-01-03 01:17:24 +01:00
{
2022-09-01 23:56:01 +02:00
Auth::fails($mglnr, $password);
2022-02-19 15:18:24 +01:00
NamiSettings::fake([
'mglnr' => $mglnr,
'password' => $password,
2022-08-30 23:39:02 +02:00
'default_group_id' => 55,
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-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) {
$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;
}
2022-09-06 17:51:18 +02:00
/**
* @param <class-string> $class
*/
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;
}
2020-04-10 20:32:12 +02:00
}