Add Factory for RequestTest
This commit is contained in:
parent
b223dbf190
commit
07fbba1d6a
|
@ -2,23 +2,29 @@
|
||||||
|
|
||||||
namespace Tests\Feature\Initialize;
|
namespace Tests\Feature\Initialize;
|
||||||
|
|
||||||
use App\Console\Commands\NamiInitializeCommand;
|
|
||||||
use App\Initialize\Actions\InitializeAction;
|
use App\Initialize\Actions\InitializeAction;
|
||||||
use App\Initialize\InitializeJob;
|
use App\Initialize\InitializeJob;
|
||||||
use App\Initialize\Initializer;
|
|
||||||
use App\Setting\NamiSettings;
|
use App\Setting\NamiSettings;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
|
||||||
use Illuminate\Support\Facades\Queue;
|
use Illuminate\Support\Facades\Queue;
|
||||||
use Phake;
|
use Tests\RequestFactories\InitializeRequestFactory;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use Zoomyboy\LaravelNami\Authentication\Auth;
|
use Zoomyboy\LaravelNami\Authentication\Auth;
|
||||||
use Zoomyboy\LaravelNami\Fakes\GroupFake;
|
use Zoomyboy\LaravelNami\Fakes\GroupFake;
|
||||||
|
|
||||||
class RequestTest extends TestCase
|
class InitializeActionTest extends TestCase
|
||||||
{
|
{
|
||||||
use DatabaseTransactions;
|
use DatabaseTransactions;
|
||||||
|
|
||||||
|
public function testItCannotInitializeWhenNotLoggedIn(): void
|
||||||
|
{
|
||||||
|
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
||||||
|
|
||||||
|
$response = $this->post('/initialize', $this->factory()->create());
|
||||||
|
|
||||||
|
$response->assertRedirect('/login');
|
||||||
|
}
|
||||||
|
|
||||||
public function testItSetsSettingsBeforeRunningInitializer(): void
|
public function testItSetsSettingsBeforeRunningInitializer(): void
|
||||||
{
|
{
|
||||||
$this->withoutExceptionHandling()->login();
|
$this->withoutExceptionHandling()->login();
|
||||||
|
@ -26,11 +32,7 @@ class RequestTest extends TestCase
|
||||||
Auth::success(12345, 'secret');
|
Auth::success(12345, 'secret');
|
||||||
app(GroupFake::class)->fetches(null, [185 => ['name' => 'testgroup']]);
|
app(GroupFake::class)->fetches(null, [185 => ['name' => 'testgroup']]);
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
$response = $this->post('/initialize', $this->factory()->withCredentials(12345, 'secret')->withGroup(185)->create());
|
||||||
'group_id' => 185,
|
|
||||||
'password' => 'secret',
|
|
||||||
'mglnr' => 12345,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response->assertRedirect('/');
|
$response->assertRedirect('/');
|
||||||
$settings = app(NamiSettings::class);
|
$settings = app(NamiSettings::class);
|
||||||
|
@ -42,13 +44,8 @@ class RequestTest extends TestCase
|
||||||
public function testItValidatesSetupInfo(): void
|
public function testItValidatesSetupInfo(): void
|
||||||
{
|
{
|
||||||
$this->login();
|
$this->login();
|
||||||
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
$response = $this->post('/initialize', $this->factory()->invalid()->create());
|
||||||
'group_id' => null,
|
|
||||||
'password' => null,
|
|
||||||
'mglnr' => null,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertErrors(['password' => 'Passwort ist erforderlich.'], $response);
|
$this->assertErrors(['password' => 'Passwort ist erforderlich.'], $response);
|
||||||
$this->assertErrors(['mglnr' => 'Mitgliedsnummer ist erforderlich.'], $response);
|
$this->assertErrors(['mglnr' => 'Mitgliedsnummer ist erforderlich.'], $response);
|
||||||
|
@ -59,13 +56,8 @@ class RequestTest extends TestCase
|
||||||
{
|
{
|
||||||
$this->login();
|
$this->login();
|
||||||
Auth::fails(12345, 'secret');
|
Auth::fails(12345, 'secret');
|
||||||
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
$response = $this->post('/initialize', $this->factory()->withCredentials(12345, 'secret')->create());
|
||||||
'group_id' => 12345,
|
|
||||||
'password' => 'secret',
|
|
||||||
'mglnr' => 100102,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
|
$this->assertErrors(['nami' => 'NaMi Login fehlgeschlagen.'], $response);
|
||||||
}
|
}
|
||||||
|
@ -73,15 +65,10 @@ class RequestTest extends TestCase
|
||||||
public function testItValidatesGroupExistance(): void
|
public function testItValidatesGroupExistance(): void
|
||||||
{
|
{
|
||||||
$this->login();
|
$this->login();
|
||||||
InitializeAction::partialMock()->shouldReceive('handle')->never();
|
|
||||||
Auth::success(12345, 'secret');
|
Auth::success(12345, 'secret');
|
||||||
app(GroupFake::class)->fetches(null, []);
|
app(GroupFake::class)->fetches(null, []);
|
||||||
|
|
||||||
$response = $this->post('/initialize', [
|
$response = $this->post('/initialize', $this->factory()->withCredentials(12345, 'secret')->withGroup(185)->create());
|
||||||
'group_id' => 185,
|
|
||||||
'password' => 'secret',
|
|
||||||
'mglnr' => 12345,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertErrors(['nami' => 'Gruppierung nicht gefunden.'], $response);
|
$this->assertErrors(['nami' => 'Gruppierung nicht gefunden.'], $response);
|
||||||
}
|
}
|
||||||
|
@ -96,12 +83,8 @@ class RequestTest extends TestCase
|
||||||
Queue::assertPushed(InitializeJob::class);
|
Queue::assertPushed(InitializeJob::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItInitializesFromCommandLine(): void
|
private function factory(): InitializeRequestFactory
|
||||||
{
|
{
|
||||||
$this->stubIo(Initializer::class, fn ($mock) => $mock);
|
return InitializeRequestFactory::new();
|
||||||
|
|
||||||
Artisan::call(NamiInitializeCommand::class);
|
|
||||||
|
|
||||||
Phake::verify(app(Initializer::class))->run();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\RequestFactories;
|
||||||
|
|
||||||
|
use Worksome\RequestFactories\RequestFactory;
|
||||||
|
|
||||||
|
class InitializeRequestFactory extends RequestFactory
|
||||||
|
{
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'group_id' => $this->faker->numberBetween(100, 200),
|
||||||
|
'password' => $this->faker->word(),
|
||||||
|
'mglnr' => $this->faker->numberBetween(100, 200),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function invalid(): self
|
||||||
|
{
|
||||||
|
return $this->state([
|
||||||
|
'group_id' => null,
|
||||||
|
'password' => null,
|
||||||
|
'mglnr' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withCredentials(int $mglnr, string $password): self
|
||||||
|
{
|
||||||
|
return $this->state([
|
||||||
|
'mglnr' => $mglnr,
|
||||||
|
'password' => $password,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withGroup(int $group): self
|
||||||
|
{
|
||||||
|
return $this->state([
|
||||||
|
'group_id' => $group,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue