Skip to content

Commit

Permalink
Merge pull request #4 from Rjiegit/feat-php8-support
Browse files Browse the repository at this point in the history
Support PHP 8
  • Loading branch information
shengyou authored Jul 31, 2023
2 parents 65f1178 + 5e01839 commit 3838cc3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PHP

on: [ 'push', 'pull_request' ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
operating-system: [ 'ubuntu-latest' ]
php-versions: [ '7.2','7.3','7.4','8.0' ]

steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer run-script test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/build
/vendor
composer.lock

/.idea
.phpunit.result.cache
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
}
],
"require": {
"php": "^7.0"
"php": "^7.0|^8.0"
},
"require-dev": {
"illuminate/support": "^5.3",
"phpunit/phpunit": "^7.2",
"phpunit/phpunit": "^8.5.12",
"squizlabs/php_codesniffer": "^3.3"
},
"autoload": {
Expand All @@ -45,7 +45,10 @@
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
"optimize-autoloader": true,
"allow-plugins": {
"kylekatarnls/update-helper": false
}
},
"extra": {
"laravel": {
Expand Down
4 changes: 3 additions & 1 deletion src/TaiwanIdentityCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public function make($location = null, $gender = null)

$count = $this->locations[$id_number_chars[0]];
foreach ($this->weights as $i => $weight) {
if ($i == 8) break;
if ($i == 8) {
break;
}
$count += $id_number_chars[$i + 1] * $weight;
}

Expand Down
58 changes: 58 additions & 0 deletions tests/TaiwanIdentityCardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Meditate\IdentityCard\Tests;

use Exception;
use Meditate\IdentityCard\TaiwanIdentityCard;
use PHPUnit\Framework\TestCase;

class TaiwanIdentityCardTest extends TestCase
{
public function testCheckValidIdNumber()
{
$id_card = new TaiwanIdentityCard();
$valid_id_number = 'A123456789';

$this->assertTrue($id_card->check($valid_id_number));
}

public function testCheckInvalidIdNumber()
{
$id_card = new TaiwanIdentityCard();
$invalid_id_number = 'A123456780';

$this->assertFalse($id_card->check($invalid_id_number));
}

public function testMakeFakeIdNumberWithRandomLocationAndGender()
{
$id_card = new TaiwanIdentityCard();
$fake_id_number = $id_card->make();

$this->assertTrue($id_card->check($fake_id_number));
}

public function testMakeFakeIdNumberWithSpecifiedLocationAndGender()
{
$id_card = new TaiwanIdentityCard();
$fake_id_number = $id_card->make('A', 1);

$this->assertSame(substr($fake_id_number, 0, 1), 'A');
$this->assertSame(substr($fake_id_number, 1, 1), '1');
$this->assertTrue($id_card->check($fake_id_number));
}

public function testMakeFakeIdNumberWithInvalidLocation()
{
$this->expectException(Exception::class);
$id_card = new TaiwanIdentityCard();
$id_card->make('AA');
}

public function testMakeFakeIdNumberWithInvalidGender()
{
$this->expectException(Exception::class);
$id_card = new TaiwanIdentityCard();
$id_card->make('A', 3);
}
}

0 comments on commit 3838cc3

Please sign in to comment.