Enabled foreign keys in sqlite tests, added missing fields to factories

This commit is contained in:
Igor Scheller 2023-10-24 23:00:04 +02:00 committed by msquare
parent 0a0cf5265c
commit 39dbfabea7
14 changed files with 27 additions and 10 deletions

View File

@ -24,6 +24,7 @@ class AngelTypeFactory extends Factory
'restricted' => $this->faker->boolean(),
'requires_driver_license' => $this->faker->boolean(),
'requires_ifsg_certificate' => $this->faker->boolean(),
'shift_self_signup' => $this->faker->boolean(),
'show_on_dashboard' => $this->faker->boolean(),
'hide_register' => $this->faker->boolean(),

View File

@ -18,6 +18,7 @@ class LocationFactory extends Factory
'name' => $this->faker->unique()->firstName(),
'map_url' => $this->faker->url(),
'description' => $this->faker->text(),
'dect' => $this->faker->optional()->numberBetween(1000, 9999),
];
}
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Database\Factories\Engelsystem\Models;
use Engelsystem\Models\OAuth;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class OAuthFactory extends Factory
@ -18,11 +19,12 @@ class OAuthFactory extends Factory
public function definition(): array
{
return [
'user_id' => User::factory(),
'provider' => $this->faker->unique()->word(),
'identifier' => $this->faker->unique()->word(),
'access_token' => $this->faker->unique()->word(),
'refresh_token' => $this->faker->unique()->word(),
'expires_at' => '2099-12-31',
'expires_at' => $this->faker->dateTimeInInterval('+5 days', '+3 months')->format('Y-m-d'),
];
}
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\Contact;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class ContactFactory extends Factory
@ -15,6 +16,7 @@ class ContactFactory extends Factory
public function definition(): array
{
return [
'user_id' => User::factory(),
'dect' => $this->faker->optional()->numberBetween(1000, 9999),
'email' => $this->faker->unique()->optional()->safeEmail(),
'mobile' => $this->faker->optional(.2)->phoneNumber(),

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\License;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class LicenseFactory extends Factory
@ -25,6 +26,7 @@ class LicenseFactory extends Factory
$ifsg_certificate_light = $this->faker->boolean(0.5) && !$ifsg_certificate;
return [
'user_id' => User::factory(),
'has_car' => $drive_car && $this->faker->boolean(.7),
'drive_forklift' => $drive_forklift,
'drive_car' => $drive_car,

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class PasswordResetFactory extends Factory
@ -15,6 +16,7 @@ class PasswordResetFactory extends Factory
public function definition(): array
{
return [
'user_id' => User::factory(),
'token' => bin2hex(random_bytes(16)),
];
}

View File

@ -6,6 +6,7 @@ namespace Database\Factories\Engelsystem\Models\User;
use Carbon\Carbon;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class PersonalDataFactory extends Factory
@ -19,6 +20,7 @@ class PersonalDataFactory extends Factory
$departure = $this->faker->optional()->dateTimeThisMonth('2 weeks');
return [
'user_id' => User::factory(),
'first_name' => $this->faker->optional(.7)->firstName(),
'last_name' => $this->faker->optional()->lastName(),
'pronoun' => $this->faker->optional(.3)->pronoun(),

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class SettingsFactory extends Factory
@ -15,6 +16,7 @@ class SettingsFactory extends Factory
public function definition(): array
{
return [
'user_id' => User::factory(),
'language' => $this->faker->locale(),
'theme' => $this->faker->numberBetween(1, 20),
'email_human' => $this->faker->boolean(),

View File

@ -6,6 +6,7 @@ namespace Database\Factories\Engelsystem\Models\User;
use Carbon\Carbon;
use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class StateFactory extends Factory
@ -18,6 +19,7 @@ class StateFactory extends Factory
$arrival = $this->faker->optional()->dateTimeThisMonth();
return [
'user_id' => User::factory(),
'arrived' => (bool) $arrival,
'arrival_date' => $arrival ? Carbon::instance($arrival) : null,
'active' => $this->faker->boolean(.3),

View File

@ -19,6 +19,7 @@ class UserFactory extends Factory
'password' => crypt(random_bytes(16), '$1$salt$'),
'email' => $this->faker->unique()->safeEmail(),
'api_key' => bin2hex(random_bytes(32)),
'updated_at' => $this->faker->dateTimeInInterval('-3 months', 'now'),
];
}
}

View File

@ -279,10 +279,11 @@ class NewsControllerTest extends ControllerTest
->willReturnSelf();
$this->app->instance('events.dispatcher', $eventDispatcher);
$user = User::factory()->create();
(new News([
'title' => 'Foo',
'text' => '**foo**',
'user_id' => 1,
'user_id' => $user->id,
]))->save();
}
}

View File

@ -249,9 +249,6 @@ class NewsControllerTest extends ControllerTest
{
$this->request = $this->request->withAttribute('comment_id', 2)->withParsedBody(['delete' => '1']);
$this->addUser(1);
$this->addUser(2);
/** @var NewsController $controller */
$controller = $this->app->get(NewsController::class);
$controller->setValidator($this->app->get(Validator::class));
@ -268,8 +265,6 @@ class NewsControllerTest extends ControllerTest
$this->request = $this->request->withAttribute('comment_id', 1)->withParsedBody(['delete' => '1']);
$this->setExpects($this->response, 'redirectTo', ['http://localhost/news/1'], $this->response);
$this->addUser(1);
/** @var NewsController $controller */
$controller = $this->app->get(NewsController::class);
$controller->setValidator($this->app->get(Validator::class));
@ -293,6 +288,9 @@ class NewsControllerTest extends ControllerTest
$this->auth = $this->createMock(Authenticator::class);
$this->app->instance(Authenticator::class, $this->auth);
$this->addUser(1);
$this->addUser(2);
foreach ($this->data as $news) {
(new News($news))->save();
}

View File

@ -23,7 +23,7 @@ trait HasDatabase
protected function initDatabase(): void
{
$dbManager = new CapsuleManager();
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'foreign_key_constraints' => true]);
$dbManager->bootEloquent();
$connection = $dbManager->getConnection();

View File

@ -336,8 +336,9 @@ class UserTest extends ModelTest
*/
public function testNewsComments(): void
{
News::factory()->create();
($user = new User($this->data))->save();
$newsComment = NewsComment::create(['news_id' => 0, 'text' => 'test comment', 'user_id' => $user->id]);
$newsComment = NewsComment::create(['news_id' => 1, 'text' => 'test comment', 'user_id' => $user->id]);
$comments = $user->newsComments;
$this->assertCount(1, $comments);