2020-04-30 06:50:12 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-04-30 06:50:12 -07:00
|
|
|
use App\Models\Category;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Component;
|
|
|
|
use App\Models\Location;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
|
|
class ComponentTest extends BaseTest
|
|
|
|
{
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* @var \UnitTester
|
|
|
|
*/
|
2020-04-30 06:50:12 -07:00
|
|
|
protected $tester;
|
|
|
|
|
|
|
|
public function testFailsEmptyValidation()
|
|
|
|
{
|
|
|
|
// An Component requires a name, a qty, and a category_id.
|
|
|
|
$a = Component::create();
|
|
|
|
$this->assertFalse($a->isValid());
|
|
|
|
$fields = [
|
|
|
|
'name' => 'name',
|
|
|
|
'qty' => 'qty',
|
2021-06-10 13:15:52 -07:00
|
|
|
'category_id' => 'category id',
|
2020-04-30 06:50:12 -07:00
|
|
|
];
|
|
|
|
$errors = $a->getErrors();
|
|
|
|
foreach ($fields as $field => $fieldTitle) {
|
|
|
|
$this->assertEquals($errors->get($field)[0], "The ${fieldTitle} field is required.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsMinValidation()
|
|
|
|
{
|
|
|
|
// An Component name has a min length of 3
|
|
|
|
// An Component has a min qty of 1
|
|
|
|
// An Component has a min amount of 0
|
2021-06-10 13:17:44 -07:00
|
|
|
$a = Component::factory()->make([
|
2020-04-30 06:50:12 -07:00
|
|
|
'name' => 'a',
|
|
|
|
'qty' => 0,
|
2021-06-10 13:15:52 -07:00
|
|
|
'min_amt' => -1,
|
2020-04-30 06:50:12 -07:00
|
|
|
]);
|
|
|
|
$fields = [
|
|
|
|
'name' => 'name',
|
|
|
|
'qty' => 'qty',
|
2021-06-10 13:15:52 -07:00
|
|
|
'min_amt' => 'min amt',
|
2020-04-30 06:50:12 -07:00
|
|
|
];
|
|
|
|
$this->assertFalse($a->isValid());
|
|
|
|
$errors = $a->getErrors();
|
|
|
|
foreach ($fields as $field => $fieldTitle) {
|
|
|
|
$this->assertStringContainsString("The ${fieldTitle} must be at least", $errors->get($field)[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCategoryIdMustExist()
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$category = $this->createValidCategory('component-hdd-category',
|
2020-04-30 06:50:12 -07:00
|
|
|
['category_type' => 'component']);
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()->ssdCrucial240()
|
2020-04-30 06:50:12 -07:00
|
|
|
->make(['category_id' => $category->id]);
|
|
|
|
$this->createValidManufacturer('apple');
|
|
|
|
|
|
|
|
$component->save();
|
|
|
|
$this->assertTrue($component->isValid());
|
|
|
|
$newId = $category->id + 1;
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()->ssdCrucial240()->make(['category_id' => $newId]);
|
2020-04-30 06:50:12 -07:00
|
|
|
$component->save();
|
|
|
|
|
|
|
|
$this->assertFalse($component->isValid());
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->assertStringContainsString('The selected category id is invalid.', $component->getErrors()->get('category_id')[0]);
|
2020-04-30 06:50:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAnComponentBelongsToACompany()
|
|
|
|
{
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()
|
|
|
|
->create(['company_id' => Company::factory()->create()->id]);
|
2020-04-30 06:50:12 -07:00
|
|
|
$this->assertInstanceOf(Company::class, $component->company);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAnComponentHasALocation()
|
|
|
|
{
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()
|
|
|
|
->create(['location_id' => Location::factory()->create()->id]);
|
2020-04-30 06:50:12 -07:00
|
|
|
$this->assertInstanceOf(Location::class, $component->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAnComponentBelongsToACategory()
|
|
|
|
{
|
2021-06-10 13:17:44 -07:00
|
|
|
$component = Component::factory()->ssdCrucial240()
|
2020-04-30 06:50:12 -07:00
|
|
|
->create([
|
2021-06-10 13:17:44 -07:00
|
|
|
'category_id' => Category::factory()->componentHddCategory()
|
2021-06-10 13:15:52 -07:00
|
|
|
->create(['category_type' => 'component'])->id,
|
2020-04-30 06:50:12 -07:00
|
|
|
]);
|
|
|
|
$this->assertInstanceOf(Category::class, $component->category);
|
|
|
|
$this->assertEquals('component', $component->category->category_type);
|
|
|
|
}
|
|
|
|
}
|