From 5cba7de890f5135467731b8dfc8ed34aec408e42 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sun, 29 Jun 2025 22:25:38 -0700 Subject: [PATCH] test additions variants table --- ...29054627_CreateProductCategoryVariants.php | 9 +- src/Model/Entity/ProductCategoryVariant.php | 4 +- .../Enum/ProductCategoryVariantTypeId.php | 21 ++ .../Table/ProductCategoryVariantsTable.php | 11 +- templates/ProductCategoryVariants/add.php | 8 +- templates/ProductCategoryVariants/edit.php | 8 +- .../element/ProductCategoryVariants/form.php | 11 + .../ProductCategoryVariantsFixture.php | 40 +++ .../ProductCategoryVariantsControllerTest.php | 325 ++++++++++++++++++ 9 files changed, 414 insertions(+), 23 deletions(-) create mode 100644 src/Model/Enum/ProductCategoryVariantTypeId.php create mode 100644 templates/element/ProductCategoryVariants/form.php create mode 100644 tests/Fixture/ProductCategoryVariantsFixture.php create mode 100644 tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php diff --git a/config/Migrations/20250629054627_CreateProductCategoryVariants.php b/config/Migrations/20250629054627_CreateProductCategoryVariants.php index 1ac8e8f..c13eb00 100644 --- a/config/Migrations/20250629054627_CreateProductCategoryVariants.php +++ b/config/Migrations/20250629054627_CreateProductCategoryVariants.php @@ -32,7 +32,7 @@ class CreateProductCategoryVariants extends AbstractMigration 'default' => null, 'null' => true, ]); - $table->addColumn('attribute_type_id', 'integer', [ + $table->addColumn('variant_type_id', 'integer', [ 'default' => null, 'limit' => 11, 'null' => false, @@ -44,20 +44,21 @@ class CreateProductCategoryVariants extends AbstractMigration $table->addIndex([ 'product_category_id', ], [ - 'name' => 'BY_PRODUCT_CATEGORY_ID', + 'name' => 'VARIANTS_BY_PRODUCT_CATEGORY_ID', 'unique' => false, ]); $table->addIndex([ 'product_id', ], [ - 'name' => 'BY_PRODUCT_ID', + 'name' => 'VARIANTS_BY_PRODUCT_ID', 'unique' => false, ]); $table->addIndex([ 'name', 'product_category_id', + 'product_id', ], [ - 'name' => 'BY_NAME_AND_PRODUCT_CATEGORY_ID_UNIQUE', + 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', 'unique' => true, ]); $table->create(); diff --git a/src/Model/Entity/ProductCategoryVariant.php b/src/Model/Entity/ProductCategoryVariant.php index 38fb6c8..3a5987a 100644 --- a/src/Model/Entity/ProductCategoryVariant.php +++ b/src/Model/Entity/ProductCategoryVariant.php @@ -12,7 +12,7 @@ use Cake\ORM\Entity; * @property string $name * @property string|null $product_category_id * @property string|null $product_id - * @property int $attribute_type_id + * @property int $variant_type_id * @property bool $enabled * * @property \App\Model\Entity\ProductCategory $product_category @@ -33,7 +33,7 @@ class ProductCategoryVariant extends Entity 'name' => true, 'product_category_id' => true, 'product_id' => true, - 'attribute_type_id' => true, + 'variant_type_id' => true, 'enabled' => true, 'product_category' => true, 'product' => true, diff --git a/src/Model/Enum/ProductCategoryVariantTypeId.php b/src/Model/Enum/ProductCategoryVariantTypeId.php new file mode 100644 index 0000000..1f28cce --- /dev/null +++ b/src/Model/Enum/ProductCategoryVariantTypeId.php @@ -0,0 +1,21 @@ + 'All Variant Combinations Created Automatically', + self::ManualAdd => 'Variants Manually Managed', + }; + } +} diff --git a/src/Model/Table/ProductCategoryVariantsTable.php b/src/Model/Table/ProductCategoryVariantsTable.php index dd37f0b..1eb44ac 100644 --- a/src/Model/Table/ProductCategoryVariantsTable.php +++ b/src/Model/Table/ProductCategoryVariantsTable.php @@ -3,10 +3,13 @@ declare(strict_types=1); namespace CakeProducts\Model\Table; +use Cake\Database\Type\EnumType; use Cake\ORM\Query\SelectQuery; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; +use CakeProducts\Model\Enum\ProductCategoryAttributeTypeId; +use CakeProducts\Model\Enum\ProductCategoryVariantTypeId; /** * ProductCategoryVariants Model @@ -53,6 +56,8 @@ class ProductCategoryVariantsTable extends Table 'foreignKey' => 'product_id', 'className' => 'CakeProducts.Products', ]); + $this->getSchema()->setColumnType('variant_type_id', EnumType::from(ProductCategoryVariantTypeId::class)); + } /** @@ -78,9 +83,9 @@ class ProductCategoryVariantsTable extends Table ->allowEmptyString('product_id'); $validator - ->integer('attribute_type_id') - ->requirePresence('attribute_type_id', 'create') - ->notEmptyString('attribute_type_id'); + ->integer('variant_type_id') + ->requirePresence('variant_type_id', 'create') + ->notEmptyString('variant_type_id'); $validator ->boolean('enabled') diff --git a/templates/ProductCategoryVariants/add.php b/templates/ProductCategoryVariants/add.php index 1f8ff0c..28a187a 100644 --- a/templates/ProductCategoryVariants/add.php +++ b/templates/ProductCategoryVariants/add.php @@ -18,13 +18,7 @@ Form->create($productCategoryVariant) ?>
- Form->control('name'); - echo $this->Form->control('product_category_id', ['options' => $productCategories, 'empty' => true]); - echo $this->Form->control('product_id', ['options' => $products, 'empty' => true]); - echo $this->Form->control('attribute_type_id'); - echo $this->Form->control('enabled'); - ?> + element('ProductCategoryVariants/form'); ?>
Form->button(__('Submit')) ?> Form->end() ?> diff --git a/templates/ProductCategoryVariants/edit.php b/templates/ProductCategoryVariants/edit.php index 0a9f27b..7ed361b 100644 --- a/templates/ProductCategoryVariants/edit.php +++ b/templates/ProductCategoryVariants/edit.php @@ -23,13 +23,7 @@ Form->create($productCategoryVariant) ?>
- Form->control('name'); - echo $this->Form->control('product_category_id', ['options' => $productCategories, 'empty' => true]); - echo $this->Form->control('product_id', ['options' => $products, 'empty' => true]); - echo $this->Form->control('attribute_type_id'); - echo $this->Form->control('enabled'); - ?> + element('ProductCategoryVariants/form'); ?>
Form->button(__('Submit')) ?> Form->end() ?> diff --git a/templates/element/ProductCategoryVariants/form.php b/templates/element/ProductCategoryVariants/form.php new file mode 100644 index 0000000..3485f4c --- /dev/null +++ b/templates/element/ProductCategoryVariants/form.php @@ -0,0 +1,11 @@ +Form->control('name'); +echo $this->Form->control('product_category_id', ['options' => $productCategories, 'empty' => true]); +echo $this->Form->control('product_id', ['options' => $products, 'empty' => true]); +echo $this->Form->control('attribute_type_id'); +echo $this->Form->control('enabled'); +?> \ No newline at end of file diff --git a/tests/Fixture/ProductCategoryVariantsFixture.php b/tests/Fixture/ProductCategoryVariantsFixture.php new file mode 100644 index 0000000..6b9d34a --- /dev/null +++ b/tests/Fixture/ProductCategoryVariantsFixture.php @@ -0,0 +1,40 @@ +records = [ + [ + 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', + 'name' => 'Color', + 'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', + 'product_id' => null, + 'variant_type_id' => 1, + 'enabled' => 1, + ], + [ + 'id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', + 'name' => 'Color', + 'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', + 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', + 'variant_type_id' => 1, + 'enabled' => 1, + ], + ]; + parent::init(); + } +} diff --git a/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php b/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php new file mode 100644 index 0000000..fbc1e53 --- /dev/null +++ b/tests/TestCase/Controller/ProductCategoryVariantsControllerTest.php @@ -0,0 +1,325 @@ + + */ + protected array $fixtures = [ + 'plugin.CakeProducts.ProductCategoryVariants', + 'plugin.CakeProducts.ProductCategories', + 'plugin.CakeProducts.Products', + ]; + + /** + * setUp method + * + * @return void + */ + protected function setUp(): void + { + parent::setUp(); + $this->ProductCategoryVariants = $this->getTableLocator()->get('ProductCategoryVariants'); + } + + /** + * tearDown method + * + * @return void + */ + protected function tearDown(): void + { + unset($this->ProductCategoryVariants); + + parent::tearDown(); + } + + /** + * Test index method + * + * Tests the index action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::index() + * @throws Exception + * + * @return void + */ + public function testIndexGet(): void + { + $this->loginUserByRole('admin'); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'index', + ]; + $this->get($url); + $this->assertResponseCode(200); + } + + /** + * Test view method + * + * Tests the view action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::view() + * @throws Exception + * + * @return void + */ + public function testViewGet(): void + { + $id = '5a386e9f-6e7a-4ae7-9360-c8e529f78d93'; + $this->loginUserByRole('admin'); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'view', + $id, + ]; + $this->get($url); + $this->assertResponseCode(200); + } + + /** + * Test add method + * + * Tests the add action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::add() + * @throws Exception + * + * @return void + */ + public function testAddGet(): void + { + $cntBefore = $this->ProductCategoryVariants->find()->count(); + + $this->loginUserByRole('admin'); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'add', + ]; + $this->get($url); + $this->assertResponseCode(200); + + $cntAfter = $this->ProductCategoryVariants->find()->count(); + $this->assertEquals($cntBefore, $cntAfter); + } + + /** + * Test add method + * + * Tests a POST request to the add action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::add() + * @throws Exception + * + * @return void + */ + public function testAddPostLoggedInSuccess(): void + { + $cntBefore = $this->ProductCategoryVariants->find()->count(); + + $this->loginUserByRole('admin'); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'add', + ]; + $data = [ + 'name' => 'Size', + 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', + 'variant_type_id' => 1, + 'enabled' => true, + ]; + $this->post($url, $data); + $this->assertResponseCode(302); + $this->assertRedirectContains('product-category-variants'); + + $cntAfter = $this->ProductCategoryVariants->find()->count(); + $this->assertEquals($cntBefore + 1, $cntAfter); + } + + /** + * Test add method + * + * Tests a POST request to the add action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::add() + * @throws Exception + * + * @return void + */ + public function testAddPostLoggedInFailure(): void + { + $cntBefore = $this->ProductCategoryVariants->find()->count(); + + $this->loginUserByRole('admin'); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'add', + ]; + $data = [ + 'name' => '', + 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', + 'variant_type_id' => 1, + 'enabled' => true, + ]; + $this->post($url, $data); + $this->assertResponseCode(200); + + $cntAfter = $this->ProductCategoryVariants->find()->count(); + $this->assertEquals($cntBefore, $cntAfter); + } + + /** + * Test edit method + * + * Tests the edit action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::edit() + * @throws Exception + * + * @return void + */ + public function testEditGet(): void + { + $this->loginUserByRole('admin'); + $id = '5a386e9f-6e7a-4ae7-9360-c8e529f78d93'; + + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'edit', + $id, + ]; + $this->get($url); + $this->assertResponseCode(200); + } + + /** + * Test edit method + * + * Tests a PUT request to the edit action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::edit() + * @throws Exception + * + * @return void + */ + public function testEditPutLoggedInSuccess(): void + { + $this->loginUserByRole('admin'); + $id = '5a386e9f-6e7a-4ae7-9360-c8e529f78d93'; + $before = $this->ProductCategoryVariants->get($id); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'edit', + $id, + ]; + $data = [ + // test new data here + 'name' => 'updated name', + 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', + 'variant_type_id' => 1, + 'enabled' => true, + ]; + $this->put($url, $data); + + $this->assertResponseCode(302); + $this->assertRedirectContains('product-category-variants'); + + $after = $this->ProductCategoryVariants->get($id); + // assert saved properly below + } + + /** + * Test edit method + * + * Tests a PUT request to the edit action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::edit() + * @throws Exception + * + * @return void + */ + public function testEditPutLoggedInFailure(): void + { + $this->loginUserByRole('admin'); + $id = '5a386e9f-6e7a-4ae7-9360-c8e529f78d93'; + $before = $this->ProductCategoryVariants->get($id); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'edit', + $id, + ]; + $data = [ + 'name' => '', + 'product_category_id' => 'db4b4273-eddc-46d4-93c8-45cf7c6e058e', + 'variant_type_id' => 1, + 'enabled' => true, + ]; + $this->put($url, $data); + $this->assertResponseCode(200); + $after = $this->ProductCategoryVariants->get($id); + + // assert save failed below + } + + /** + * Test delete method + * + * Tests the delete action with a logged in user + * + * @uses \CakeProducts\Controller\ProductCategoryVariantsController::delete() + * @throws Exception + * + * @return void + */ + public function testDelete(): void + { + $cntBefore = $this->ProductCategoryVariants->find()->count(); + + $this->loginUserByRole('admin'); + $id = '5a386e9f-6e7a-4ae7-9360-c8e529f78d93'; + + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductCategoryVariants', + 'action' => 'delete', + $id, + ]; + $this->delete($url); + $this->assertResponseCode(302); + $this->assertRedirectContains('product-category-variants'); + + $cntAfter = $this->ProductCategoryVariants->find()->count(); + $this->assertEquals($cntBefore - 1, $cntAfter); + } +}