test additions variants table

This commit is contained in:
2025-06-29 22:25:38 -07:00
parent 5bb1110e7f
commit 5cba7de890
9 changed files with 414 additions and 23 deletions

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* ProductCategoryVariantsFixture
*/
class ProductCategoryVariantsFixture extends TestFixture
{
/**
* Init method
*
* @return void
*/
public function init(): void
{
$this->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();
}
}

View File

@@ -0,0 +1,325 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\TestCase\Controller;
use CakeProducts\Controller\ProductCategoryVariantsController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeProducts\Model\Table\ProductCategoryVariantsTable;
use PHPUnit\Exception;
/**
* CakeProducts\Controller\ProductCategoryVariantsController Test Case
*
* @uses \CakeProducts\Controller\ProductCategoryVariantsController
*/
class ProductCategoryVariantsControllerTest extends BaseControllerTest
{
/**
* Test subject
*
* @var ProductCategoryVariantsTable|Table
*/
protected $ProductCategoryVariants;
/**
* Fixtures
*
* @var array<string>
*/
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);
}
}