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/.github/workflows/main.yml b/.github/workflows/main.yml index be3a31e..4217753 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,23 +6,66 @@ on: pull_request: 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.*] - stability: [prefer-lowest, prefer-stable] + php: + - '8.3' + - '8.2' + - '8.1' + - '8.0' + laravel: + - '10.*' + - '9.*' + - '8.*' + dependency-version: + - 'prefer-stable' + + exclude: + - laravel: '10.*' + php: '8.0' 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: '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.*' + - laravel: '8.*' + php: '8.0' + testbench: '6.*' - name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest steps: - name: Checkout code @@ -32,18 +75,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 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 9d917b6..9601b6e 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. @@ -130,7 +128,7 @@ class VideoCourse extends Model use HasVouchers; # ... } - +``` ## Creating Vouchers ### Using the facade @@ -220,6 +218,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: diff --git a/src/Vouchers.php b/src/Vouchers.php index 8170b52..5b33428 100755 --- a/src/Vouchers.php +++ b/src/Vouchers.php @@ -82,6 +82,32 @@ public function check(string $code) return $voucher; } + /** + * @param string $code + * @return bool + */ + public function isValidCode(string $code): bool + { + try { + $this->check($code); + } catch (VoucherIsInvalid $exception) { + return false; + } catch (VoucherExpired $exception) { + return false; + } + + return true; + } + + /** + * @param Voucher $voucher + * @return bool + */ + public function isValidVoucher(Voucher $voucher): bool + { + return $this->isValidCode($voucher->code); + } + /** * @return string */ 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