SmartCane/laravel_app/tests/Unit/Models/ProjectTest.php
Martin Folkerts eb1def36a1 wip
2023-12-22 16:55:40 +01:00

148 lines
5 KiB
PHP

<?php
namespace Tests\Unit\Models;
use App\Models\Project;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Mockery;
use Tests\TestCase;
class ProjectTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
}
/** @test */
public function it_should_return_the_correct_attachment_path()
{
$project = Project::factory()->create(['name' => 'Test Project']);
$this->assertStringEndsWith(
'/storage/test_project/attachments',
$project->attachment_path
);
}
/** @test */
public function it_can_add_a_mailing_and_attachment()
{
$project = Project::factory()->create();
$project->mailings()->create([
'subject' => 'Test Subject',
'message' => 'Test Message',
]);
$fileFake = \Illuminate\Http\UploadedFile::fake()->create('test.pdf');
$project->mailings()->first()->addAttachment('test.pdf', $fileFake);
$this->assertCount(1, $project->mailings);
$this->assertCount(1, $project->mailings()->first()->attachments);
}
/** @test */
public function when_running_the_seeder_their_are_three_projects(): void
{
$this->seed();
$this->assertCount(3, Project::all());
}
/** @test */
public function when_running_the_seeder_their_are_three_projects_with_the_correct_names(): void
{
$this->seed();
$this->assertEquals('Chemba', Project::find(1)->name);
$this->assertEquals('Xinavane', Project::find(2)->name);
$this->assertEquals('Kakira', Project::find(3)->name);
}
/** @test */
public function when_running_the_seeder_their_are_three_projects_with_the_correct_download_paths(): void
{
$this->seed();
$this->assertEquals('chemba', Project::find(1)->download_path);
$this->assertEquals('xinavane', Project::find(2)->download_path);
$this->assertEquals('kakira', Project::find(3)->download_path);
}
/** @test */
public function when_not_all_mosaics_are_present_it_should_return_an_exception()
{
$this->seed();
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Missing mosaics: week_52_2022.tif, week_51_2021.tif, week_50_2021.tif, week_49_2021.tif');
$project = Project::find(1);
$lastDate = Carbon::parse('2022-01-01');
//$lastDate->getWeekOfYear();
$project->allMosaicsPresent($lastDate);
}
/** @test */
public function when_all_mosaics_are_present_it_should_return_true()
{
$project = Mockery::mock(Project::class)->makePartial();
$lastDate = Carbon::parse('2021-01-01');
$project->shouldReceive('getMosaicList')->andReturn(
collect([
"chemba/weekly_mosaic/week_53_2020.tif",
"chemba/weekly_mosaic/week_52_2020.tif",
"chemba/weekly_mosaic/week_51_2020.tif",
"chemba/weekly_mosaic/week_50_2020.tif",
]));
$this->assertTrue($project->allMosaicsPresent($lastDate));
}
/** @test */
public function when_not_mosaics_are_present_it_should_throw_an_exception_listing_the_missing_mosiacs()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Missing mosaics: week_53_2020.tif');
$project = Mockery::mock(Project::class)->makePartial();
$lastDate = Carbon::parse('2021-01-01');
$project->shouldReceive('getMosaicList')->andReturn(
collect([
"chemba/weekly_mosaic/week_52_2020.tif",
"chemba/weekly_mosaic/week_51_2020.tif",
"chemba/weekly_mosaic/week_50_2020.tif",
"chemba/weekly_mosaic/week_49_2021.tif",
"chemba/weekly_mosaic/week_48_2021.tif",
]));
$project->allMosaicsPresent($lastDate);
}
/** @test */
public function when_all_mosaics_are_present_is_called_with_future_date_it_should_throw_an_expection()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('End date is in the future');
$this->seed();
$project = Project::find(1);
Carbon::setTestNow(Carbon::parse('2020-01-01'));
$lastDate = Carbon::parse('2021-01-01');
// stub getMosiacList() to return a list containing four filenames;
($project->allMosaicsPresent($lastDate));
}
/** @test */
public function getMosiacFileListByEndDate_should_return_four_filenames()
{
$this->seed();
$project = Project::find(1);
$lastDate = Carbon::parse('2021-01-01');
$list = $project->getMosiacFilenameListByEndDate($lastDate);
$this->assertCount(4, $list);
$this->assertEquals([
"week_53_2020.tif",
"week_52_2020.tif",
"week_51_2020.tif",
"week_50_2020.tif",
], $list->toArray());
}
}