108 lines
3.7 KiB
PHP
108 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use App\AccountClass;
|
|
use App\AccountType;
|
|
use App\Booking;
|
|
use App\IOType;
|
|
use App\Person;
|
|
use App\PersonTarget;
|
|
use App\Project;
|
|
use App\Scope;
|
|
use App\SubBooking;
|
|
use App\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
use Symfony\Component\Console\Output\BufferedOutput;
|
|
|
|
abstract class DatabaseTestCase extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected $incomeAccount;
|
|
protected $expenseAccount;
|
|
protected $project;
|
|
protected $user;
|
|
|
|
public function artisan($command, $parameters = [])
|
|
{
|
|
// TODO(compat): Laravel 5.6's Kernel::call uses a numeric command key, which the
|
|
// locked Symfony Console reads as a string offset on PHP 7.4.
|
|
// After upgrading Laravel/Symfony, remove this entire override and its
|
|
// Kernel, ArrayInput and BufferedOutput imports once `just test` passes
|
|
// using Laravel's inherited artisan() helper.
|
|
$output = new BufferedOutput;
|
|
$status = $this->app[Kernel::class]->handle(
|
|
new ArrayInput(array_merge(['command' => $command], $parameters)), $output
|
|
);
|
|
$this->assertSame(0, $status, $output->fetch());
|
|
|
|
return $status;
|
|
}
|
|
|
|
protected function setUp()
|
|
{
|
|
parent::setUp();
|
|
|
|
// Small, explicit fixtures; no dependency on demo data or fixed IDs.
|
|
$scope = $this->record(Scope::class, ['name' => 'Test scope']);
|
|
foreach ([1 => 'Einnahme', -1 => 'Ausgabe'] as $factor => $name) {
|
|
$io = $this->record(IOType::class, compact('name', 'factor'));
|
|
$class = $this->record(AccountClass::class, [
|
|
'name' => $name, 'number' => $factor > 0 ? 10 : 20,
|
|
'scope_id' => $scope->id, 'io_type_id' => $io->id,
|
|
]);
|
|
$account = $this->record(AccountType::class, [
|
|
'name' => $name, 'number' => $factor > 0 ? 100 : 200,
|
|
'account_class_id' => $class->id,
|
|
]);
|
|
if ($factor > 0) {
|
|
$this->incomeAccount = $account;
|
|
} else {
|
|
$this->expenseAccount = $account;
|
|
}
|
|
}
|
|
|
|
$this->project = $this->record(Project::class, [
|
|
'name' => 'Test project', 'start_date' => '2019-01-01', 'end_date' => '2020-12-31',
|
|
]);
|
|
$this->user = $this->record(User::class, [
|
|
'name' => 'Test user', 'email' => 'test@example.com', 'password' => bcrypt('test-password'),
|
|
]);
|
|
}
|
|
|
|
protected function record($class, array $attributes)
|
|
{
|
|
$model = new $class;
|
|
$model->forceFill($attributes)->save();
|
|
|
|
return $model;
|
|
}
|
|
|
|
protected function booking($date, $amount, $account = null, array $attributes = [])
|
|
{
|
|
$booking = $this->record(Booking::class, [
|
|
'date' => $date, 'reference' => 'Test booking', 'person_or_company' => 'Test company',
|
|
]);
|
|
$this->record(SubBooking::class, array_merge([
|
|
'booking_id' => $booking->id, 'reference' => 'Test line', 'amount' => $amount,
|
|
'account_type_id' => ($account ?: $this->incomeAccount)->id,
|
|
'project_id' => $this->project->id,
|
|
], $attributes));
|
|
|
|
return $booking;
|
|
}
|
|
|
|
protected function target($amount = 100, $account = null)
|
|
{
|
|
$person = $this->record(Person::class, ['title' => 'Firma', 'name_or_company' => 'Test company']);
|
|
|
|
return $this->record(PersonTarget::class, [
|
|
'person_id' => $person->id, 'date' => '2020-01-01', 'reference' => 'Test target',
|
|
'amount' => $amount, 'io_type_id' => ($account ?: $this->incomeAccount)->accountClass->io_type_id,
|
|
]);
|
|
}
|
|
}
|