Files
cake-carts/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php

125 lines
3.0 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace CakeCarts\Test\TestCase\Controller\Component;
use Cake\Controller\ComponentRegistry;
2026-01-23 22:40:45 -08:00
use Cake\Controller\Controller;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\Event;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeCarts\Controller\Component\ShoppingCartComponent;
/**
* CakeCarts\Controller\Component\ShoppingCartComponent Test Case
*/
2026-01-23 20:32:22 -08:00
class ShoppingCartComponentTest extends TestCase {
2026-01-23 22:40:45 -08:00
use IntegrationTestTrait;
protected ShoppingCartComponent $component;
protected Controller $controller;
/**
* Fixtures
*
2026-01-23 22:40:45 -08:00
* @var array<string>
*/
2026-01-23 22:40:45 -08:00
protected array $fixtures = [
'plugin.CakeCarts.CartItems',
'plugin.CakeCarts.Carts',
];
2026-01-23 20:32:22 -08:00
/**
* setUp method
*
* @return void
*/
2026-01-23 20:32:22 -08:00
protected function setUp(): void {
parent::setUp();
2026-01-23 22:40:45 -08:00
$request = new ServerRequest();
$this->controller = new Controller($request);
$registry = new ComponentRegistry($this->controller);
$this->component = new ShoppingCartComponent($registry);
2026-01-23 20:32:22 -08:00
}
/**
* tearDown method
*
* @return void
*/
2026-01-23 20:32:22 -08:00
protected function tearDown(): void {
unset($this->ShoppingCart);
parent::tearDown();
}
2026-01-23 22:40:45 -08:00
/**
* @return void
*/
public function testGetCartForUserById()
{
$this->session([
'CakeCarts.session_id' => 'cli',
]);
$cart = $this->component->getCartForUserById('74d1aa54-92a2-4039-bc10-61e1190c51ea');
$this->assertNotNull($cart);
}
/**
* @return void
*/
public function testGetCartForUserByIdDoesNotExist()
{
$this->expectException(RecordNotFoundException::class);
$cart = $this->component->getCartForUserById('74d1aa54-92a2-4039-bc10-61e1190c51ec');
}
/**
* @return void
*/
public function testGetUserIdField()
{
$this->assertNotNull($this->component->getUserIdField());
}
/**
* @return void
*/
public function testGetSessionId()
{
$this->assertNotNull($this->component->getSessionId());
}
/**
* @return void
*/
public function testCheckIfIsOwnCartIsOwnCart()
{
$this->session([
'CakeCarts.session_id' => 'session_1',
]);
$cart = $this->fetchTable('Carts')->get('74d1aa54-92a2-4039-bc10-61e1190c51ea');
$this->component->checkIfIsOwnCart($cart);
// if exception not thrown test passes
$this->assertEquals(1, 1);
}
/**
* @return void
*/
public function testCheckIfIsOwnCartIsNotOwnCart()
{
$this->expectExceptionCode(404);
$cart = $this->fetchTable('Carts')->get('74d1aa54-92a2-4039-bc10-61e1190c51ec');
$this->component->checkIfIsOwnCart($cart);
// if exception not thrown test fails
$this->expectException(RecordNotFoundException::class);
}
}