Skip to content

Commit 3ed053e

Browse files
authored
Merge pull request #51 from hendurhance/feat-valid-vouchers
Enhancement: Add Function to Check Voucher Validity
2 parents 11a7c78 + 950e786 commit 3ed053e

File tree

7 files changed

+116
-66
lines changed

7 files changed

+116
-66
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
# Ignore all test and documentation with "export-ignore".
55
/.gitattributes export-ignore
66
/.gitignore export-ignore
7-
/.travis.yml export-ignore
87
/phpunit.xml.dist export-ignore
9-
/.scrutinizer.yml export-ignore
108
/tests export-ignore
119
/.editorconfig export-ignore

.github/workflows/main.yml

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,66 @@ on:
66
pull_request:
77
branches: [master]
88

9+
910
jobs:
10-
test:
11-
runs-on: ${{ matrix.os }}
11+
php-tests:
12+
runs-on: ubuntu-latest
13+
1214
strategy:
13-
fail-fast: true
15+
fail-fast: false
1416
matrix:
15-
os: [ubuntu-latest, windows-latest]
16-
php: [7.4, 8.0, 8.1]
17-
laravel: [8.*, 10.*]
18-
stability: [prefer-lowest, prefer-stable]
17+
php:
18+
- '8.3'
19+
- '8.2'
20+
- '8.1'
21+
- '8.0'
22+
laravel:
23+
- '10.*'
24+
- '9.*'
25+
- '8.*'
26+
dependency-version:
27+
- 'prefer-stable'
28+
29+
exclude:
30+
- laravel: '10.*'
31+
php: '8.0'
1932
include:
20-
- laravel: 10.*
21-
testbench: ^8.0
22-
- laravel: 8.*
23-
testbench: ^6.6
33+
- laravel: '10.*'
34+
php: '8.3'
35+
testbench: '8.*'
36+
- laravel: '10.*'
37+
php: '8.2'
38+
testbench: '8.*'
39+
- laravel: '10.*'
40+
php: '8.1'
41+
testbench: '8.*'
42+
- laravel: '9.*'
43+
php: '8.3'
44+
testbench: '7.*'
45+
- laravel: '9.*'
46+
php: '8.2'
47+
testbench: '7.*'
48+
- laravel: '9.*'
49+
php: '8.1'
50+
testbench: '7.*'
51+
- laravel: '9.*'
52+
php: '8.0'
53+
testbench: '7.*'
54+
- laravel: '8.*'
55+
php: '8.3'
56+
testbench: '6.*'
57+
- laravel: '8.*'
58+
php: '8.2'
59+
testbench: '6.*'
60+
- laravel: '8.*'
61+
php: '8.1'
62+
testbench: '6.*'
63+
- laravel: '8.*'
64+
php: '8.0'
65+
testbench: '6.*'
2466

25-
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
67+
68+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest
2669

2770
steps:
2871
- name: Checkout code
@@ -32,18 +75,13 @@ jobs:
3275
uses: shivammathur/setup-php@v2
3376
with:
3477
php-version: ${{ matrix.php }}
35-
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
78+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
3679
coverage: none
3780

38-
- name: Setup problem matchers
39-
run: |
40-
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
41-
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
42-
4381
- name: Install dependencies
4482
run: |
4583
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
46-
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
84+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
4785
4886
- name: Execute tests
4987
run: vendor/bin/phpunit

.scrutinizer.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Laravel Vouchers 🎟
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-vouchers.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-vouchers)
4-
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-vouchers/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-vouchers)
5-
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-vouchers.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-vouchers)
64
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-vouchers.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-vouchers)
75

86
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
130128
use HasVouchers;
131129
# ...
132130
}
133-
131+
```
134132
## Creating Vouchers
135133

136134
### Using the facade
@@ -220,6 +218,14 @@ $voucher = $user->redeemCode('ABCD-EFGH');
220218
$videoCourse = $voucher->model;
221219
```
222220

221+
## Validating Vouchers & Voucher Codes
222+
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.
223+
224+
```php
225+
Vouchers::isValidCode('ABCD-EFGH'); // true or false
226+
Vouchers::isValidVoucher($voucher); // true or false
227+
```
228+
223229
## Handling Errors
224230

225231
The `redeemCode` and `redeemVoucher` methods throw a couple of exceptions that you will want to catch and react to in your application:

src/Vouchers.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ public function check(string $code)
8282
return $voucher;
8383
}
8484

85+
/**
86+
* @param string $code
87+
* @return bool
88+
*/
89+
public function isValidCode(string $code): bool
90+
{
91+
try {
92+
$this->check($code);
93+
} catch (VoucherIsInvalid $exception) {
94+
return false;
95+
} catch (VoucherExpired $exception) {
96+
return false;
97+
}
98+
99+
return true;
100+
}
101+
102+
/**
103+
* @param Voucher $voucher
104+
* @return bool
105+
*/
106+
public function isValidVoucher(Voucher $voucher): bool
107+
{
108+
return $this->isValidCode($voucher->code);
109+
}
110+
85111
/**
86112
* @return string
87113
*/

tests/CanRedeemVouchersTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,28 @@ public function redeeming_vouchers_fires_an_event()
101101
return $e->user->id === $user->id && $e->voucher->id === $voucher->id;
102102
});
103103
}
104+
105+
public function test_is_valid_code()
106+
{
107+
$this->assertFalse(Vouchers::isValidCode('invalid'));
108+
109+
$item = Item::create(['name' => 'Foo']);
110+
111+
$voucher = $item->createVoucher();
112+
113+
$this->assertTrue(Vouchers::isValidCode($voucher->code));
114+
}
115+
116+
public function test_is_valid_voucher()
117+
{
118+
$item = Item::create(['name' => 'Foo']);
119+
$voucher = $item->createVoucher([], today()->subDay());
120+
121+
$this->assertFalse(Vouchers::isValidVoucher($voucher));
122+
123+
$item = Item::create(['name' => 'Foo']);
124+
$voucher = $item->createVoucher();
125+
126+
$this->assertTrue(Vouchers::isValidVoucher($voucher));
127+
}
104128
}

0 commit comments

Comments
 (0)