some vibe tests

better than none
This commit is contained in:
2026-10-30 10:20:59 +01:00
parent 613154a1f9
commit d4eaa45baa
8 changed files with 596 additions and 0 deletions
+105
View File
@@ -0,0 +1,105 @@
<?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 = [])
{
// 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.
// Use its normal input handler with a named command until upgraded.
$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,
]);
}
}