From 1f2ea43e8bae905d627d7341ce0997923acf11db Mon Sep 17 00:00:00 2001 From: "Endurance, the Martian" Date: Sat, 21 Oct 2023 11:49:24 +0100 Subject: [PATCH 01/12] added: check if a voucher is valid --- src/Vouchers.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Vouchers.php b/src/Vouchers.php index 8170b52..5c9cc80 100755 --- a/src/Vouchers.php +++ b/src/Vouchers.php @@ -82,6 +82,23 @@ public function check(string $code) return $voucher; } + /** + * @param string $code + * @return bool + */ + public function isValid(string $code): bool + { + try { + $this->check($code); + } catch (VoucherIsInvalid $exception) { + return false; + } catch (VoucherExpired $exception) { + return false; + } + + return true; + } + /** * @return string */ From 27004330f8db89323f87d8adc4665fd5a1e7bab9 Mon Sep 17 00:00:00 2001 From: "Endurance, the Martian" Date: Tue, 21 Nov 2023 01:25:19 +0100 Subject: [PATCH 02/12] added: isValidCode & isValidVoucher method --- src/Vouchers.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Vouchers.php b/src/Vouchers.php index 5c9cc80..5b33428 100755 --- a/src/Vouchers.php +++ b/src/Vouchers.php @@ -86,7 +86,7 @@ public function check(string $code) * @param string $code * @return bool */ - public function isValid(string $code): bool + public function isValidCode(string $code): bool { try { $this->check($code); @@ -99,6 +99,15 @@ public function isValid(string $code): bool return true; } + /** + * @param Voucher $voucher + * @return bool + */ + public function isValidVoucher(Voucher $voucher): bool + { + return $this->isValidCode($voucher->code); + } + /** * @return string */ From 8291cd059409acf3fb793bd78b9078763dbd25be Mon Sep 17 00:00:00 2001 From: "Endurance, the Martian" Date: Tue, 21 Nov 2023 01:26:02 +0100 Subject: [PATCH 03/12] docs: added isValidCode & isValidVoucher usage to readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 9d917b6..bbc8291 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,14 @@ $voucher = $user->redeemCode('ABCD-EFGH'); $videoCourse = $voucher->model; ``` +## Validating Vouchers & Voucher Codes +The `isValidCode` and `isValidVoucher` methods on the `Vouchers` facade allow you to check if a voucher code is valid or if a voucher model is valid. + +```php +Vouchers::isValidCode('ABCD-EFGH'); // true or false +Vouchers::isValidVoucher($voucher); // true or false +``` + ## Handling Errors The `redeemCode` and `redeemVoucher` methods throw a couple of exceptions that you will want to catch and react to in your application: From af073a7f8a2918a29bb927e8305eb66f100b063a Mon Sep 17 00:00:00 2001 From: "Endurance, the Martian" Date: Tue, 21 Nov 2023 01:27:28 +0100 Subject: [PATCH 04/12] fixes: markdown formatting error --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bbc8291..39b2f1a 100644 --- a/README.md +++ b/README.md @@ -130,13 +130,13 @@ class VideoCourse extends Model use HasVouchers; # ... } - +``` ## Creating Vouchers ### Using the facade You can create one or multiple vouchers by using the `Vouchers` facade: - +``` ```php $videoCourse = VideoCourse::find(1); From e6797d3ce5600aed3c2f0b47ddbf3bfbc8d97c27 Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 14:43:07 +0100 Subject: [PATCH 05/12] Added tests --- tests/CanRedeemVouchersTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/CanRedeemVouchersTest.php b/tests/CanRedeemVouchersTest.php index d86ed47..7c2ce3a 100644 --- a/tests/CanRedeemVouchersTest.php +++ b/tests/CanRedeemVouchersTest.php @@ -101,4 +101,28 @@ public function redeeming_vouchers_fires_an_event() return $e->user->id === $user->id && $e->voucher->id === $voucher->id; }); } + + public function test_is_valid_code() + { + $this->assertFalse(Vouchers::isValidCode('invalid')); + + $item = Item::create(['name' => 'Foo']); + + $voucher = $item->createVoucher(); + + $this->assertTrue(Vouchers::isValidCode($voucher->code)); + } + + public function test_is_valid_voucher() + { + $item = Item::create(['name' => 'Foo']); + $voucher = $item->createVoucher([], today()->subDay()); + + $this->assertFalse(Vouchers::isValidVoucher($voucher)); + + $item = Item::create(['name' => 'Foo']); + $voucher = $item->createVoucher(); + + $this->assertTrue(Vouchers::isValidVoucher($voucher)); + } } \ No newline at end of file From 433f93b1512391b86da319a2e95c7f293dfe0d9f Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 14:49:00 +0100 Subject: [PATCH 06/12] exclude certain combinations from test matrix --- .github/workflows/main.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index be3a31e..8080c2d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,6 +16,11 @@ jobs: php: [7.4, 8.0, 8.1] laravel: [8.*, 10.*] stability: [prefer-lowest, prefer-stable] + exclude: + - php: 7.4 + laravel: 10.* + - php: 7.4 + laravel: 9.* include: - laravel: 10.* testbench: ^8.0 From 59cb43c4f971ebe05ab2c8484c6a313502cfd69c Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 14:52:17 +0100 Subject: [PATCH 07/12] further simplified test matrix --- .github/workflows/main.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8080c2d..733c0e5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,6 @@ jobs: os: [ubuntu-latest, windows-latest] php: [7.4, 8.0, 8.1] laravel: [8.*, 10.*] - stability: [prefer-lowest, prefer-stable] exclude: - php: 7.4 laravel: 10.* @@ -27,7 +26,7 @@ jobs: - laravel: 8.* testbench: ^6.6 - name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.os }} steps: - name: Checkout code From 3640782d46d0e0043e96744fd1c6dc95b411749a Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 14:59:27 +0100 Subject: [PATCH 08/12] wip --- .github/workflows/main.yml | 59 +++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 733c0e5..c18a336 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,26 +7,44 @@ on: branches: [master] jobs: - test: - runs-on: ${{ matrix.os }} + php-tests: + runs-on: ubuntu-latest + strategy: - fail-fast: true + fail-fast: false matrix: - os: [ubuntu-latest, windows-latest] - php: [7.4, 8.0, 8.1] - laravel: [8.*, 10.*] - exclude: - - php: 7.4 - laravel: 10.* - - php: 7.4 - laravel: 9.* + php: + - '8.3' + - '8.2' + - '8.1' + - '8.0' + laravel: + - '10.*' + - '9.*' + - '8.*' + dependency-version: + - 'prefer-stable' include: - - laravel: 10.* - testbench: ^8.0 - - laravel: 8.* - testbench: ^6.6 + - laravel: '10.*' + php: '8.3' + testbench: '8.*' + - laravel: '10.*' + php: '8.2' + testbench: '8.*' + - laravel: '9.*' + php: '8.2' + testbench: '7.*' + - laravel: '9.*' + php: '8.1' + testbench: '7.*' + - laravel: '8.*' + php: '8.0' + testbench: '6.*' + - laravel: '8.*' + php: '8.1' + testbench: '6.*' - name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.os }} + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest steps: - name: Checkout code @@ -36,18 +54,13 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick coverage: none - - name: Setup problem matchers - run: | - echo "::add-matcher::${{ runner.tool_cache }}/php.json" - echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - name: Install dependencies run: | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update - composer update --${{ matrix.stability }} --prefer-dist --no-interaction + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - name: Execute tests run: vendor/bin/phpunit From d9583897e3f573f9bfb947fffc6e13aa01895643 Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 15:04:40 +0100 Subject: [PATCH 09/12] wip --- .github/workflows/main.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c18a336..933c977 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,6 +6,7 @@ on: pull_request: branches: [master] + jobs: php-tests: runs-on: ubuntu-latest @@ -32,16 +33,16 @@ jobs: php: '8.2' testbench: '8.*' - laravel: '9.*' - php: '8.2' + php: '8.3' testbench: '7.*' - laravel: '9.*' - php: '8.1' + php: '8.2' testbench: '7.*' - laravel: '8.*' - php: '8.0' + php: '8.1' testbench: '6.*' - laravel: '8.*' - php: '8.1' + php: '8.0' testbench: '6.*' name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest From 4b9e8a8c3b83d78ede31ff6a094f49d18e97dadf Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 15:09:10 +0100 Subject: [PATCH 10/12] wip --- .github/workflows/main.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 933c977..4217753 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,6 +25,10 @@ jobs: - '8.*' dependency-version: - 'prefer-stable' + + exclude: + - laravel: '10.*' + php: '8.0' include: - laravel: '10.*' php: '8.3' @@ -32,12 +36,27 @@ jobs: - laravel: '10.*' php: '8.2' testbench: '8.*' + - laravel: '10.*' + php: '8.1' + testbench: '8.*' - laravel: '9.*' php: '8.3' testbench: '7.*' - laravel: '9.*' php: '8.2' testbench: '7.*' + - laravel: '9.*' + php: '8.1' + testbench: '7.*' + - laravel: '9.*' + php: '8.0' + testbench: '7.*' + - laravel: '8.*' + php: '8.3' + testbench: '6.*' + - laravel: '8.*' + php: '8.2' + testbench: '6.*' - laravel: '8.*' php: '8.1' testbench: '6.*' @@ -45,6 +64,7 @@ jobs: php: '8.0' testbench: '6.*' + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest steps: From af488b9579a476787bd7832dcac3bdb9974e26b0 Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 15:12:37 +0100 Subject: [PATCH 11/12] wip --- .gitattributes | 2 -- .scrutinizer.yml | 19 ------------------- .travis.yml | 23 ----------------------- README.md | 2 -- 4 files changed, 46 deletions(-) delete mode 100644 .scrutinizer.yml delete mode 100644 .travis.yml diff --git a/.gitattributes b/.gitattributes index bb6265e..757ed64 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,8 +4,6 @@ # Ignore all test and documentation with "export-ignore". /.gitattributes export-ignore /.gitignore export-ignore -/.travis.yml export-ignore /phpunit.xml.dist export-ignore -/.scrutinizer.yml export-ignore /tests export-ignore /.editorconfig export-ignore diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index df16b68..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,19 +0,0 @@ -filter: - excluded_paths: [tests/*] - -checks: - php: - remove_extra_empty_lines: true - remove_php_closing_tag: true - remove_trailing_whitespace: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: true - order_alphabetically: true - fix_php_opening_tag: true - fix_linefeed: true - fix_line_ending: true - fix_identation_4spaces: true - fix_doc_comments: true - diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5819dec..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php - -php: - - 7.1 - - 7.2 - - 7.3 - - 7.4 - - 8.0 - - 8.1 -env: - matrix: - - COMPOSER_FLAGS="--prefer-lowest" - - COMPOSER_FLAGS="" - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover diff --git a/README.md b/README.md index 39b2f1a..aea0052 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # Laravel Vouchers 🎟 [![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-vouchers.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-vouchers) -[![Build Status](https://img.shields.io/travis/beyondcode/laravel-vouchers/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-vouchers) -[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-vouchers.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-vouchers) [![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-vouchers.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-vouchers) This package can associate vouchers with your Eloquent models. This might come in handy, if you need to associate voucher codes with content that is stored in your Eloquent models. From 950e78625033a84ed5e4921b36e6b961d4762bdd Mon Sep 17 00:00:00 2001 From: Di Date: Fri, 15 Dec 2023 16:28:51 +0100 Subject: [PATCH 12/12] Readme format --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aea0052..9601b6e 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ class VideoCourse extends Model ### Using the facade You can create one or multiple vouchers by using the `Vouchers` facade: -``` + ```php $videoCourse = VideoCourse::find(1);