296 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			296 lines
		
	
	
		
			7.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\ProductsController;
 | |
| use CakeProducts\Model\Table\ProductCatalogsTable;
 | |
| use CakeProducts\Model\Table\ProductsTable;
 | |
| use PHPUnit\Exception;
 | |
| 
 | |
| /**
 | |
|  * CakeProducts\Controller\ProductsController Test Case
 | |
|  *
 | |
|  * @uses ProductsController
 | |
|  */
 | |
| class ProductsControllerTest extends BaseControllerTest
 | |
| {
 | |
|     /**
 | |
|      * Test subject table
 | |
|      *
 | |
|      * @var ProductsTable|Table
 | |
|      */
 | |
|     protected $Products;
 | |
| 
 | |
|     /**
 | |
|      * Fixtures
 | |
|      *
 | |
|      * @var array<string>
 | |
|      */
 | |
|     protected array $fixtures = [
 | |
|         'plugin.CakeProducts.Products',
 | |
|         'plugin.CakeProducts.ProductCategories',
 | |
| //        'plugin.CakeProducts.ProductCatalogs',
 | |
|     ];
 | |
| 
 | |
|     /**
 | |
|      * setUp method
 | |
|      *
 | |
|      * @return void
 | |
|      */
 | |
|     protected function setUp(): void
 | |
|     {
 | |
|         parent::setUp();
 | |
| //        $this->enableCsrfToken();
 | |
| //        $this->enableSecurityToken();
 | |
|         $config = $this->getTableLocator()->exists('Products') ? [] : ['className' => ProductsTable::class];
 | |
|         $this->Products = $this->getTableLocator()->get('Products', $config);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * tearDown method
 | |
|      *
 | |
|      * @return void
 | |
|      */
 | |
|     protected function tearDown(): void
 | |
|     {
 | |
|         unset($this->Products);
 | |
| 
 | |
|         parent::tearDown();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test index method
 | |
|     *
 | |
|     * Tests the index action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|     *
 | |
|     * @uses ProductsController::index
 | |
|     */
 | |
|     public function testIndexGet(): void
 | |
|     {
 | |
|         $this->loginUserByRole('admin');
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'index',
 | |
|         ];
 | |
|         $this->get($url);
 | |
|         $this->assertResponseCode(200);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test view method
 | |
|     *
 | |
|     * Tests the view action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|     *
 | |
|     * @uses ProductsController::view
 | |
|     */
 | |
|     public function testViewGet(): void
 | |
|     {
 | |
|         $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
 | |
|         $this->loginUserByRole('admin');
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'view',
 | |
|             $id,
 | |
|         ];
 | |
|         $this->get($url);
 | |
|         $this->assertResponseCode(200);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test add method
 | |
|     *
 | |
|     * Tests the add action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|     *
 | |
|     * @uses ProductsController::add
 | |
|     */
 | |
|     public function testAddGet(): void
 | |
|     {
 | |
|         $cntBefore = $this->Products->find()->count();
 | |
| 
 | |
|         $this->loginUserByRole('admin');
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'add',
 | |
|         ];
 | |
|         $this->get($url);
 | |
|         $this->assertResponseCode(200);
 | |
| 
 | |
|         $cntAfter = $this->Products->find()->count();
 | |
|         $this->assertEquals($cntBefore, $cntAfter);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test add method
 | |
|     *
 | |
|     * Tests a POST request to the add action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|     *
 | |
|     * @uses ProductsController::add
 | |
|     */
 | |
|     public function testAddPostFailure(): void
 | |
|     {
 | |
|         $cntBefore = $this->Products->find()->count();
 | |
| 
 | |
|         $this->loginUserByRole('admin');
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'add',
 | |
|         ];
 | |
|         $data = [
 | |
|             'product_catalog_id' => '',
 | |
|             'product_category_id' => '',
 | |
|             'name' => '',
 | |
|             'product_type_id' => 1,
 | |
|         ];
 | |
|         $this->post($url, $data);
 | |
|         $this->assertResponseCode(200);
 | |
| 
 | |
|         $cntAfter = $this->Products->find()->count();
 | |
|         $this->assertEquals($cntBefore, $cntAfter);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test edit method
 | |
|     *
 | |
|     * Tests the edit action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|      *
 | |
|     * @uses ProductsController::edit
 | |
|     */
 | |
|     public function testEditGet(): void
 | |
|     {
 | |
|         $this->loginUserByRole('admin');
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'edit',
 | |
|             'cfc98a9a-29b2-44c8-b587-8156adc05317',
 | |
|         ];
 | |
|         $this->get($url);
 | |
|         $this->assertResponseCode(200);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test edit method
 | |
|     *
 | |
|     * Tests a PUT request to the edit action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|      *
 | |
|     * @uses ProductsController::edit
 | |
|     */
 | |
|     public function testEditPutSuccess(): void
 | |
|     {
 | |
|         $this->loginUserByRole('admin');
 | |
|         $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
 | |
|         $before = $this->Products->get($id);
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'edit',
 | |
|             $id,
 | |
|         ];
 | |
|         $data = [
 | |
|             // test new data here
 | |
|             'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
 | |
|             'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23',
 | |
|             'name' => 'edited product name',
 | |
|             'product_type_id' => 1,
 | |
|         ];
 | |
|         $this->put($url, $data);
 | |
| 
 | |
|         $this->assertResponseCode(302);
 | |
|         $this->assertRedirectContains('products');
 | |
| 
 | |
|         $after = $this->Products->get($id);
 | |
|         $this->assertEquals($data['name'], $after->name);
 | |
|         // assert saved properly below
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test edit method
 | |
|     *
 | |
|     * Tests a PUT request to the edit action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|      * @throws Exception
 | |
|      *
 | |
|     * @uses ProductsController::edit
 | |
|     */
 | |
|     public function testEditPutFailure(): void
 | |
|     {
 | |
|         $this->loginUserByRole('admin');
 | |
|         $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
 | |
|         $before = $this->Products->get($id);
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'edit',
 | |
|             $id,
 | |
|         ];
 | |
|         $data = [
 | |
|             'product_catalog_id' => '',
 | |
|             'product_category_id' => '',
 | |
|             'name' => 'edited name not gonna take',
 | |
|             'product_type_id' => 1,
 | |
|         ];
 | |
|         $this->put($url, $data);
 | |
|         $this->assertResponseCode(200);
 | |
|         $after = $this->Products->get($id);
 | |
|         $this->assertEquals($before->name, $after->name);
 | |
|         $this->assertEquals($before->product_category_id, $after->product_category_id);
 | |
|         // assert save failed below
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|     * Test delete method
 | |
|     *
 | |
|     * Tests the delete action with a logged in user
 | |
|     *
 | |
|     * @return void
 | |
|     *@throws Exception
 | |
|      *
 | |
|      * @uses ProductsController::delete
 | |
|     */
 | |
|     public function testDelete(): void
 | |
|     {
 | |
|         $cntBefore = $this->Products->find()->count();
 | |
| 
 | |
|         $this->loginUserByRole('admin');
 | |
|         $url = [
 | |
|             'plugin' => 'CakeProducts',
 | |
|             'controller' => 'Products',
 | |
|             'action' => 'delete',
 | |
|             'cfc98a9a-29b2-44c8-b587-8156adc05317',
 | |
|         ];
 | |
|         $this->delete($url);
 | |
|         $this->assertResponseCode(302);
 | |
|         $this->assertRedirectContains('products');
 | |
| 
 | |
|         $cntAfter = $this->Products->find()->count();
 | |
|         $this->assertEquals($cntBefore - 1, $cntAfter);
 | |
|     }
 | |
| }
 |