From 3890ecf1641f4f8070cdceade3128f36be28d35b Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Fri, 16 Feb 2024 23:42:27 +0100 Subject: [PATCH 01/13] Fix DDEV db port --- .ddev/config.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.ddev/config.yaml b/.ddev/config.yaml index 1c981df..b3f7a89 100644 --- a/.ddev/config.yaml +++ b/.ddev/config.yaml @@ -7,8 +7,9 @@ xdebug_enabled: false additional_hostnames: [] additional_fqdns: [] database: - type: mariadb - version: "10.4" + type: mariadb + version: "10.4" +host_db_port: "32770" use_dns_when_possible: true composer_version: "2" web_environment: [] From 119962d865cfbb024222169e82bb392b584dd0a5 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Fri, 16 Feb 2024 23:50:13 +0100 Subject: [PATCH 02/13] Fix style --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index f042a5b..eec6679 100644 --- a/.env.example +++ b/.env.example @@ -34,7 +34,7 @@ MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null -MAIL_FROM_ADDRESS="hello@example.com" +MAIL_FROM_ADDRESS=hello@example.com MAIL_FROM_NAME="${APP_NAME}" AWS_ACCESS_KEY_ID= From ac994d347217afbb5ed7e9240fee68204e1d03d9 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Fri, 23 Feb 2024 22:15:40 +0100 Subject: [PATCH 03/13] Fix Pest configuration --- tests/Pest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Pest.php b/tests/Pest.php index f239f3c..fac5a26 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -14,6 +14,12 @@ use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Tests\TestCase; +uses( + TestCase::class, + LazilyRefreshDatabase::class +) + ->in('Unit'); + uses( TestCase::class, LazilyRefreshDatabase::class From 2df95ca7fb5867391c0691037bb3829e5187aba7 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Fri, 23 Feb 2024 22:43:27 +0100 Subject: [PATCH 04/13] Fix `.env.testing` --- .env.testing | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.testing b/.env.testing index 7b15912..2185f50 100644 --- a/.env.testing +++ b/.env.testing @@ -7,7 +7,7 @@ APP_URL="https://laravel.ddev.site" DB_CONNECTION="mysql" DB_HOST="db" DB_PORT="3306" -DB_DATABASE="db_test" +DB_DATABASE="test" DB_USERNAME="db" DB_PASSWORD="db" From a88b72064fb8b036245843387100a5683989a6bc Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Fri, 23 Feb 2024 22:44:29 +0100 Subject: [PATCH 05/13] Add `Product` model with factory, migration and test --- app/Models/Product.php | 28 +++++++++++++ database/factories/ProductFactory.php | 38 +++++++++++++++++ ...024_02_23_210714_create_products_table.php | 29 +++++++++++++ tests/Feature/Models/ProductTest.php | 42 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 app/Models/Product.php create mode 100644 database/factories/ProductFactory.php create mode 100644 database/migrations/2024_02_23_210714_create_products_table.php create mode 100644 tests/Feature/Models/ProductTest.php diff --git a/app/Models/Product.php b/app/Models/Product.php new file mode 100644 index 0000000..11f9fcc --- /dev/null +++ b/app/Models/Product.php @@ -0,0 +1,28 @@ + 'float', + ]; + + /** + * {@inheritdoc} + */ + protected $guarded = [ + 'id', + ]; +} diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php new file mode 100644 index 0000000..8e40f2d --- /dev/null +++ b/database/factories/ProductFactory.php @@ -0,0 +1,38 @@ + + */ +class ProductFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = Product::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => fake()->word(), + + 'price' => fake()->randomFloat( + nbMaxDecimals: 2, + max: 100 + ), + ]; + } +} diff --git a/database/migrations/2024_02_23_210714_create_products_table.php b/database/migrations/2024_02_23_210714_create_products_table.php new file mode 100644 index 0000000..e424e3c --- /dev/null +++ b/database/migrations/2024_02_23_210714_create_products_table.php @@ -0,0 +1,29 @@ +id(); + $table->string('name'); + $table->decimal('price', 5, 2, true); + $table->timestamps(); + }); + } +}; diff --git a/tests/Feature/Models/ProductTest.php b/tests/Feature/Models/ProductTest.php new file mode 100644 index 0000000..38f7d24 --- /dev/null +++ b/tests/Feature/Models/ProductTest.php @@ -0,0 +1,42 @@ + $products */ + $products = Product::factory() + ->count(MODELS_COUNT) + ->create(); + + expect(Product::count()) + ->toBe(MODELS_COUNT); + + /** @var Product $product */ + foreach ($products as $product) { + $product->refresh(); + + expect($product->id) + ->not() + ->toBeNull() + ->toBeInt(); + + expect($product->name) + ->not() + ->toBeNull() + ->toBeString(); + + expect($product->price) + ->not() + ->toBeNull() + ->toBeFloat() + ->toBeGreaterThanOrEqual(0); + } +}); From f790413530ef6df1d1531009a127ec5f3d652124 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 01:49:40 +0100 Subject: [PATCH 06/13] Install `cknow/laravel-money` --- composer.json | 1 + composer.lock | 158 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 158 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 62bd646..608fd79 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require": { "php": "^8.3", "ext-readline": "*", + "cknow/laravel-money": "^7.2", "guzzlehttp/guzzle": "^7.2", "laravel/framework": "^10.10", "laravel/sanctum": "^3.3", diff --git a/composer.lock b/composer.lock index 7024ff9..7af9b25 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d2310ecc05bb4590c2571f370f0a53e8", + "content-hash": "4645c95664ba4e3b75f11b4b5654220b", "packages": [ { "name": "brick/math", @@ -130,6 +130,74 @@ ], "time": "2023-12-11T17:09:12+00:00" }, + { + "name": "cknow/laravel-money", + "version": "v7.2.0", + "source": { + "type": "git", + "url": "https://github.com/cknow/laravel-money.git", + "reference": "d54ebc59342ed2a68647cc878444ef48af9f21ef" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cknow/laravel-money/zipball/d54ebc59342ed2a68647cc878444ef48af9f21ef", + "reference": "d54ebc59342ed2a68647cc878444ef48af9f21ef", + "shasum": "" + }, + "require": { + "ext-intl": "*", + "ext-json": "*", + "illuminate/support": "^7.0|^8.0|^9.0|^10.0", + "illuminate/view": "^7.0|^8.0|^9.0|^10.0", + "moneyphp/money": "^3.3|^4.2", + "php": "^7.3|^8.0" + }, + "require-dev": { + "graham-campbell/testbench": "^5.7", + "illuminate/filesystem": "^7.0|^8.0|^9.0|^10.0", + "mockery/mockery": "^1.6", + "phpunit/phpunit": "^8.5|^9.5.10|^10.0", + "spatie/phpunit-watcher": "^1.23" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Cknow\\Money\\MoneyServiceProvider" + ] + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Cknow\\Money\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ricardo Gobbo de Souza", + "email": "ricardogobbosouza@yahoo.com.br" + } + ], + "description": "Laravel Money", + "homepage": "https://github.com/cknow/laravel-money", + "keywords": [ + "currency", + "laravel", + "money" + ], + "support": { + "issues": "https://github.com/cknow/laravel-money/issues", + "source": "https://github.com/cknow/laravel-money/tree/v7.2.0" + }, + "time": "2023-08-18T11:13:55+00:00" + }, { "name": "dflydev/dot-access-data", "version": "v3.0.2", @@ -1893,6 +1961,94 @@ ], "time": "2023-10-17T14:13:20+00:00" }, + { + "name": "moneyphp/money", + "version": "v4.5.0", + "source": { + "type": "git", + "url": "https://github.com/moneyphp/money.git", + "reference": "a1daa7daf159b4044e3d0c34c41fe2be5860e850" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/moneyphp/money/zipball/a1daa7daf159b4044e3d0c34c41fe2be5860e850", + "reference": "a1daa7daf159b4044e3d0c34c41fe2be5860e850", + "shasum": "" + }, + "require": { + "ext-bcmath": "*", + "ext-filter": "*", + "ext-json": "*", + "php": "~8.1.0 || ~8.2.0 || ~8.3.0" + }, + "require-dev": { + "cache/taggable-cache": "^1.1.0", + "doctrine/coding-standard": "^12.0", + "doctrine/instantiator": "^1.5.0 || ^2.0", + "ext-gmp": "*", + "ext-intl": "*", + "florianv/exchanger": "^2.8.1", + "florianv/swap": "^4.3.0", + "moneyphp/crypto-currencies": "^1.1.0", + "moneyphp/iso-currencies": "^3.4", + "php-http/message": "^1.16.0", + "php-http/mock-client": "^1.6.0", + "phpbench/phpbench": "^1.2.5", + "phpunit/phpunit": "^10.5.9", + "psalm/plugin-phpunit": "^0.18.4", + "psr/cache": "^1.0.1 || ^2.0 || ^3.0", + "vimeo/psalm": "~5.20.0" + }, + "suggest": { + "ext-gmp": "Calculate without integer limits", + "ext-intl": "Format Money objects with intl", + "florianv/exchanger": "Exchange rates library for PHP", + "florianv/swap": "Exchange rates library for PHP", + "psr/cache-implementation": "Used for Currency caching" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Money\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mathias Verraes", + "email": "mathias@verraes.net", + "homepage": "http://verraes.net" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com" + }, + { + "name": "Frederik Bosch", + "email": "f.bosch@genkgo.nl" + } + ], + "description": "PHP implementation of Fowler's Money pattern", + "homepage": "http://moneyphp.org", + "keywords": [ + "Value Object", + "money", + "vo" + ], + "support": { + "issues": "https://github.com/moneyphp/money/issues", + "source": "https://github.com/moneyphp/money/tree/v4.5.0" + }, + "time": "2024-02-15T19:47:21+00:00" + }, { "name": "monolog/monolog", "version": "3.5.0", From 48f5f8567611cfb0c6be88b33d8500c3d065031c Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 01:58:13 +0100 Subject: [PATCH 07/13] Cast products price to `Money` class --- app/Models/Product.php | 3 ++- tests/Feature/Models/ProductTest.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Models/Product.php b/app/Models/Product.php index 11f9fcc..f994219 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -2,6 +2,7 @@ namespace App\Models; +use Cknow\Money\Casts\MoneyDecimalCast; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -16,7 +17,7 @@ final class Product extends Model * {@inheritdoc} */ protected $casts = [ - 'price' => 'float', + 'price' => MoneyDecimalCast::class . ':EUR,true', ]; /** diff --git a/tests/Feature/Models/ProductTest.php b/tests/Feature/Models/ProductTest.php index 38f7d24..a74c292 100644 --- a/tests/Feature/Models/ProductTest.php +++ b/tests/Feature/Models/ProductTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Models; use App\Models\Product; +use Cknow\Money\Money; use Illuminate\Database\Eloquent\Collection; /** @@ -36,7 +37,6 @@ expect($product->price) ->not() ->toBeNull() - ->toBeFloat() - ->toBeGreaterThanOrEqual(0); + ->toBeInstanceOf(Money::class); } }); From b8aeae9e2013e11d657d8be1dbba438170ec45a7 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 01:58:22 +0100 Subject: [PATCH 08/13] Fix style --- database/factories/UserFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 584104c..c4ceb07 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,12 +2,13 @@ namespace Database\Factories; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> + * @extends Factory */ class UserFactory extends Factory { From a10d8e01bd9c3d77fb864a9b91af01804ad731da Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 02:03:47 +0100 Subject: [PATCH 09/13] Fix `price` attribute generation in `ProductFactory` --- database/factories/ProductFactory.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index 8e40f2d..935839b 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\Product; +use Cknow\Money\Money; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -29,9 +30,11 @@ public function definition(): array return [ 'name' => fake()->word(), - 'price' => fake()->randomFloat( - nbMaxDecimals: 2, - max: 100 + 'price' => Money::EUR( + fake()->randomFloat( + nbMaxDecimals: 2, + max: 100 + ) ), ]; } From 45c7afbed0b0374fa925dfa4ede48a7e727b5274 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 02:32:52 +0100 Subject: [PATCH 10/13] Fix repository URLs and name in `composer.json` --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 608fd79..3d90f2a 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,10 @@ { - "name": "mloru/root-price-list", + "name": "rootclub/price-list", "type": "project", "description": "Simple project to display and manage the Root Club price list.", "support": { - "issues": "https://github.com/mloru/root-price-list/issues", - "source": "https://github.com/mloru/root-price-list" + "issues": "https://github.com/rootclub/price-list/issues", + "source": "https://github.com/rootclub/price-list" }, "keywords": [ "laravel", From 91282a3ca01fde5a429c1ee3c84bcc026d04ae76 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 02:36:58 +0100 Subject: [PATCH 11/13] Fix `PULL_REQUEST_TEMPLATE.md` --- .github/PULL_REQUEST_TEMPLATE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index f64061b..f32ed2d 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -2,7 +2,7 @@ **What is the new behavior?** -Fix [XXX-nnnn](https://mloru.atlassian.net/browse/XXX-nnnn) + **Does this PR introduce a breaking change?** No From 2b24be38618acbc6f3367ee267b948c779e95495 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Sat, 24 Feb 2024 03:09:51 +0100 Subject: [PATCH 12/13] Fix `README.md` --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 56e9791..1e3bd8c 100644 --- a/README.md +++ b/README.md @@ -26,36 +26,36 @@ ddev start ddev composer install ``` -### Migrazione database +### Database migration The project uses two database: * For `local` environment: `db` -* For `testing` environment: `db_test` +* For `testing` environment: `test` -Migrations on `db_test` are not required since `LazilyRefreshDatabase` trait is used. +Migrations on `test` are not required since the `LazilyRefreshDatabase` trait is used. See [Resetting The Database After Each Test](https://laravel.com/docs/5.7/database-testing#resetting-the-database-after-each-test) for more information. ```shell -ddev exec php artisan migrate +ddev artisan migrate ``` ### Run seeders ```shell -ddev exec php artisan db:seed +ddev artisan db:seed ``` To set up a fresh version of database and run seeders at the same time: ```shell -ddev exec php artisan migrate:fresh --seed +ddev artisan migrate:fresh --seed ``` ## Styles and static analyzers -Run checks prior to commit +Run checks before committing: ```shell ddev exec tools/static-analysis.sh @@ -67,7 +67,7 @@ Fix style in project with PHP CS Fixer: ddev exec tools/fix-style.sh ``` -To run Php Insights with verbose output: +To run PHP Insights with verbose output: ```shell ddev exec ./vendor/bin/phpinsights -v @@ -104,7 +104,7 @@ By defaults, queues are sync: QUEUE_CONNECTION=sync ``` -It's possible to set the async with: +It's possible to set them as async with: ```dotenv QUEUE_CONNECTION=redis @@ -115,12 +115,12 @@ Redis is used as message queues manager. ### Local development ```shell -ddev exec php artisan queue:work +ddev artisan queue:work ``` ### Test -During tests, queues are sync since in `.env.testing` there is: +During tests, queues are sync since `.env.testing` contains: ```dotenv QUEUE_CONNECTION=sync From e64c9ff124fb4335e3cbd8055da7dc3d3bf57eb5 Mon Sep 17 00:00:00 2001 From: Mirko Lorusso Date: Fri, 15 Mar 2024 23:20:52 +0100 Subject: [PATCH 13/13] Fix PHPDoc --- tests/Feature/Models/ProductTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Models/ProductTest.php b/tests/Feature/Models/ProductTest.php index a74c292..7855ba6 100644 --- a/tests/Feature/Models/ProductTest.php +++ b/tests/Feature/Models/ProductTest.php @@ -12,7 +12,7 @@ const MODELS_COUNT = 3; test('it can be persisted', function () { - /** @var Collection $products */ + /** @var Collection $products */ $products = Product::factory() ->count(MODELS_COUNT) ->create();