From 534c3ab7dc49e402a0a19e69bc758a33ab3a0cad Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Wed, 11 Feb 2026 19:27:09 -0800 Subject: [PATCH] cs fixes --- .../Component/ShoppingCartComponent.php | 1 + .../Component/ShoppingCartComponentTest.php | 135 +++++++++--------- 2 files changed, 65 insertions(+), 71 deletions(-) diff --git a/src/Controller/Component/ShoppingCartComponent.php b/src/Controller/Component/ShoppingCartComponent.php index 8e840cc..66c5459 100644 --- a/src/Controller/Component/ShoppingCartComponent.php +++ b/src/Controller/Component/ShoppingCartComponent.php @@ -73,6 +73,7 @@ class ShoppingCartComponent extends Component { } else { $cartsQ->where(['session_id' => $sessionId]); } + return $cartsQ->firstOrFail(); } diff --git a/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php b/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php index fde05ab..4ed7453 100644 --- a/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php +++ b/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php @@ -6,9 +6,6 @@ namespace CakeCarts\Test\TestCase\Controller\Component; use Cake\Controller\ComponentRegistry; 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; @@ -19,21 +16,22 @@ use CakeCarts\Controller\Component\ShoppingCartComponent; */ class ShoppingCartComponentTest extends TestCase { - use IntegrationTestTrait; - - protected ShoppingCartComponent $component; - protected Controller $controller; - - /** + use IntegrationTestTrait; + + protected ShoppingCartComponent $component; + + protected Controller $controller; + + /** * Fixtures * * @var array */ - protected array $fixtures = [ - 'plugin.CakeCarts.CartItems', - 'plugin.CakeCarts.Carts', - ]; - + protected array $fixtures = [ + 'plugin.CakeCarts.CartItems', + 'plugin.CakeCarts.Carts', + ]; + /** * setUp method * @@ -41,11 +39,11 @@ class ShoppingCartComponentTest extends TestCase { */ protected function setUp(): void { parent::setUp(); - $request = new ServerRequest(); - $this->controller = new Controller($request); - $registry = new ComponentRegistry($this->controller); - - $this->component = new ShoppingCartComponent($registry); + $request = new ServerRequest(); + $this->controller = new Controller($request); + $registry = new ComponentRegistry($this->controller); + + $this->component = new ShoppingCartComponent($registry); } /** @@ -54,71 +52,66 @@ class ShoppingCartComponentTest extends TestCase { * @return void */ protected function tearDown(): void { - unset($this->ShoppingCart); + unset($this->component); parent::tearDown(); } - - /** + + /** * @return void */ - public function testGetCartForUserById() - { - $this->session([ - 'CakeCarts.session_id' => 'cli', - ]); - $cart = $this->component->getCartForUserById('74d1aa54-92a2-4039-bc10-61e1190c51ea'); - $this->assertNotNull($cart); - } - - /** + 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'); - } - - /** + 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()); - } - - /** + public function testGetUserIdField() { + $this->assertNotNull($this->component->getUserIdField()); + } + + /** * @return void */ - public function testGetSessionId() - { - $this->assertNotNull($this->component->getSessionId()); - } - - /** + 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); - } - - /** + 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); - } + 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); + } + }