diff --git a/.gitea/workflows/status.yaml b/.gitea/workflows/status.yaml new file mode 100644 index 0000000..c703766 --- /dev/null +++ b/.gitea/workflows/status.yaml @@ -0,0 +1,31 @@ +--- +name: CI Build Status + +on: + workflow_run: + workflows: + - CI + types: + - completed +jobs: + on-success: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + steps: + - name: Webhook + uses: zzzze/webhook-trigger@master + with: + data: "{\"text\":\"${{ gitea.repository }} build succeeded 🎉\"}" + webhook_url: ${{ secrets.BUILD_STATUS_NOTIFY_WEBHOOK_URL }} + options: "-H \"Content-Type: application/json\"" + + on-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + steps: + - name: Webhook + uses: zzzze/webhook-trigger@master + with: + data: "{\"text\":\"${{ gitea.repository }} build failed 🔎\"}" + webhook_url: ${{ secrets.BUILD_STATUS_NOTIFY_WEBHOOK_URL }} + options: "-H \"Content-Type: application/json\"" diff --git a/composer.json b/composer.json index dd4324c..32f6214 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,14 @@ "cakephp/cakephp": "^5.1" }, "require-dev": { - "phpunit/phpunit": "^10.1", + "phpunit/phpunit": "^10.3", "cakephp/migrations": "^4.0.0", "phpstan/phpstan": "^2.1", "dereuromark/composer-prefer-lowest": "^0.1.10", "cakedc/cakephp-phpstan": "^4.1", - "fig-r/psr2r-sniffer": "^2.7" + "cakephp/authentication": "^3.0", + "fig-r/psr2r-sniffer": "^2.7", + "cakephp/authorization": "3.x-dev" }, "suggest": { "hi-powered-dev/cake-products": "Manage products and SKUs with a heirarhical category structure, includes attributes and variants" diff --git a/phpstan.neon b/phpstan.neon index b251d7d..0b4f42c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,3 +8,4 @@ parameters: bootstrapFiles: - tests/bootstrap.php treatPhpDocTypesAsCertain: false + ignoreErrors: diff --git a/src/Controller/CartItemsController.php b/src/Controller/CartItemsController.php index c77eb41..a7a234a 100644 --- a/src/Controller/CartItemsController.php +++ b/src/Controller/CartItemsController.php @@ -9,8 +9,9 @@ use Cake\Log\Log; /** * CartItems Controller * + * @property \CakeCarts\Controller\Component\ShoppingCartComponent $ShoppingCart * @property \CakeCarts\Model\Table\CartItemsTable $CartItems - * @property \Authorization\Controller\Component\AuthorizationComponent $Authorization + * @property \Authorization\Controller\Component\AuthorizationComponent|null $Authorization */ class CartItemsController extends AppController { diff --git a/src/Controller/Component/ShoppingCartComponent.php b/src/Controller/Component/ShoppingCartComponent.php index 66c5459..8e840cc 100644 --- a/src/Controller/Component/ShoppingCartComponent.php +++ b/src/Controller/Component/ShoppingCartComponent.php @@ -73,7 +73,6 @@ class ShoppingCartComponent extends Component { } else { $cartsQ->where(['session_id' => $sessionId]); } - return $cartsQ->firstOrFail(); } diff --git a/src/Model/Table/CartsTable.php b/src/Model/Table/CartsTable.php index 4b2ba78..5ffecb2 100644 --- a/src/Model/Table/CartsTable.php +++ b/src/Model/Table/CartsTable.php @@ -11,7 +11,7 @@ use CakeCarts\Model\Enum\CartTypeId; /** * Carts Model * - * @property \CakeCarts\Model\Table\UsersTable&\Cake\ORM\Association\BelongsTo $Users + * @property \Cake\ORM\Association\BelongsTo $Users * @property \CakeCarts\Model\Table\CartItemsTable&\Cake\ORM\Association\HasMany $CartItems * * @method \CakeCarts\Model\Entity\Cart newEmptyEntity() diff --git a/tests/Fixture/CartsFixture.php b/tests/Fixture/CartsFixture.php index 7d0cec7..54fb917 100644 --- a/tests/Fixture/CartsFixture.php +++ b/tests/Fixture/CartsFixture.php @@ -22,7 +22,7 @@ class CartsFixture extends TestFixture { [ 'id' => '74d1aa54-92a2-4039-bc10-61e1190c51ea', 'cart_type_id' => CartTypeId::Cart->value, - 'session_id' => 'session_1', + 'session_id' => 'cli', 'user_id' => null, 'user_id_uuid' => '5a34a6ae-7d3f-4dcf-bac7-57335b51e697', 'created' => '2025-10-08 09:55:15', @@ -36,7 +36,7 @@ class CartsFixture extends TestFixture { [ 'id' => '74d1aa54-92a2-4039-bc10-61e1190c51eb', 'cart_type_id' => CartTypeId::Cart->value, - 'session_id' => 'session_1', + 'session_id' => 'cli', 'user_id' => null, 'user_id_uuid' => '5a34a6ae-7d3f-4dcf-bac7-57335b51e697', 'created' => '2025-10-08 09:55:15', diff --git a/tests/TestCase/Controller/CartItemsControllerTest.php b/tests/TestCase/Controller/CartItemsControllerTest.php index c67da99..6e76559 100644 --- a/tests/TestCase/Controller/CartItemsControllerTest.php +++ b/tests/TestCase/Controller/CartItemsControllerTest.php @@ -91,7 +91,6 @@ class CartItemsControllerTest extends TestCase { ]; $this->session(['Auth.User.id' => 1]); $this->session(['Auth.id' => 1]); - $this->session(['CakeCarts.session_id' => 'session_1']); $before = $this->CartItems->get($id, contain: ['Carts']); // dd($before); $skuId = '3a477e3e-7977-4813-81f6-f85949613979'; diff --git a/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php b/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php index 1d59301..fde05ab 100644 --- a/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php +++ b/tests/TestCase/Controller/Component/ShoppingCartComponentTest.php @@ -4,6 +4,13 @@ declare(strict_types=1); 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; use CakeCarts\Controller\Component\ShoppingCartComponent; @@ -12,13 +19,21 @@ use CakeCarts\Controller\Component\ShoppingCartComponent; */ class ShoppingCartComponentTest extends TestCase { - /** - * Test subject + use IntegrationTestTrait; + + protected ShoppingCartComponent $component; + protected Controller $controller; + + /** + * Fixtures * - * @var \CakeCarts\Controller\Component\ShoppingCartComponent + * @var array */ - protected $ShoppingCart; - + protected array $fixtures = [ + 'plugin.CakeCarts.CartItems', + 'plugin.CakeCarts.Carts', + ]; + /** * setUp method * @@ -26,8 +41,11 @@ class ShoppingCartComponentTest extends TestCase { */ protected function setUp(): void { parent::setUp(); - $registry = new ComponentRegistry(); - $this->ShoppingCart = new ShoppingCartComponent($registry); + $request = new ServerRequest(); + $this->controller = new Controller($request); + $registry = new ComponentRegistry($this->controller); + + $this->component = new ShoppingCartComponent($registry); } /** @@ -40,5 +58,67 @@ class ShoppingCartComponentTest extends TestCase { 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); + } + + /** + * @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); + } }