327 lines
8.3 KiB
PHP
327 lines
8.3 KiB
PHP
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace CakeProducts\Test\TestCase\Controller;
|
||
|
|
||
|
use Cake\ORM\Table;
|
||
|
use Cake\TestSuite\IntegrationTestTrait;
|
||
|
use Cake\TestSuite\TestCase;
|
||
|
use CakeProducts\Controller\ProductSkusController;
|
||
|
use CakeProducts\Model\Table\ProductSkusTable;
|
||
|
use CakeProducts\Model\Table\ProductsTable;
|
||
|
use PHPUnit\Exception;
|
||
|
|
||
|
/**
|
||
|
* App\Controller\ProductSkusController Test Case
|
||
|
*
|
||
|
* @uses \CakeProducts\Controller\ProductSkusController
|
||
|
*/
|
||
|
class ProductSkusControllerTest extends BaseControllerTest
|
||
|
{
|
||
|
/**
|
||
|
* Test subject table
|
||
|
*
|
||
|
* @var ProductSkusTable|Table
|
||
|
*/
|
||
|
protected $ProductSkus;
|
||
|
|
||
|
/**
|
||
|
* Fixtures
|
||
|
*
|
||
|
* @var array<string>
|
||
|
*/
|
||
|
protected array $fixtures = [
|
||
|
'plugin.CakeProducts.ProductSkus',
|
||
|
'plugin.CakeProducts.Products',
|
||
|
'plugin.CakeProducts.ProductAttributes',
|
||
|
];
|
||
|
|
||
|
/**
|
||
|
* setUp method
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
protected function setUp(): void
|
||
|
{
|
||
|
parent::setUp();
|
||
|
// $this->enableCsrfToken();
|
||
|
// $this->enableSecurityToken();
|
||
|
$config = $this->getTableLocator()->exists('ProductSkus') ? [] : ['className' => ProductSkusTable::class];
|
||
|
$this->ProductSkus = $this->getTableLocator()->get('ProductSkus', $config);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* tearDown method
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
protected function tearDown(): void
|
||
|
{
|
||
|
unset($this->ProductSkus);
|
||
|
|
||
|
parent::tearDown();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test index method
|
||
|
*
|
||
|
* Tests the index action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::index()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testIndexGet(): void
|
||
|
{
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'index',
|
||
|
];
|
||
|
$this->get($url);
|
||
|
$this->assertResponseCode(200);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test view method
|
||
|
*
|
||
|
* Tests the view action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::view()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testViewGet(): void
|
||
|
{
|
||
|
$id = '3a477e3e-7977-4813-81f6-f85949613979';
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'view',
|
||
|
$id,
|
||
|
];
|
||
|
$this->get($url);
|
||
|
$this->assertResponseCode(200);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test add method
|
||
|
*
|
||
|
* Tests the add action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::add()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testAddGet(): void
|
||
|
{
|
||
|
$cntBefore = $this->ProductSkus->find()->count();
|
||
|
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'add',
|
||
|
];
|
||
|
$this->get($url);
|
||
|
$this->assertResponseCode(200);
|
||
|
|
||
|
$cntAfter = $this->ProductSkus->find()->count();
|
||
|
$this->assertEquals($cntBefore, $cntAfter);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test add method
|
||
|
*
|
||
|
* Tests a POST request to the add action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::add()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testAddPostSuccess(): void
|
||
|
{
|
||
|
$cntBefore = $this->ProductSkus->find()->count();
|
||
|
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'add',
|
||
|
];
|
||
|
$data = [
|
||
|
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'price' => 1.5,
|
||
|
'cost' => 1.5,
|
||
|
];
|
||
|
$this->post($url, $data);
|
||
|
$this->assertResponseCode(302);
|
||
|
$this->assertRedirectContains('product-skus');
|
||
|
|
||
|
$cntAfter = $this->ProductSkus->find()->count();
|
||
|
$this->assertEquals($cntBefore + 1, $cntAfter);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test add method
|
||
|
*
|
||
|
* Tests a POST request to the add action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::add()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testAddPostFailure(): void
|
||
|
{
|
||
|
$cntBefore = $this->ProductSkus->find()->count();
|
||
|
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'add',
|
||
|
];
|
||
|
$data = [
|
||
|
'product_id' => '999999999', //does not exist
|
||
|
'sku' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'price' => 1.5,
|
||
|
'cost' => 1.5,
|
||
|
];
|
||
|
$this->post($url, $data);
|
||
|
$this->assertResponseCode(200);
|
||
|
|
||
|
$cntAfter = $this->ProductSkus->find()->count();
|
||
|
$this->assertEquals($cntBefore, $cntAfter);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test edit method
|
||
|
*
|
||
|
* Tests the edit action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::edit()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testEditGet(): void
|
||
|
{
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'edit',
|
||
|
'3a477e3e-7977-4813-81f6-f85949613979',
|
||
|
];
|
||
|
$this->get($url);
|
||
|
$this->assertResponseCode(200);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test edit method
|
||
|
*
|
||
|
* Tests a PUT request to the edit action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::edit()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testEditPutSuccess(): void
|
||
|
{
|
||
|
$this->loginUserByRole('admin');
|
||
|
$id = '3a477e3e-7977-4813-81f6-f85949613979';
|
||
|
$before = $this->ProductSkus->get($id);
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'edit',
|
||
|
$id,
|
||
|
];
|
||
|
$data = [
|
||
|
// test new data here
|
||
|
];
|
||
|
$this->put($url, $data);
|
||
|
|
||
|
$this->assertResponseCode(302);
|
||
|
$this->assertRedirectContains('product-skus');
|
||
|
|
||
|
$after = $this->ProductSkus->get($id);
|
||
|
// assert saved properly below
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test edit method
|
||
|
*
|
||
|
* Tests a PUT request to the edit action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::edit()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testEditPutFailure(): void
|
||
|
{
|
||
|
$this->loginUserByRole('admin');
|
||
|
$id = '3a477e3e-7977-4813-81f6-f85949613979';
|
||
|
$before = $this->ProductSkus->get($id);
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'edit',
|
||
|
$id,
|
||
|
];
|
||
|
$data = [
|
||
|
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'sku' => '',
|
||
|
'barcode' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||
|
'price' => 1.5,
|
||
|
'cost' => 1.5,
|
||
|
];
|
||
|
$this->put($url, $data);
|
||
|
$this->assertResponseCode(200);
|
||
|
$after = $this->ProductSkus->get($id);
|
||
|
|
||
|
// assert save failed below
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test delete method
|
||
|
*
|
||
|
* Tests the delete action with a logged in user
|
||
|
*
|
||
|
* @uses \App\Controller\ProductSkusController::delete()
|
||
|
* @throws Exception
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function testDelete(): void
|
||
|
{
|
||
|
$cntBefore = $this->ProductSkus->find()->count();
|
||
|
|
||
|
$this->loginUserByRole('admin');
|
||
|
$url = [
|
||
|
'plugin' => 'CakeProducts',
|
||
|
'controller' => 'ProductSkus',
|
||
|
'action' => 'delete',
|
||
|
'3a477e3e-7977-4813-81f6-f85949613979',
|
||
|
];
|
||
|
$this->delete($url);
|
||
|
$this->assertResponseCode(302);
|
||
|
$this->assertRedirectContains('product-skus');
|
||
|
|
||
|
$cntAfter = $this->ProductSkus->find()->count();
|
||
|
$this->assertEquals($cntBefore - 1, $cntAfter);
|
||
|
}
|
||
|
}
|