adrema/tests/Feature/Mailgateway/StoreTest.php

92 lines
3.5 KiB
PHP
Raw Normal View History

2023-06-01 15:45:02 +02:00
<?php
namespace Tests\Feature\Mailgateway;
use App\Mailgateway\Types\LocalType;
2023-06-07 22:52:02 +02:00
use App\Mailgateway\Types\MailmanType;
2023-06-01 15:45:02 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-06-07 22:52:02 +02:00
use Phake;
2023-06-01 15:45:02 +02:00
use Tests\RequestFactories\MailgatewayRequestFactory;
2023-06-07 22:52:02 +02:00
use Tests\RequestFactories\MailmanTypeRequest;
2023-06-01 15:45:02 +02:00
use Tests\TestCase;
class StoreTest extends TestCase
{
use DatabaseTransactions;
public function setUp(): void
{
parent::setUp();
$this->login()->loginNami();
}
public function testItCanStoreALocalGateway(): void
{
2023-06-07 22:52:02 +02:00
$response = $this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->name('lala')->type(LocalType::class, [])->domain('example.com')->create());
2023-06-01 15:45:02 +02:00
$response->assertOk();
$this->assertDatabaseHas('mailgateways', [
'domain' => 'example.com',
'name' => 'lala',
2023-06-07 21:43:11 +02:00
'type' => json_encode([
'cls' => LocalType::class,
'params' => [],
]),
2023-06-01 15:45:02 +02:00
]);
}
2023-06-07 22:52:02 +02:00
public function testItCanStoreAMailmanGateway(): void
{
$typeParams = ['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret'];
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
Phake::when($mock)->works()->thenReturn(true);
});
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create());
$this->assertDatabaseHas('mailgateways', [
'type' => json_encode([
'cls' => MailmanType::class,
'params' => $typeParams,
]),
]);
}
public function testItThrowsErrorWhenMailmanConnectionFailed(): void
{
$typeParams = ['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret'];
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
Phake::when($mock)->works()->thenReturn(false);
});
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create())
->assertJsonValidationErrors('connection');
}
public function testItValidatesCustomFields(): void
{
$typeParams = ['url' => 'https://example.com', 'user' => '', 'password' => 'secret'];
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
Phake::when($mock)->works()->thenReturn(false);
});
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create())
->assertJsonValidationErrors(['type.params.user' => 'Benutzer ist erforderlich.']);
}
public function testItValidatesType(): void
{
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->missingType()->create())
->assertJsonValidationErrors('type.cls');
}
public function testItValidatesNameAndDomain(): void
{
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->withoutName()->withoutDomain()->create())
->assertJsonValidationErrors('domain')
->assertJsonValidationErrors('name');
}
2023-06-01 15:45:02 +02:00
}