diff --git a/app/Database/factories/ExpenseFactory.php b/app/Database/factories/ExpenseFactory.php new file mode 100644 index 00000000..99792729 --- /dev/null +++ b/app/Database/factories/ExpenseFactory.php @@ -0,0 +1,18 @@ +define(App\Models\Expense::class, function (Faker $faker) { + return [ + 'id' => null, + 'airline_id' => function () { + return factory(App\Models\Airline::class)->create()->id; + }, + 'name' => $faker->text(20), + 'amount' => $faker->randomFloat(2, 100, 1000), + 'type' => ExpenseType::FLIGHT, + 'multiplier' => false, + 'active' => true, + ]; +}); diff --git a/app/Repositories/ExpenseRepository.php b/app/Repositories/ExpenseRepository.php index f6ca28dc..960c0f32 100644 --- a/app/Repositories/ExpenseRepository.php +++ b/app/Repositories/ExpenseRepository.php @@ -30,6 +30,7 @@ class ExpenseRepository extends BaseRepository implements CacheableInterface { $expenses = $this->findWhere([ 'type' => $type, + ['airline_id', '=', null] ]); if($airline_id) { diff --git a/tests/FinanceTest.php b/tests/FinanceTest.php index e48770fc..79465732 100644 --- a/tests/FinanceTest.php +++ b/tests/FinanceTest.php @@ -1,5 +1,7 @@ addData('base'); + $this->expenseRepo = app(ExpenseRepository::class); $this->fareSvc = app(FareService::class); $this->financeSvc = app(FinanceService::class); $this->fleetSvc = app(FleetService::class); @@ -546,6 +550,41 @@ class FinanceTest extends TestCase } } + /** + * Test that all expenses are pulled properly + */ + public function testPirepExpenses() + { + $airline = factory(App\Models\Airline::class)->create(); + $airline2 = factory(App\Models\Airline::class)->create(); + + factory(App\Models\Expense::class)->create([ + 'airline_id' => $airline->id + ]); + + factory(App\Models\Expense::class)->create([ + 'airline_id' => $airline2->id + ]); + + factory(App\Models\Expense::class)->create([ + 'airline_id' => null + ]); + + $expenses = $this->expenseRepo + ->getAllForType(ExpenseType::FLIGHT, $airline->id); + + $this->assertCount(2, $expenses); + + $found = $expenses->where('airline_id', null); + $this->assertCount(1, $found); + + $found = $expenses->where('airline_id', $airline->id); + $this->assertCount(1, $found); + + $found = $expenses->where('airline_id', $airline2->id); + $this->assertCount(0, $found); + } + /** * * @throws Exception