From 9def21afc344ab4195c5b1d56f88789300baad6f Mon Sep 17 00:00:00 2001 From: Jakub Lech Date: Wed, 14 Sep 2022 10:21:50 +0200 Subject: [PATCH] init commit --- .editorconfig | 15 ++ .gitattributes | 16 ++ .github/workflows/build.yml | 57 +++++++ .gitignore | 8 + CHANGELOG.md | 22 +++ CONTRIBUTING.md | 32 ++++ LICENSE.md | 21 +++ README.md | 81 ++++++++++ composer.json | 36 +++++ phpcs.xml.dist | 14 ++ phpstan.neon | 14 ++ phpunit.xml.dist | 28 ++++ psalm.xml | 15 ++ src/Events/AddAddressInfo.php | 35 +++++ src/Events/AddPaymentInfo.php | 46 ++++++ src/Events/AddShippingInfo.php | 46 ++++++ src/Events/AddToCart.php | 23 +++ src/Events/AddToWishlist.php | 23 +++ src/Events/BeginCheckout.php | 35 +++++ src/Events/EarnVirtualCurrency.php | 46 ++++++ src/Events/EventInterface.php | 16 ++ src/Events/GenerateLead.php | 36 +++++ src/Events/ItemsContainerEvent.php | 55 +++++++ src/Events/ItemsContainerInterface.php | 18 +++ src/Events/JoinGroup.php | 35 +++++ src/Events/LevelEnd.php | 46 ++++++ src/Events/LevelStart.php | 35 +++++ src/Events/LevelUp.php | 46 ++++++ src/Events/Login.php | 35 +++++ src/Events/PostScore.php | 56 +++++++ src/Events/Purchase.php | 80 ++++++++++ src/Events/Refund.php | 78 +++++++++ src/Events/RemoveFromCart.php | 22 +++ src/Events/Search.php | 34 ++++ src/Events/SelectContent.php | 47 ++++++ src/Events/SelectItem.php | 52 ++++++ src/Events/SelectPromotion.php | 88 +++++++++++ src/Events/Share.php | 58 +++++++ src/Events/Signup.php | 36 +++++ src/Events/SpendVirtualCurrency.php | 50 ++++++ src/Events/TutorialBegin.php | 24 +++ src/Events/TutorialComplete.php | 24 +++ src/Events/UnlockAchievement.php | 34 ++++ src/Events/ViewCart.php | 22 +++ src/Events/ViewItem.php | 22 +++ src/Events/ViewItemList.php | 52 ++++++ src/Events/ViewPromotion.php | 86 ++++++++++ src/Exception/CurrencyMismatchException.php | 16 ++ src/Item/Item.php | 165 ++++++++++++++++++++ src/Item/ItemInterface.php | 17 ++ src/Item/PromotionItem.php | 43 +++++ src/JsonSerializeTrait.php | 37 +++++ tests/Unit/Events/AddPaymentInfoTest.php | 57 +++++++ tests/Unit/Events/AddShippingInfoTest.php | 57 +++++++ tests/Unit/Events/AddToCartTest.php | 54 +++++++ tests/Unit/Events/AddToWishListTest.php | 54 +++++++ tests/Unit/Events/BeginCheckoutTest.php | 55 +++++++ tests/Unit/Events/GenerateLeadTest.php | 39 +++++ tests/Unit/Events/JoinGroupTest.php | 45 ++++++ tests/Unit/Events/LoginTest.php | 44 ++++++ tests/Unit/Events/PurchaseTest.php | 54 +++++++ tests/Unit/Events/RemoveFromCartTest.php | 54 +++++++ tests/Unit/Events/SearchTest.php | 43 +++++ tests/Unit/Events/SelectItemTest.php | 47 ++++++ tests/Unit/Events/SignupTest.php | 45 ++++++ tests/Unit/Events/ViewCartTest.php | 54 +++++++ tests/Unit/Events/ViewItemListTest.php | 57 +++++++ tests/Unit/Events/ViewItemTest.php | 54 +++++++ tests/Unit/Events/ViewPromotionTest.php | 60 +++++++ tests/Unit/ItemTest.php | 73 +++++++++ tests/Unit/ItemsContainerEventTest.php | 42 +++++ 71 files changed, 3066 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpcs.xml.dist create mode 100644 phpstan.neon create mode 100644 phpunit.xml.dist create mode 100644 psalm.xml create mode 100644 src/Events/AddAddressInfo.php create mode 100644 src/Events/AddPaymentInfo.php create mode 100644 src/Events/AddShippingInfo.php create mode 100644 src/Events/AddToCart.php create mode 100644 src/Events/AddToWishlist.php create mode 100644 src/Events/BeginCheckout.php create mode 100644 src/Events/EarnVirtualCurrency.php create mode 100644 src/Events/EventInterface.php create mode 100644 src/Events/GenerateLead.php create mode 100644 src/Events/ItemsContainerEvent.php create mode 100644 src/Events/ItemsContainerInterface.php create mode 100644 src/Events/JoinGroup.php create mode 100644 src/Events/LevelEnd.php create mode 100644 src/Events/LevelStart.php create mode 100644 src/Events/LevelUp.php create mode 100644 src/Events/Login.php create mode 100644 src/Events/PostScore.php create mode 100644 src/Events/Purchase.php create mode 100644 src/Events/Refund.php create mode 100644 src/Events/RemoveFromCart.php create mode 100644 src/Events/Search.php create mode 100644 src/Events/SelectContent.php create mode 100644 src/Events/SelectItem.php create mode 100644 src/Events/SelectPromotion.php create mode 100644 src/Events/Share.php create mode 100644 src/Events/Signup.php create mode 100644 src/Events/SpendVirtualCurrency.php create mode 100644 src/Events/TutorialBegin.php create mode 100644 src/Events/TutorialComplete.php create mode 100644 src/Events/UnlockAchievement.php create mode 100644 src/Events/ViewCart.php create mode 100644 src/Events/ViewItem.php create mode 100644 src/Events/ViewItemList.php create mode 100644 src/Events/ViewPromotion.php create mode 100644 src/Exception/CurrencyMismatchException.php create mode 100644 src/Item/Item.php create mode 100644 src/Item/ItemInterface.php create mode 100644 src/Item/PromotionItem.php create mode 100644 src/JsonSerializeTrait.php create mode 100644 tests/Unit/Events/AddPaymentInfoTest.php create mode 100644 tests/Unit/Events/AddShippingInfoTest.php create mode 100644 tests/Unit/Events/AddToCartTest.php create mode 100644 tests/Unit/Events/AddToWishListTest.php create mode 100644 tests/Unit/Events/BeginCheckoutTest.php create mode 100644 tests/Unit/Events/GenerateLeadTest.php create mode 100644 tests/Unit/Events/JoinGroupTest.php create mode 100644 tests/Unit/Events/LoginTest.php create mode 100644 tests/Unit/Events/PurchaseTest.php create mode 100644 tests/Unit/Events/RemoveFromCartTest.php create mode 100644 tests/Unit/Events/SearchTest.php create mode 100644 tests/Unit/Events/SelectItemTest.php create mode 100644 tests/Unit/Events/SignupTest.php create mode 100644 tests/Unit/Events/ViewCartTest.php create mode 100644 tests/Unit/Events/ViewItemListTest.php create mode 100644 tests/Unit/Events/ViewItemTest.php create mode 100644 tests/Unit/Events/ViewPromotionTest.php create mode 100644 tests/Unit/ItemTest.php create mode 100644 tests/Unit/ItemsContainerEventTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b26a9a9 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at https://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..48a95b6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,16 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.editorconfig export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.scrutinizer.yml export-ignore +/.styleci.yml export-ignore +/.travis.yml export-ignore +/PULL_REQUEST_TEMPLATE.md export-ignore +/ISSUE_TEMPLATE.md export-ignore +/phpcs.xml.dist export-ignore +/phpunit.xml.dist export-ignore +/tests export-ignore +/docs export-ignore diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..c623638 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,57 @@ +name: Build + +on: + push: + branches-ignore: + - 'dependabot/**' + pull_request: ~ + release: + types: [created] + schedule: + - + cron: "0 1 * * 6" # Run at 1am every Saturday + workflow_dispatch: ~ + +jobs: + tests: + runs-on: ubuntu-22.04 + + name: "Spinbits Sylius Google Analytics 4 Events Dto's , PHP ${{ matrix.php }}" + + strategy: + fail-fast: false + matrix: + php: ["7.4", "8.0", "8.1"] + + steps: + - + uses: actions/checkout@v2 + + - + name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + extensions: intl + tools: symfony + coverage: none + + - + name: Install PHP dependencies + run: composer install --no-interaction + + - + name: Validate composer.json + run: composer validate --ansi --strict + + - + name: Run PHPStan + run: vendor/bin/phpstan analyse -c phpstan.neon -l max src/ + + - + name: Run Psalm + run: vendor/bin/psalm + + - + name: Run PHPUnit + run: vendor/bin/phpunit --colors=always diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ffb97d --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +build +composer.lock +vendor +phpcs.xml +phpunit.xml +.phpunit.result.cache +.idea + diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1a9ec85 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Changelog + +All notable changes to `spinbits/baselinker-sdk` will be documented in this file. + +Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) principles. + +## NEXT - YYYY-MM-DD + +### Added +- Nothing + +### Deprecated +- Nothing + +### Fixed +- Nothing + +### Removed +- Nothing + +### Security +- Nothing diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e8c21e1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/spinbits/baselinker-sdk). + + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - Check the code style with ``$ composer check-style`` and fix it with ``$ composer fix-style``. + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **Create feature branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + + +## Running Tests + +``` bash +$ composer test +``` + + +**Happy coding**! diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..734c1c3 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2021 spinbits.io + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..76142fe --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +## Google Analytics 4 events dto's + +[![Latest Version on Packagist][ico-version]][link-packagist] +[![Software License][ico-license]](LICENSE.md) +[![Build Status][ico-travis]][link-travis] +[![Total Downloads][ico-downloads]][link-downloads] + +## Description + +This package is implementation of [Google Analytics 4](https://developers.google.com/analytics/devguides/collection/ga4), [events](https://developers.google.com/analytics/devguides/collection/ga4/reference/events). + +This package provides simple interface to build and serialize objects. + +## Install + +Via Composer + +``` bash +$ composer require spinbits/google-analytics-4-events-dto-s +``` + +## Example Usage + +``` php +$purchase = new Purchase($order->getNumber()); + +$purchase->setShipping($order->getShippingTotal()); +$purchase->setTax($order->getTaxTotal()); + +$purchase->addItem(new Item('item_id', 'item_name')); + +//get event as array +$purchase->jsonSerialize(); + +//get event as json +$json = (string) $purchase; +``` + +You can find more examples in tests directory and in this repo: [Google Analytics 4 Enhanced ecommerce Plugin](https://github.com/spinbits/sylius-google-analytics-plugin) + +## Change log + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Testing + +``` bash +$ composer test +``` + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Security + +If you discover any security related issues, please email office@spinbits.io instead of using the issue tracker. + +## Credits + +- [Jakub Lech][link-author] +- [All Contributors][link-contributors] + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +[ico-version]: https://img.shields.io/packagist/v/spinbits/baselinker-sdk.svg?style=flat-square +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square +[ico-travis]: https://img.shields.io/travis/spinbits/baselinker-sdk/master.svg?style=flat-square +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/spinbits/baselinker-sdk.svg?style=flat-square +[ico-code-quality]: https://img.shields.io/scrutinizer/g/spinbits/baselinker-sdk.svg?style=flat-square +[ico-downloads]: https://img.shields.io/packagist/dt/spinbits/baselinker-sdk.svg?style=flat-square + +[link-packagist]: https://packagist.org/packages/spinbits/baselinker-sdk +[link-travis]: https://travis-ci.org/spinbits/baselinker-sdk +[link-scrutinizer]: https://scrutinizer-ci.com/g/spinbits/baselinker-sdk/code-structure +[link-code-quality]: https://scrutinizer-ci.com/g/spinbits/baselinker-sdk +[link-downloads]: https://packagist.org/packages/spinbits/baselinker-sdk +[link-author]: https://github.com/spinbits +[link-contributors]: ../../contributors diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8770d86 --- /dev/null +++ b/composer.json @@ -0,0 +1,36 @@ +{ + "name": "spinbits/google-analytics-4-events-dto-s", + "description": "Google Analytics 4 Dto's", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Jakub Lech", + "email": "j.lech@spinbits.io" + } + ], + "require": { + "php": "~7.4 || ^8.0" + }, + "require-dev": { + "phpunit/phpunit" : ">=8.0", + "squizlabs/php_codesniffer": "^3.0", + "phpstan/phpstan": "^1.8", + "vimeo/psalm": "^4.27" + }, + "autoload": { + "psr-4": { + "Spinbits\\GoogleAnalytics4EventsDtoS\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Spinbits\\BaselinkerSdk\\Tests\\": "tests" + } + }, + "scripts": { + "test": "phpunit", + "check-style": "phpcs src tests", + "fix-style": "phpcbf src tests" + } +} diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..5d696f5 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,14 @@ + + + The coding standard of spinbits/baselinker-sdk package + + + + + + + + + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..9e19eb0 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + level: max + reportUnmatchedIgnoredErrors: false + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + paths: + - src + - tests/Behat + + excludes_analyse: + - 'tests/Unit/**.php' + + ignoreErrors: + - '#Property (.*) is never read, only written.#' diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..1fc7ef2 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,28 @@ + + + + + tests + + + + + src/ + + + + + + + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..3240886 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/src/Events/AddAddressInfo.php b/src/Events/AddAddressInfo.php new file mode 100644 index 0000000..7163a70 --- /dev/null +++ b/src/Events/AddAddressInfo.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class AddAddressInfo extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $coupon = null; + + public function getName(): string + { + return 'add_address_info'; + } + + /** + * @param string|null $coupon + * @return AddAddressInfo + */ + public function setCoupon(?string $coupon): AddAddressInfo + { + $this->coupon = $coupon; + return $this; + } +} diff --git a/src/Events/AddPaymentInfo.php b/src/Events/AddPaymentInfo.php new file mode 100644 index 0000000..0597d0b --- /dev/null +++ b/src/Events/AddPaymentInfo.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class AddPaymentInfo extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $coupon = null; + private ?string $payment_type = null; + + public function getName(): string + { + return 'add_payment_info'; + } + + /** + * @param string|null $coupon + * @return AddPaymentInfo + */ + public function setCoupon(?string $coupon): AddPaymentInfo + { + $this->coupon = $coupon; + return $this; + } + + /** + * @param string|null $payment_type + * @return AddPaymentInfo + */ + public function setPaymentType(?string $payment_type): AddPaymentInfo + { + $this->payment_type = $payment_type; + return $this; + } +} diff --git a/src/Events/AddShippingInfo.php b/src/Events/AddShippingInfo.php new file mode 100644 index 0000000..5d34fd8 --- /dev/null +++ b/src/Events/AddShippingInfo.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class AddShippingInfo extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $coupon = null; + private ?string $shipping_tier = null; + + public function getName(): string + { + return 'add_shipping_info'; + } + + /** + * @param string|null $coupon + * @return AddShippingInfo + */ + public function setCoupon(?string $coupon): AddShippingInfo + { + $this->coupon = $coupon; + return $this; + } + + /** + * @param string|null $shipping_tier + * @return AddShippingInfo + */ + public function setShippingTier(?string $shipping_tier): AddShippingInfo + { + $this->shipping_tier = $shipping_tier; + return $this; + } +} diff --git a/src/Events/AddToCart.php b/src/Events/AddToCart.php new file mode 100644 index 0000000..2e1f05d --- /dev/null +++ b/src/Events/AddToCart.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class AddToCart extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'add_to_cart'; + } +} diff --git a/src/Events/AddToWishlist.php b/src/Events/AddToWishlist.php new file mode 100644 index 0000000..2f2b6f1 --- /dev/null +++ b/src/Events/AddToWishlist.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class AddToWishlist extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'add_to_wishlist'; + } +} diff --git a/src/Events/BeginCheckout.php b/src/Events/BeginCheckout.php new file mode 100644 index 0000000..84acce9 --- /dev/null +++ b/src/Events/BeginCheckout.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class BeginCheckout extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $coupon = null; + + public function getName(): string + { + return 'begin_checkout'; + } + + /** + * @param string|null $coupon + * @return BeginCheckout + */ + public function setCoupon(?string $coupon): BeginCheckout + { + $this->coupon = $coupon; + return $this; + } +} diff --git a/src/Events/EarnVirtualCurrency.php b/src/Events/EarnVirtualCurrency.php new file mode 100644 index 0000000..6de0d43 --- /dev/null +++ b/src/Events/EarnVirtualCurrency.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class EarnVirtualCurrency implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $virtual_currency_name = null; + private ?float $value = null; + + public function getName(): string + { + return 'earn_virtual_currency'; + } + + /** + * @param string|null $virtual_currency_name + * @return EarnVirtualCurrency + */ + public function setVirtualCurrencyName(?string $virtual_currency_name): EarnVirtualCurrency + { + $this->virtual_currency_name = $virtual_currency_name; + return $this; + } + + /** + * @param float|null $value + * @return EarnVirtualCurrency + */ + public function setValue(?float $value): EarnVirtualCurrency + { + $this->value = $value; + return $this; + } +} diff --git a/src/Events/EventInterface.php b/src/Events/EventInterface.php new file mode 100644 index 0000000..b72c31d --- /dev/null +++ b/src/Events/EventInterface.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +interface EventInterface +{ + public function getName(): string; +} diff --git a/src/Events/GenerateLead.php b/src/Events/GenerateLead.php new file mode 100644 index 0000000..71f1f01 --- /dev/null +++ b/src/Events/GenerateLead.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class GenerateLead implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private string $currency; + private float $value; + + /** + * @param string $currency + * @param float $value + */ + public function __construct(string $currency = 'USD', float $value = 0) + { + $this->currency = $currency; + $this->value = $value; + } + + public function getName(): string + { + return 'generate_lead'; + } +} diff --git a/src/Events/ItemsContainerEvent.php b/src/Events/ItemsContainerEvent.php new file mode 100644 index 0000000..228a2f5 --- /dev/null +++ b/src/Events/ItemsContainerEvent.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Exception\CurrencyMismatchException; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class ItemsContainerEvent implements \JsonSerializable, ItemsContainerInterface +{ + use JsonSerializeTrait; + + protected ?string $currency = null; + protected ?float $value = null; + protected array $items = []; + + /** + * @param ItemInterface $item + * @return self + * @throws CurrencyMismatchException + */ + public function addItem(ItemInterface $item): self + { + $this->calculate($item); + $this->items[] = $item; + + return $this; + } + + /** + * @param ItemInterface $item + * @return void + * @throws CurrencyMismatchException + */ + protected function calculate(ItemInterface $item): void + { + if ($this->currency === null and count($this->items) === 0) { + $this->currency = $item->getCurrency(); + } + + if ($item->getCurrency() != $this->currency) { + throw new CurrencyMismatchException(); + } + + $this->value = round((float) $this->value + $item->getValue(), 2); + } +} diff --git a/src/Events/ItemsContainerInterface.php b/src/Events/ItemsContainerInterface.php new file mode 100644 index 0000000..f688740 --- /dev/null +++ b/src/Events/ItemsContainerInterface.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +interface ItemsContainerInterface +{ + public function addItem(ItemInterface $item): self; +} diff --git a/src/Events/JoinGroup.php b/src/Events/JoinGroup.php new file mode 100644 index 0000000..370f90c --- /dev/null +++ b/src/Events/JoinGroup.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class JoinGroup implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $group_id = null; + + public function getName(): string + { + return 'join_group'; + } + + /** + * @param string|null $group_id + * @return JoinGroup + */ + public function setGroupId(?string $group_id): JoinGroup + { + $this->group_id = $group_id; + return $this; + } +} diff --git a/src/Events/LevelEnd.php b/src/Events/LevelEnd.php new file mode 100644 index 0000000..957d6c2 --- /dev/null +++ b/src/Events/LevelEnd.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class LevelEnd implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $level_name = null; + private ?bool $success = null; + + public function getName(): string + { + return 'level_end'; + } + + /** + * @param string|null $level_name + * @return LevelEnd + */ + public function setLevelName(?string $level_name): LevelEnd + { + $this->level_name = $level_name; + return $this; + } + + /** + * @param bool|null $success + * @return LevelEnd + */ + public function setSuccess(?bool $success): LevelEnd + { + $this->success = $success; + return $this; + } +} diff --git a/src/Events/LevelStart.php b/src/Events/LevelStart.php new file mode 100644 index 0000000..1337355 --- /dev/null +++ b/src/Events/LevelStart.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class LevelStart implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $level_name = null; + + public function getName(): string + { + return 'level_start'; + } + + /** + * @param string|null $level_name + * @return LevelStart + */ + public function setLevelName(?string $level_name): LevelStart + { + $this->level_name = $level_name; + return $this; + } +} diff --git a/src/Events/LevelUp.php b/src/Events/LevelUp.php new file mode 100644 index 0000000..e233895 --- /dev/null +++ b/src/Events/LevelUp.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class LevelUp implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?float $level = null; + private ?string $character = null; + + public function getName(): string + { + return 'level_up'; + } + + /** + * @param float|null $level + * @return LevelUp + */ + public function setLevel(?float $level): LevelUp + { + $this->level = $level; + return $this; + } + + /** + * @param string|null $character + * @return LevelUp + */ + public function setCharacter(?string $character): LevelUp + { + $this->character = $character; + return $this; + } +} diff --git a/src/Events/Login.php b/src/Events/Login.php new file mode 100644 index 0000000..573a41d --- /dev/null +++ b/src/Events/Login.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Login implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $method = null; + + public function getName(): string + { + return 'login'; + } + + /** + * @param string|null $method + * @return Login + */ + public function setMethod(?string $method): Login + { + $this->method = $method; + return $this; + } +} diff --git a/src/Events/PostScore.php b/src/Events/PostScore.php new file mode 100644 index 0000000..3b3e8da --- /dev/null +++ b/src/Events/PostScore.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class PostScore implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private float $score; + private ?float $level = null; + private ?string $character = null; + + /** + * @param float $score + */ + public function __construct(float $score) + { + $this->score = $score; + } + + public function getName(): string + { + return 'post_score'; + } + + /** + * @param float|null $level + * @return PostScore + */ + public function setLevel(?float $level): PostScore + { + $this->level = $level; + return $this; + } + + /** + * @param string|null $character + * @return PostScore + */ + public function setCharacter(?string $character): PostScore + { + $this->character = $character; + return $this; + } +} diff --git a/src/Events/Purchase.php b/src/Events/Purchase.php new file mode 100644 index 0000000..4e611ca --- /dev/null +++ b/src/Events/Purchase.php @@ -0,0 +1,80 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Purchase extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private string $transaction_id; + private ?string $affiliation = null; + private ?string $coupon = null; + private ?float $shipping = null; + private ?float $tax = null; + + /** + * @param string $transaction_id + */ + public function __construct(string $transaction_id) + { + $this->transaction_id = $transaction_id; + } + + public function getName(): string + { + return 'purchase'; + } + + /** + * @param string|null $affiliation + * @return Purchase + */ + public function setAffiliation(?string $affiliation): Purchase + { + $this->affiliation = $affiliation; + return $this; + } + + /** + * @param string|null $coupon + * @return Purchase + */ + public function setCoupon(?string $coupon): Purchase + { + $this->coupon = $coupon; + return $this; + } + + /** + * @param float|null $shipping + * @return Purchase + */ + public function setShipping(?float $shipping): Purchase + { + $this->value = (float) $this->value - (float) $this->shipping; + $this->shipping = $shipping; + $this->value += (float) $this->shipping; + return $this; + } + + /** + * @param float|null $tax + * @return Purchase + */ + public function setTax(?float $tax): Purchase + { + $this->value = (float) $this->value - (float) $this->tax; + $this->tax = $tax; + $this->value += (float) $this->tax; + return $this; + } +} diff --git a/src/Events/Refund.php b/src/Events/Refund.php new file mode 100644 index 0000000..5bbf890 --- /dev/null +++ b/src/Events/Refund.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Refund implements EventInterface +{ + use JsonSerializeTrait; + + private string $transaction_id; + private ?string $affiliation = null; + private ?string $coupon = null; + private ?float $shipping = null; + private ?float $tax = null; + + /** + * @param string $transaction_id + */ + public function __construct(string $transaction_id) + { + $this->transaction_id = $transaction_id; + } + + public function getName(): string + { + return 'refund'; + } + + /** + * @param string|null $affiliation + * @return Refund + */ + public function setAffiliation(?string $affiliation): Refund + { + $this->affiliation = $affiliation; + return $this; + } + + /** + * @param string|null $coupon + * @return Refund + */ + public function setCoupon(?string $coupon): Refund + { + $this->coupon = $coupon; + return $this; + } + + /** + * @param float|null $shipping + * @return Refund + */ + public function setShipping(?float $shipping): Refund + { + $this->shipping = $shipping; + return $this; + } + + /** + * @param float|null $tax + * @return Refund + */ + public function setTax(?float $tax): Refund + { + $this->tax = $tax; + return $this; + } +} diff --git a/src/Events/RemoveFromCart.php b/src/Events/RemoveFromCart.php new file mode 100644 index 0000000..80288ac --- /dev/null +++ b/src/Events/RemoveFromCart.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class RemoveFromCart extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'remove_from_cart'; + } +} diff --git a/src/Events/Search.php b/src/Events/Search.php new file mode 100644 index 0000000..b008297 --- /dev/null +++ b/src/Events/Search.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Search implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private string $search_term; + + /** + * @param string $search_term + */ + public function __construct(string $search_term) + { + $this->search_term = $search_term; + } + + public function getName(): string + { + return 'search'; + } +} diff --git a/src/Events/SelectContent.php b/src/Events/SelectContent.php new file mode 100644 index 0000000..67df082 --- /dev/null +++ b/src/Events/SelectContent.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class SelectContent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $content_type = null; + private ?string $item_id = null; + + public function getName(): string + { + return 'select_content'; + } + + /** + * @param string|null $content_type + * @return SelectContent + */ + public function setContentType(?string $content_type): SelectContent + { + $this->content_type = $content_type; + return $this; + } + + /** + * @param string|null $item_id + * @return SelectContent + */ + public function setItemId(?string $item_id): SelectContent + { + $this->item_id = $item_id; + return $this; + } +} diff --git a/src/Events/SelectItem.php b/src/Events/SelectItem.php new file mode 100644 index 0000000..5048716 --- /dev/null +++ b/src/Events/SelectItem.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class SelectItem extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $item_list_id = null; + private ?string $item_list_name = null; + + public function getName(): string + { + return 'select_item'; + } + + /** + * @param string|null $item_list_id + * @return SelectItem + */ + public function setItemListId(?string $item_list_id): SelectItem + { + $this->item_list_id = $item_list_id; + return $this; + } + + /** + * @param string|null $item_list_name + * @return SelectItem + */ + public function setItemListName(?string $item_list_name): SelectItem + { + $this->item_list_name = $item_list_name; + return $this; + } + + /** + * {@inheritDoc} + */ + protected function calculate(ItemInterface $item): void{} +} diff --git a/src/Events/SelectPromotion.php b/src/Events/SelectPromotion.php new file mode 100644 index 0000000..e289c9f --- /dev/null +++ b/src/Events/SelectPromotion.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\PromotionItem; + +class SelectPromotion extends ItemsContainerEvent implements EventInterface +{ + use JsonSerializeTrait; + + private ?string $creative_name = null; + private ?string $creative_slot = null; + private ?string $location_id = null; + private ?string $promotion_id = null; + private ?string $promotion_name = null; + + public function getName(): string + { + return 'select_promotion'; + } + + /** + * @param string|null $creative_name + * @return SelectPromotion + */ + public function setCreativeName(?string $creative_name): SelectPromotion + { + $this->creative_name = $creative_name; + return $this; + } + + /** + * @param string|null $creative_slot + * @return SelectPromotion + */ + public function setCreativeSlot(?string $creative_slot): SelectPromotion + { + $this->creative_slot = $creative_slot; + return $this; + } + + /** + * @param string|null $location_id + * @return SelectPromotion + */ + public function setLocationId(?string $location_id): SelectPromotion + { + $this->location_id = $location_id; + return $this; + } + + /** + * @param string|null $promotion_id + * @return SelectPromotion + */ + public function setPromotionId(?string $promotion_id): SelectPromotion + { + $this->promotion_id = $promotion_id; + return $this; + } + + /** + * @param string|null $promotion_name + * @return SelectPromotion + */ + public function setPromotionName(?string $promotion_name): SelectPromotion + { + $this->promotion_name = $promotion_name; + return $this; + } + + /** + * @param PromotionItem|Item|ItemInterface $item + */ + protected function calculate(ItemInterface $item): void{} +} diff --git a/src/Events/Share.php b/src/Events/Share.php new file mode 100644 index 0000000..ec918f0 --- /dev/null +++ b/src/Events/Share.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Share implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $method = null; + private ?string $content_type = null; + private ?string $item_id = null; + + public function getName(): string + { + return 'share'; + } + + /** + * @param string|null $method + * @return Share + */ + public function setMethod(?string $method): Share + { + $this->method = $method; + return $this; + } + + /** + * @param string|null $content_type + * @return Share + */ + public function setContentType(?string $content_type): Share + { + $this->content_type = $content_type; + return $this; + } + + /** + * @param string|null $item_id + * @return Share + */ + public function setItemId(?string $item_id): Share + { + $this->item_id = $item_id; + return $this; + } +} diff --git a/src/Events/Signup.php b/src/Events/Signup.php new file mode 100644 index 0000000..e1a4740 --- /dev/null +++ b/src/Events/Signup.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Signup implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $method = null; + + public function getName(): string + { + return 'sign_up'; + } + + /** + * @param string|null $method + * @return Signup + */ + public function setMethod(?string $method): Signup + { + $this->method = $method; + return $this; + } +} diff --git a/src/Events/SpendVirtualCurrency.php b/src/Events/SpendVirtualCurrency.php new file mode 100644 index 0000000..47a0ee6 --- /dev/null +++ b/src/Events/SpendVirtualCurrency.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class SpendVirtualCurrency implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private string $virtual_currency_name; + private float $value; + private ?string $item_name = null; + + /** + * @param string $virtual_currency_name + * @param float $value + */ + public function __construct(string $virtual_currency_name, float $value) + { + $this->virtual_currency_name = $virtual_currency_name; + $this->value = $value; + } + + public function getName(): string + { + return 'spend_virtual_currency'; + } + + /** + * @param string $item_name + * @return SpendVirtualCurrency + */ + public function setItemName(string $item_name): SpendVirtualCurrency + { + $this->item_name = $item_name; + return $this; + } + + +} diff --git a/src/Events/TutorialBegin.php b/src/Events/TutorialBegin.php new file mode 100644 index 0000000..64ff016 --- /dev/null +++ b/src/Events/TutorialBegin.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class TutorialBegin implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'tutorial_begin'; + } +} diff --git a/src/Events/TutorialComplete.php b/src/Events/TutorialComplete.php new file mode 100644 index 0000000..f338a26 --- /dev/null +++ b/src/Events/TutorialComplete.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class TutorialComplete implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'tutorial_complete'; + } +} diff --git a/src/Events/UnlockAchievement.php b/src/Events/UnlockAchievement.php new file mode 100644 index 0000000..95d5fa3 --- /dev/null +++ b/src/Events/UnlockAchievement.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class UnlockAchievement implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private string $achievement_id; + + /** + * @param string $achievement_id + */ + public function __construct(string $achievement_id) + { + $this->achievement_id = $achievement_id; + } + + public function getName(): string + { + return 'unlock_achievement'; + } +} diff --git a/src/Events/ViewCart.php b/src/Events/ViewCart.php new file mode 100644 index 0000000..86f1378 --- /dev/null +++ b/src/Events/ViewCart.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class ViewCart extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'view_cart'; + } +} diff --git a/src/Events/ViewItem.php b/src/Events/ViewItem.php new file mode 100644 index 0000000..0cd816c --- /dev/null +++ b/src/Events/ViewItem.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class ViewItem extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + public function getName(): string + { + return 'view_item'; + } +} diff --git a/src/Events/ViewItemList.php b/src/Events/ViewItemList.php new file mode 100644 index 0000000..541c94c --- /dev/null +++ b/src/Events/ViewItemList.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class ViewItemList extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $item_list_id = null; + private ?string $item_list_name = null; + + public function getName(): string + { + return 'view_item_list'; + } + + /** + * @param null|string $item_list_id + * @return ViewItemList + */ + public function setItemListId(?string $item_list_id) + { + $this->item_list_id = $item_list_id; + return $this; + } + + /** + * @param null|string $item_list_name + * @return ViewItemList + */ + public function setItemListName(?string $item_list_name) + { + $this->item_list_name = $item_list_name; + return $this; + } + + /** + * {@inheritDoc} + */ + protected function calculate(ItemInterface $item): void{} +} diff --git a/src/Events/ViewPromotion.php b/src/Events/ViewPromotion.php new file mode 100644 index 0000000..280f29d --- /dev/null +++ b/src/Events/ViewPromotion.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\PromotionItem; + +class ViewPromotion extends ItemsContainerEvent implements \JsonSerializable, EventInterface +{ + use JsonSerializeTrait; + + private ?string $creative_name = null; + private ?string $creative_slot = null; + private ?string $location_id = null; + private ?string $promotion_id = null; + private ?string $promotion_name = null; + + public function getName(): string + { + return 'view_promotion'; + } + + /** + * @param string $creative_name + * @return ViewPromotion + */ + public function setCreativeName(string $creative_name): ViewPromotion + { + $this->creative_name = $creative_name; + return $this; + } + + /** + * @param string $creative_slot + * @return ViewPromotion + */ + public function setCreativeSlot(string $creative_slot): ViewPromotion + { + $this->creative_slot = $creative_slot; + return $this; + } + + /** + * @param string $location_id + * @return ViewPromotion + */ + public function setLocationId(string $location_id): ViewPromotion + { + $this->location_id = $location_id; + return $this; + } + + /** + * @param string $promotion_id + * @return ViewPromotion + */ + public function setPromotionId(string $promotion_id): ViewPromotion + { + $this->promotion_id = $promotion_id; + return $this; + } + + /** + * @param string $promotion_name + * @return ViewPromotion + */ + public function setPromotionName(string $promotion_name): ViewPromotion + { + $this->promotion_name = $promotion_name; + return $this; + } + + /** + * @param PromotionItem|ItemInterface $item + */ + protected function calculate(ItemInterface $item): void{} +} diff --git a/src/Exception/CurrencyMismatchException.php b/src/Exception/CurrencyMismatchException.php new file mode 100644 index 0000000..cdb979d --- /dev/null +++ b/src/Exception/CurrencyMismatchException.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Exception; + +class CurrencyMismatchException extends \Exception +{ + +} diff --git a/src/Item/Item.php b/src/Item/Item.php new file mode 100644 index 0000000..6f6d394 --- /dev/null +++ b/src/Item/Item.php @@ -0,0 +1,165 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Item; + +use Spinbits\GoogleAnalytics4EventsDtoS\JsonSerializeTrait; + +class Item implements \JsonSerializable, ItemInterface +{ + use JsonSerializeTrait; + + private string $item_id; + private string $item_name; + private ?string $affiliation; + private ?string $coupon; + private string $currency; + private ?float $discount; + private ?int $index; + private ?string $item_brand; + private ?string $item_category = null; + private ?string $item_category2 = null; + private ?string $item_category3 = null; + private ?string $item_category4 = null; + private ?string $item_category5 = null; + private ?string $item_list_id; + private ?string $item_list_name; + private ?string $item_variant; + private ?string $location_id; + private ?float $price; + private int $quantity; + + public function __construct( + string $item_id, + string $item_name, + float $price, + string $currency, + float $discount = 0, + int $quantity = 1, + ?string $affiliation = null, + ?string $coupon = null, + ?int $index = null, + ?string $item_brand = null, + ?array $item_category = null, + ?string $item_list_id = null, + ?string $item_list_name = null, + ?string $item_variant = null, + ?string $location_id = null + ) { + $this->item_id = $item_id; + $this->item_name = $item_name; + $this->affiliation = $affiliation; + $this->coupon = $coupon; + $this->currency = $currency; + $this->discount = $discount; + $this->index = $index; + $this->item_brand = $item_brand; + $this->item_list_id = $item_list_id; + $this->item_list_name = $item_list_name; + $this->item_variant = $item_variant; + $this->location_id = $location_id; + $this->price = $price; + $this->quantity = $quantity; + + if (is_array($item_category)) { + $this->item_category = (count($item_category)>0) ? (string) array_shift($item_category) : null; + $this->item_category2 = (count($item_category)>0) ? (string) array_shift($item_category) : null; + $this->item_category3 = (count($item_category)>0) ? (string) array_shift($item_category) : null; + $this->item_category4 = (count($item_category)>0) ? (string) array_shift($item_category) : null; + $this->item_category5 = (count($item_category)>0) ? (string) array_shift($item_category) : null; + } + } + + public function getValue(): float + { + return ((float) $this->price - (float) $this->discount) * $this->quantity; + } + + public function getPrice(): ?float + { + return $this->price; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function getCurrency(): string + { + return $this->currency; + } + + public function setAffiliation(?string $affiliation): Item + { + $this->affiliation = $affiliation; + return $this; + } + + public function setCoupon(?string $coupon): Item + { + $this->coupon = $coupon; + return $this; + } + + public function setDiscount(?float $discount): Item + { + $this->discount = $discount; + return $this; + } + + public function setIndex(?int $index): Item + { + $this->index = $index; + return $this; + } + + public function setItemBrand(?string $item_brand): Item + { + $this->item_brand = $item_brand; + return $this; + } + + public function setItemListId(?string $item_list_id): Item + { + $this->item_list_id = $item_list_id; + return $this; + } + + public function setItemListName(?string $item_list_name): Item + { + $this->item_list_name = $item_list_name; + return $this; + } + + public function setItemVariant(?string $item_variant): Item + { + $this->item_variant = $item_variant; + return $this; + } + + public function setLocationId(?string $location_id): Item + { + $this->location_id = $location_id; + return $this; + } + + public function setPrice(?float $price): Item + { + $this->price = $price; + return $this; + } + + public function setQuantity(int $quantity): Item + { + $this->quantity = $quantity; + return $this; + } +} diff --git a/src/Item/ItemInterface.php b/src/Item/ItemInterface.php new file mode 100644 index 0000000..9264411 --- /dev/null +++ b/src/Item/ItemInterface.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Item; + +interface ItemInterface{ + + public function getCurrency(): string; + public function getValue(): float; +} diff --git a/src/Item/PromotionItem.php b/src/Item/PromotionItem.php new file mode 100644 index 0000000..3e2d73e --- /dev/null +++ b/src/Item/PromotionItem.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS\Item; + +class PromotionItem extends Item +{ + private ?string $creative_name = null; + private ?string $creative_slot = null; + private ?string $promotion_id = null; + private ?string $promotion_name = null; + + public function setCreativeName(?string $creative_name): PromotionItem + { + $this->creative_name = $creative_name; + return $this; + } + + public function setCreativeSlot(?string $creative_slot): PromotionItem + { + $this->creative_slot = $creative_slot; + return $this; + } + + public function setPromotionId(?string $promotion_id): PromotionItem + { + $this->promotion_id = $promotion_id; + return $this; + } + + public function setPromotionName(?string $promotion_name): PromotionItem + { + $this->promotion_name = $promotion_name; + return $this; + } +} diff --git a/src/JsonSerializeTrait.php b/src/JsonSerializeTrait.php new file mode 100644 index 0000000..6424d8a --- /dev/null +++ b/src/JsonSerializeTrait.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Spinbits\GoogleAnalytics4EventsDtoS; + +trait JsonSerializeTrait +{ + public function __toString() + { + return (string) json_encode($this); + } + + /** + * @psalm-suppress MixedAssignment + */ + public function jsonSerialize(): array + { + $result = []; + /** @var mixed $value */ + foreach (get_object_vars($this) as $key => $value) { + if ($value instanceof \JsonSerializable) { + $result[$key] = $value->jsonSerialize(); + } elseif (null !== $value) { + $result[$key] = $value; + } + } + + return $result; + } +} diff --git a/tests/Unit/Events/AddPaymentInfoTest.php b/tests/Unit/Events/AddPaymentInfoTest.php new file mode 100644 index 0000000..43cada5 --- /dev/null +++ b/tests/Unit/Events/AddPaymentInfoTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\AddPaymentInfo; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class AddPaymentInfoTest extends TestCase +{ + /** @var AddPaymentInfo */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new AddPaymentInfo(); + } + + public function testGetName() + { + $this->assertSame('add_payment_info', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $this->sut->setCoupon('some_coupon'); + $this->sut->setPaymentType('pay_type'); + + $result = json_encode($this->sut); + + $expected = '{"coupon":"some_coupon","payment_type":"pay_type","currency":"USD","value":2,"items":[{},{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/Events/AddShippingInfoTest.php b/tests/Unit/Events/AddShippingInfoTest.php new file mode 100644 index 0000000..da0585a --- /dev/null +++ b/tests/Unit/Events/AddShippingInfoTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\AddShippingInfo; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class AddShippingInfoTest extends TestCase +{ + /** @var AddShippingInfo */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new AddShippingInfo(); + } + + public function testGetName() + { + $this->assertSame('add_shipping_info', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $this->sut->setCoupon('some_coupon'); + $this->sut->setShippingTier('tier'); + + $result = json_encode($this->sut); + + $expected = '{"coupon":"some_coupon","shipping_tier":"tier","currency":"USD","value":2,"items":[{},{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/Events/AddToCartTest.php b/tests/Unit/Events/AddToCartTest.php new file mode 100644 index 0000000..bb2463f --- /dev/null +++ b/tests/Unit/Events/AddToCartTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\AddToCart; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class AddToCartTest extends TestCase +{ + /** @var AddToCart */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new AddToCart(); + } + + public function testGetName() + { + $this->assertSame('add_to_cart', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = json_encode($this->sut); + + $expected = '{"currency":"USD","value":2,"items":[{},{}]}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/AddToWishListTest.php b/tests/Unit/Events/AddToWishListTest.php new file mode 100644 index 0000000..f45e87e --- /dev/null +++ b/tests/Unit/Events/AddToWishListTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\AddToWishlist; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class AddToWishListTest extends TestCase +{ + /** @var AddToWishlist */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new AddToWishlist(); + } + + public function testGetName() + { + $this->assertSame('add_to_wishlist', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = json_encode($this->sut); + + $expected = '{"currency":"USD","value":2,"items":[{},{}]}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/BeginCheckoutTest.php b/tests/Unit/Events/BeginCheckoutTest.php new file mode 100644 index 0000000..cde1c6b --- /dev/null +++ b/tests/Unit/Events/BeginCheckoutTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\BeginCheckout; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class BeginCheckoutTest extends TestCase +{ + /** @var BeginCheckout */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new BeginCheckout(); + } + + public function testGetName() + { + $this->assertSame('begin_checkout', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + $this->sut->setCoupon('coupon_name'); + + $result = json_encode($this->sut); + + $expected = '{"coupon":"coupon_name","currency":"USD","value":2,"items":[{},{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/Events/GenerateLeadTest.php b/tests/Unit/Events/GenerateLeadTest.php new file mode 100644 index 0000000..1c3ffec --- /dev/null +++ b/tests/Unit/Events/GenerateLeadTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\GenerateLead; +use PHPUnit\Framework\TestCase; + +class GenerateLeadTest extends TestCase +{ + /** @var GenerateLead */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new GenerateLead('USD', 1.23); + } + + public function testGetName() + { + $this->assertSame('generate_lead', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $result = json_encode($this->sut); + + $expected = '{"currency":"USD","value":1.23}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/JoinGroupTest.php b/tests/Unit/Events/JoinGroupTest.php new file mode 100644 index 0000000..b0993f3 --- /dev/null +++ b/tests/Unit/Events/JoinGroupTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\JoinGroup; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class JoinGroupTest extends TestCase +{ + /** @var JoinGroup */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new JoinGroup(); + } + + public function testGetName() + { + $this->assertSame('join_group', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $this->sut->setGroupId('example'); + + $result = json_encode($this->sut); + + $expected = '{"group_id":"example"}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/LoginTest.php b/tests/Unit/Events/LoginTest.php new file mode 100644 index 0000000..7ade18e --- /dev/null +++ b/tests/Unit/Events/LoginTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\Login; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class LoginTest extends TestCase +{ + /** @var Login */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new Login(); + } + + public function testGetName() + { + $this->assertSame('login', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $this->sut->setMethod('example'); + + $result = json_encode($this->sut); + + $expected = '{"method":"example"}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/PurchaseTest.php b/tests/Unit/Events/PurchaseTest.php new file mode 100644 index 0000000..62db68a --- /dev/null +++ b/tests/Unit/Events/PurchaseTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\Purchase; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class PurchaseTest extends TestCase +{ + /** @var Purchase */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new Purchase('some_unique_trans_id'); + } + + public function testGetName() + { + $this->assertSame('purchase', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = json_encode($this->sut); + + $expected = '{"transaction_id":"some_unique_trans_id","currency":"USD","value":2,"items":[{},{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/Events/RemoveFromCartTest.php b/tests/Unit/Events/RemoveFromCartTest.php new file mode 100644 index 0000000..cab71ad --- /dev/null +++ b/tests/Unit/Events/RemoveFromCartTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\RemoveFromCart; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class RemoveFromCartTest extends TestCase +{ + /** @var RemoveFromCart */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new RemoveFromCart(); + } + + public function testGetName() + { + $this->assertSame('remove_from_cart', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = json_encode($this->sut); + + $expected = '{"currency":"USD","value":2,"items":[{},{}]}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/SearchTest.php b/tests/Unit/Events/SearchTest.php new file mode 100644 index 0000000..02b848d --- /dev/null +++ b/tests/Unit/Events/SearchTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\Search; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class SearchTest extends TestCase +{ + /** @var Search */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new Search('example_term'); + } + + public function testGetName() + { + $this->assertSame('search', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $result = json_encode($this->sut); + + $expected = '{"search_term":"example_term"}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/SelectItemTest.php b/tests/Unit/Events/SelectItemTest.php new file mode 100644 index 0000000..288a557 --- /dev/null +++ b/tests/Unit/Events/SelectItemTest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\SelectItem; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class SelectItemTest extends TestCase +{ + /** @var SelectItem */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new SelectItem(); + } + + public function testGetName() + { + $this->assertSame('select_item', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $this->sut->addItem($item); + $this->sut->setItemListId('list_id'); + $this->sut->setItemListName('list_name'); + + $result = json_encode($this->sut); + + $expected = '{"item_list_id":"list_id","item_list_name":"list_name","items":[{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/Events/SignupTest.php b/tests/Unit/Events/SignupTest.php new file mode 100644 index 0000000..bc8893d --- /dev/null +++ b/tests/Unit/Events/SignupTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\Signup; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class SignupTest extends TestCase +{ + /** @var Signup */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new Signup(); + } + + public function testGetName() + { + $this->assertSame('sign_up', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $this->sut->setMethod('example'); + + $result = json_encode($this->sut); + + $expected = '{"method":"example"}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/ViewCartTest.php b/tests/Unit/Events/ViewCartTest.php new file mode 100644 index 0000000..8444c82 --- /dev/null +++ b/tests/Unit/Events/ViewCartTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewCart; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class ViewCartTest extends TestCase +{ + /** @var ViewCart */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new ViewCart(); + } + + public function testGetName() + { + $this->assertSame('view_cart', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = json_encode($this->sut); + + $expected = '{"currency":"USD","value":2,"items":[{},{}]}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/ViewItemListTest.php b/tests/Unit/Events/ViewItemListTest.php new file mode 100644 index 0000000..3ac4ab3 --- /dev/null +++ b/tests/Unit/Events/ViewItemListTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewItemList; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class ViewItemListTest extends TestCase +{ + /** @var ViewItemList */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new ViewItemList(); + } + + public function testGetName() + { + $this->assertSame('view_item_list', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $this->sut->setItemListId('list_id'); + $this->sut->setItemListName('list_name'); + + $result = json_encode($this->sut); + + $expected = '{"item_list_id":"list_id","item_list_name":"list_name","items":[{},{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/Events/ViewItemTest.php b/tests/Unit/Events/ViewItemTest.php new file mode 100644 index 0000000..d0e895f --- /dev/null +++ b/tests/Unit/Events/ViewItemTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewItem; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class ViewItemTest extends TestCase +{ + /** @var ViewItem */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new ViewItem(); + } + + public function testGetName() + { + $this->assertSame('view_item', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = json_encode($this->sut); + + $expected = '{"currency":"USD","value":2,"items":[{},{}]}'; + $this->assertSame($expected, $result); + } +} diff --git a/tests/Unit/Events/ViewPromotionTest.php b/tests/Unit/Events/ViewPromotionTest.php new file mode 100644 index 0000000..7d63583 --- /dev/null +++ b/tests/Unit/Events/ViewPromotionTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto\Events; + +use Spinbits\GoogleAnalytics4EventsDtoS\Events\ViewPromotion; +use PHPUnit\Framework\TestCase; +use Spinbits\GoogleAnalytics4EventsDtoS\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Item\ItemInterface; + +class ViewPromotionTest extends TestCase +{ + /** @var ViewPromotion */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new ViewPromotion(); + } + + public function testGetName() + { + $this->assertSame('view_promotion', $this->sut->getName()); + } + + public function testSerializeMockItem() + { + $item=$this->createMock(ItemInterface::class); + + $item->expects($this->any()) + ->method('getCurrency') + ->willReturn('USD'); + + $item->expects($this->any()) + ->method('getValue') + ->willReturn(1.00); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $this->sut->setCreativeName('name'); + $this->sut->setCreativeSlot('slot'); + $this->sut->setLocationId('id'); + $this->sut->setPromotionId('promo_id'); + $this->sut->setPromotionName('promo_name'); + + $result = json_encode($this->sut); + + $expected = '{"creative_name":"name","creative_slot":"slot","location_id":"id","promotion_id":"promo_id","promotion_name":"promo_name","items":[{},{}]}'; + $this->assertEqualsCanonicalizing(json_decode($expected, true), json_decode($result, true)); + } +} diff --git a/tests/Unit/ItemTest.php b/tests/Unit/ItemTest.php new file mode 100644 index 0000000..7f673d5 --- /dev/null +++ b/tests/Unit/ItemTest.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto; + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item; +use PHPUnit\Framework\TestCase; + +class ItemTest extends TestCase +{ + private Item $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new Item( + 'Id', + 'Name', + 5.59, + 'USD', + 0.1, + 2, + 'aff', + 'COUPON_NAME', + 2, + 'Brand name', + ['categoryA', 'categoryB','categoryC','categoryD','categoryE','skipped category'], + 'list_id', + 'list_name', + 'variant', + 'location', + ); + } + + public function testValue() + { + $this->assertSame(10.98, $this->sut->getValue()); + } + + public function testSerialize() + { + $result = json_encode($this->sut); + + $this->assertSame('{"item_id":"Id","item_name":"Name","affiliation":"aff","coupon":"COUPON_NAME","currency":"USD","discount":0.1,"index":2,"item_brand":"Brand name","item_category":"categoryA","item_category2":"categoryB","item_category3":"categoryC","item_category4":"categoryD","item_category5":"categoryE","item_list_id":"list_id","item_list_name":"list_name","item_variant":"variant","location_id":"location","price":5.59,"quantity":2}', $result); + } + + public function testSetters() + { + $this->sut + ->setPrice(3.51) + ->setQuantity(3) + ->setAffiliation('new_aff') + ->setCoupon('c1') + ->setDiscount(0.5) + ->setIndex(3) + ->setItemBrand('newBrand') + ->setItemListId('id') + ->setItemListName('name') + ->setItemVariant('var') + ->setLocationId('loc'); + + $result = json_encode($this->sut); + + $this->assertSame('{"item_id":"Id","item_name":"Name","affiliation":"new_aff","coupon":"c1","currency":"USD","discount":0.5,"index":3,"item_brand":"newBrand","item_category":"categoryA","item_category2":"categoryB","item_category3":"categoryC","item_category4":"categoryD","item_category5":"categoryE","item_list_id":"id","item_list_name":"name","item_variant":"var","location_id":"loc","price":3.51,"quantity":3}', $result); + } +} diff --git a/tests/Unit/ItemsContainerEventTest.php b/tests/Unit/ItemsContainerEventTest.php new file mode 100644 index 0000000..b285e7c --- /dev/null +++ b/tests/Unit/ItemsContainerEventTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Tests\Spinbits\SyliusGoogleAnalytics4Plugin\Unit\Dto; + +use Spinbits\GoogleAnalytics4EventsDtoS\Item\Item; +use Spinbits\GoogleAnalytics4EventsDtoS\Events\ItemsContainerEvent; +use PHPUnit\Framework\TestCase; + +class ItemsContainerEventTest extends TestCase +{ + /** @var ItemsContainerEvent */ + private $sut; + + protected function setUp(): void + { + parent::setUp(); + $this->sut = new ItemsContainerEvent(); + } + + /** @test */ + public function testAddItem() + { + $item = new Item('id', 'name', 2.50, 'USD'); + $item->setDiscount(0.7); + + $this->sut->addItem($item); + $this->sut->addItem($item); + + $result = $this->sut->jsonSerialize(); + + $this->assertSame('USD', $result['currency']); + $this->assertSame(3.6, (float) $result['value']); + } +}