From b28a4f310884ca3790a0c2c1d2bc28c12a2e8cb8 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Thu, 30 Apr 2020 09:50:12 -0400 Subject: [PATCH] Add component unit test and unify some validation. --- app/Models/Component.php | 13 ++--- tests/unit/ComponentTest.php | 100 +++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 tests/unit/ComponentTest.php diff --git a/app/Models/Component.php b/app/Models/Component.php index f24f3250e7..3e027cc4ee 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -20,17 +20,18 @@ class Component extends SnipeModel protected $dates = ['deleted_at', 'purchase_date']; protected $table = 'components'; - + /** * Category validation rules */ public $rules = array( - 'name' => 'required|min:3|max:255', - 'qty' => 'required|integer|min:1', - 'category_id' => 'required|integer', - 'company_id' => 'integer|nullable', + 'name' => 'required|min:3|max:255', + 'qty' => 'required|integer|min:1', + 'category_id' => 'required|integer|exists:categories,id', + 'company_id' => 'integer|nullable', + 'min_amt' => 'integer|min:0|nullable', 'purchase_date' => 'date|nullable', - 'purchase_cost' => 'numeric|nullable', + 'purchase_cost' => 'numeric|nullable', ); /** diff --git a/tests/unit/ComponentTest.php b/tests/unit/ComponentTest.php new file mode 100644 index 0000000000..1e167955a6 --- /dev/null +++ b/tests/unit/ComponentTest.php @@ -0,0 +1,100 @@ +assertFalse($a->isValid()); + $fields = [ + 'name' => 'name', + 'qty' => 'qty', + 'category_id' => 'category id' + ]; + $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 + $a = factory(Component::class)->make([ + 'name' => 'a', + 'qty' => 0, + 'min_amt' => -1 + ]); + $fields = [ + 'name' => 'name', + 'qty' => 'qty', + 'min_amt' => 'min amt' + ]; + $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() + { + $category = $this->createValidCategory('component-hdd-category', + ['category_type' => 'component']); + $component = factory(Component::class) + ->states('ssd-crucial240') + ->make(['category_id' => $category->id]); + $this->createValidManufacturer('apple'); + + $component->save(); + $this->assertTrue($component->isValid()); + $newId = $category->id + 1; + $component = factory(Component::class)->states('ssd-crucial240')->make(['category_id' => $newId]); + $component->save(); + + $this->assertFalse($component->isValid()); + $this->assertStringContainsString("The selected category id is invalid.", $component->getErrors()->get('category_id')[0]); + } + + public function testAnComponentBelongsToACompany() + { + $component = factory(Component::class) + ->create(['company_id' => factory(Company::class)->create()->id]); + $this->assertInstanceOf(Company::class, $component->company); + } + + public function testAnComponentHasALocation() + { + $component = factory(Component::class) + ->create(['location_id' => factory(Location::class)->create()->id]); + $this->assertInstanceOf(Location::class, $component->location); + } + + public function testAnComponentBelongsToACategory() + { + $component = factory(Component::class)->states('ssd-crucial240') + ->create([ + 'category_id' => factory(Category::class) + ->states('component-hdd-category') + ->create(['category_type' => 'component'])->id + ]); + $this->assertInstanceOf(Category::class, $component->category); + $this->assertEquals('component', $component->category->category_type); + } +}