From 104fee61300353d87d7a24f97f661388b368a1f1 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 23:56:30 +0200 Subject: [PATCH 1/9] Merge workflows --- .github/workflows/report-metrics.yml | 31 ---------------------------- .github/workflows/run-tests.yml | 25 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 31 deletions(-) delete mode 100644 .github/workflows/report-metrics.yml diff --git a/.github/workflows/report-metrics.yml b/.github/workflows/report-metrics.yml deleted file mode 100644 index e6fdbf2..0000000 --- a/.github/workflows/report-metrics.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: report-metrics - -on: - workflow_dispatch: - push: - -jobs: - test: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 8.3 - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo - coverage: pcov - - name: Install dependencies - run: composer update --prefer-stable --prefer-dist --no-interaction - - name: Execute tests - run: vendor/bin/pest --coverage-clover=coverage.xml - - name: Execute type coverage tests - run: vendor/bin/pest --type-coverage --type-coverage-json=pest-coverage.json - - - name: Upload coverage results - run: | - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --repo-token ${{ secrets.OTTERWISE_TOKEN }} - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file pest-coverage.json diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 44d7d64..25ad5dd 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -38,3 +38,28 @@ jobs: - name: Upload coverage results run: bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --repo-token ${{ secrets.OTTERWISE_TOKEN }} bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file pest-coverage.json + + report-metrics: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: pcov + - name: Install dependencies + run: composer update --prefer-stable --prefer-dist --no-interaction + - name: Execute tests + run: vendor/bin/pest --coverage-clover=coverage.xml + - name: Execute type coverage tests + run: vendor/bin/pest --type-coverage --type-coverage-json=pest-coverage.json + + - name: Upload coverage results + run: | + bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --repo-token ${{ secrets.OTTERWISE_TOKEN }} + bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file pest-coverage.json From 099b8e226b77b7d57b5757bb89d4679bf31e1431 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 23:58:40 +0200 Subject: [PATCH 2/9] fix file paths --- .github/workflows/run-tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 25ad5dd..b4a41c4 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -55,11 +55,11 @@ jobs: - name: Install dependencies run: composer update --prefer-stable --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/pest --coverage-clover=coverage.xml + run: vendor/bin/pest --coverage-clover=build/logs/clover.xml - name: Execute type coverage tests - run: vendor/bin/pest --type-coverage --type-coverage-json=pest-coverage.json + run: vendor/bin/pest --type-coverage --type-coverage-json=build/logs/pest-coverage.json - name: Upload coverage results run: | bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --repo-token ${{ secrets.OTTERWISE_TOKEN }} - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file pest-coverage.json + bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file build/logs/pest-coverage.json From 9d7b9c6151427762286a8921efe0044593be7aae Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 00:05:23 +0200 Subject: [PATCH 3/9] Test upload coverage workflow --- .github/workflows/run-tests.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b4a41c4..8e4662f 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -45,6 +45,8 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + with: + fetch-depth: 2 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -59,7 +61,12 @@ jobs: - name: Execute type coverage tests run: vendor/bin/pest --type-coverage --type-coverage-json=build/logs/pest-coverage.json - - name: Upload coverage results - run: | - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --repo-token ${{ secrets.OTTERWISE_TOKEN }} - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file build/logs/pest-coverage.json + - name: Upload Test Coverage + env: + OTTERWISE_TOKEN: ${{ secrets.OTTERWISE_TOKEN }} + run: bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) + + - name: Upload Types Coverage + env: + OTTERWISE_TOKEN: ${{ secrets.OTTERWISE_TOKEN }} + run: bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file build/logs/pest-coverage.json From 20072302e2854841af3527e8286ebf2bd868085b Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 00:27:22 +0200 Subject: [PATCH 4/9] Cleanup test workflow --- .github/workflows/run-tests.yml | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 8e4662f..68ddcbf 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -19,25 +19,16 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 - - name: Setup PHP 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 coverage: pcov - - 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 update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/pest --coverage-clover=coverage.xml - - - name: Upload coverage results - run: bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --repo-token ${{ secrets.OTTERWISE_TOKEN }} - bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file pest-coverage.json + run: vendor/bin/pest report-metrics: runs-on: ubuntu-latest From 5af9e96407364bcbe2399a12adb662f3283774f9 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 02:22:36 +0200 Subject: [PATCH 5/9] Imporve tests and code quality --- .gitignore | 3 +- composer.json | 4 +- phpunit.xml | 10 +- src/FakeCar.php | 3 +- .../FerrariEnzoTestProvider.php | 7 +- tests/Unit/FakeCarTest.php | 114 ++++--------- tests/Unit/VehicleRangeTest.php | 157 ++++++++++++++++++ tests/Unit/VinTest.php | 74 +++++++++ 8 files changed, 272 insertions(+), 100 deletions(-) create mode 100644 tests/Unit/VehicleRangeTest.php create mode 100644 tests/Unit/VinTest.php diff --git a/.gitignore b/.gitignore index f5aea57..2b1574d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,11 @@ composer.lock # Temp files coverage.clover +pest-coverage.json .phpunit.result.cache .php-cs-fixer.cache ocular.phar reports outdated.txt .phpunit.cache/ -coverage/ \ No newline at end of file +/build diff --git a/composer.json b/composer.json index c57895d..76210bb 100644 --- a/composer.json +++ b/composer.json @@ -49,13 +49,13 @@ "./vendor/bin/pest" ], "test-coverage": [ - "./vendor/bin/pest --coverage-text" + "./vendor/bin/pest --coverage" ], "types": [ "./vendor/bin/pest --type-coverage" ], "all": [ - "composer run test && composer run test-coverage && composer run phpmd && composer run codemetrics" + "composer run test-coverage && composer run types && composer run lint" ] }, "config": { diff --git a/phpunit.xml b/phpunit.xml index 535ba65..fa04147 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -20,10 +20,12 @@ - - - - + + + + + + diff --git a/src/FakeCar.php b/src/FakeCar.php index 3f5f066..a63c035 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -4,6 +4,7 @@ use Exception; use Faker\Generator; +use InvalidArgumentException; class FakeCar extends \Faker\Provider\Base { @@ -226,7 +227,7 @@ public function isSupported(string $method): bool return true; } - throw new \RuntimeException('Method not supported be data provider. Please implement '.$method.'() in your data provider.'); + throw new InvalidArgumentException('Method not supported be data provider. Please implement '.$method.'() in your data provider.'); } /** diff --git a/tests/TestDataProviders/FerrariEnzoTestProvider.php b/tests/TestDataProviders/FerrariEnzoTestProvider.php index d6c111b..03f1d2f 100644 --- a/tests/TestDataProviders/FerrariEnzoTestProvider.php +++ b/tests/TestDataProviders/FerrariEnzoTestProvider.php @@ -64,11 +64,6 @@ public function getVehicleGearBoxType(): string public function getVehicleEnginePower(): string { - // TODO: Implement getVehicleEnginePower() method. - } - - public function getVehicleEngineTorque(): string - { - // TODO: Implement getVehicleEngineTorque() method. + return '6.0 L'; } } diff --git a/tests/Unit/FakeCarTest.php b/tests/Unit/FakeCarTest.php index 6747ee7..d6a32e1 100644 --- a/tests/Unit/FakeCarTest.php +++ b/tests/Unit/FakeCarTest.php @@ -1,6 +1,7 @@ faker->vehicleSeatCount())->toBeGreaterThanOrEqual(1) - ->and($this->faker->vehicleSeatCount())->toBeLessThanOrEqual(9) - ->and($this->faker->vehicleSeatCount())->toBeInt(); + expect($this->faker->vehicleDoorCount())->toBeGreaterThanOrEqual(1) + ->and($this->faker->vehicleDoorCount())->toBeLessThanOrEqual(9) + ->and($this->faker->vehicleDoorCount())->toBeInt(); } }); @@ -109,6 +110,10 @@ expect(array_keys(FakeCarData::$vehicleGearBoxType))->toContain($this->faker->vehicleGearBoxType()); }); +test('vehicle gear box value', function () { + expect(array_keys(FakeCarData::$vehicleGearBoxType))->toContain($this->faker->vehicleGearBoxTypeValue()); +}); + test('get random elements from array', function () { $data = [ 'value1', @@ -168,54 +173,6 @@ expect(FakeCarHelper::getWeighted([]))->toEqual(''); }); -test('valid vin', function ($vin, $valid) { - expect($this->faker->validateVin($vin))->toBe($valid); -})->with([ - ['z2j9hhgr8Ahl1e3g', false], // Too short - ['az2j9hhgr8Ahl1e3gs', false], // Too long - ['z2j9hhgr2Ahl1e3gs', false], // Invalid check digit - ['z2j9hhgr8Ahl1e3gd', false], // Invalid - ['z2j9hhgr8Ahl1e3gs', true], // Valid VINs - ['n7u30vns7Ajsrb1nc', true], - ['3julknxb0A06hj41x', true], - ['yj12c8z40Aca2x6p3', true], - ['y95wf7gm1A9g7pz5z', true], - ['355430557Azf4u0vr', true], -]); - -test('vin returns valid vin', function () { - $vin = $this->faker->vin(); - expect($this->faker->validateVin($vin))->toBeTrue(); -}); -test('model year', function ($year, $expected) { - $object = new FakeCar($this->faker); - - expect($this->callProtectedMethod([$year], 'encodeModelYear', $object))->toEqual($expected); -})->with([ - [1980, 'A'], - [2000, 'Y'], - [2017, 'H'], - [2018, 'J'], - [2019, 'K'], -]); -test('transliterate', function () { - expect($this->callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker)))->toEqual(0) - ->and($this->callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker)))->toEqual(1) - ->and($this->callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker)))->toEqual(2); -}); - -test('check digit', function () { - expect($this->callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker)))->toEqual('4') - ->and($this->callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker)))->toEqual('1') - ->and($this->callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker)))->toEqual('8'); -}); - -test('vin', function () { - $vin = $this->faker->vin(); - expect($vin)->toMatch('/[a-zA-Z0-9]{17}/') - ->and($this->faker->validateVin($vin))->toBeTrue(); -}); - test('engine power', function () { $power = $this->faker->vehicleEnginePower(); expect($power)->toMatch('/^\d+ hp$/') @@ -223,6 +180,12 @@ ->and((int) explode(' ', $power)[0])->toBeLessThanOrEqual(1500); }); +test('engine power value', function () { + $power = $this->faker->vehicleEnginePowerValue(); + expect((int) explode(' ', $power)[0])->toBeGreaterThanOrEqual(100) + ->and((int) explode(' ', $power)[0])->toBeLessThanOrEqual(1500); +}); + test('engine torque', function () { $torque = $this->faker->vehicleEngineTorque(); expect($torque)->toMatch('/^\d+ nm$/') @@ -230,45 +193,24 @@ ->and((int) explode(' ', $torque)[0])->toBeLessThanOrEqual(700); }); -test('get range', function () { - for ($x = 0; $x < 100; $x++) { - $range = FakeCarHelper::getRange([1, 100], 0); - - expect((string) $range)->toMatch('/^\d+$/') - ->and((int) $range)->toBeGreaterThanOrEqual(1) - ->and((int) $range)->toBeLessThanOrEqual(100); - } - - for ($x = 0; $x < 100; $x++) { - $range = FakeCarHelper::getRange([100, 150], 2); +test('engine torque value', function () { + $torque = $this->faker->vehicleEngineTorqueValue(); - expect($range)->toMatch('/^\d+\.\d+$/') - ->and((int) $range)->toBeGreaterThanOrEqual(100) - ->and((int) $range)->toBeLessThanOrEqual(150); - } -}); -test('get range invalid', function () { - $this->expectException('\Random\RandomException'); - FakeCarHelper::getRange([100, 50], 2); - - $this->expectException('\InvalidArgumentException'); - FakeCarHelper::getRange([100, 50], -2); + expect((int) explode(' ', $torque)[0])->toBeGreaterThanOrEqual(100) + ->and((int) explode(' ', $torque)[0])->toBeLessThanOrEqual(700); }); -test('get range with unit', function () { - for ($x = 0; $x < 100; $x++) { - $range = FakeCarHelper::getRangeWithUnit([2065, 2450], 'l', 0); +test('is supported check', function () { + $faker = (new Factory)::create(); + $fakeCar = new FakeCar($faker); + $fakeCar->setDataProvider(new FerrariEnzoTestProvider); + $faker->addProvider($fakeCar); - expect($range)->toMatch('/^\d+ l$/') - ->and((int) $range)->toBeGreaterThanOrEqual(2065) - ->and((int) $range)->toBeLessThanOrEqual(2450); - } + expect($fakeCar->isSupported('vehicleEnginePower'))->toBeTrue(); - for ($x = 0; $x < 100; $x++) { - $range = FakeCarHelper::getRangeWithUnit([200, 250], 'hp', 2); + expect(fn () => $fakeCar->isSupported('vehicleEngineTorque')) + ->toThrow(InvalidArgumentException::class); - expect($range)->toMatch('/^\d+\.\d+ hp$/') - ->and((int) $range)->toBeGreaterThanOrEqual(200) - ->and((int) $range)->toBeLessThanOrEqual(250); - } + expect(fn () => $fakeCar->isSupported('invalidMethod')) + ->toThrow(InvalidArgumentException::class); }); diff --git a/tests/Unit/VehicleRangeTest.php b/tests/Unit/VehicleRangeTest.php new file mode 100644 index 0000000..42f9055 --- /dev/null +++ b/tests/Unit/VehicleRangeTest.php @@ -0,0 +1,157 @@ +addProvider(new FakeCar($faker)); + $this->faker = $faker; +}); + +test('get range', function () { + $range = [1, 100]; + for ($x = 0; $x < 100; $x++) { + $result = FakeCarHelper::getRange($range, 0); + + expect((string) $result)->toMatch('/^\d+$/'); + expect($result)->toBeInt(); + expect($result)->toBeGreaterThanOrEqual($range[0]); + expect($result)->toBeLessThanOrEqual($range[1]); + } + + $range = [100, 150]; + for ($x = 0; $x < 100; $x++) { + $result = FakeCarHelper::getRange($range, 2); + + expect($result)->toBeString(); + expect($result)->toMatch('/^\d+\.\d+$/'); + expect($result)->toBeGreaterThanOrEqual($range[0]); + expect($result)->toBeLessThanOrEqual($range[1]); + } +}); +test('get range invalid', function () { + $this->expectException('\Random\RandomException'); + FakeCarHelper::getRange([100, 50], 2); + + $this->expectException('\InvalidArgumentException'); + FakeCarHelper::getRange([100, 50], -2); +}); + +test('get range with unit', function () { + $unit = 'l'; + $range = [2065, 2450]; + for ($x = 0; $x < 100; $x++) { + $result = FakeCarHelper::getRangeWithUnit($range, $unit, 0); + + expect($result)->toBeString(); + expect($result)->toContain($unit); + expect($result)->toMatch('/^\d+ l$/'); + $value = (int) trim(str_replace($unit, '', $result)); + expect($value)->toBeGreaterThanOrEqual($range[0]); + expect($value)->toBeLessThanOrEqual($range[1]); + } + + $unit = 'hp'; + $range = [200, 250]; + + for ($x = 0; $x < 100; $x++) { + $result = FakeCarHelper::getRangeWithUnit([200, 250], $unit, 2); + + expect($result)->toBeString(); + expect($result)->toContain($unit); + expect($result)->toMatch('/^\d+\.\d+ hp$/'); + + $value = (int) trim(str_replace($unit, '', $result)); + expect($value)->toBeGreaterThanOrEqual($range[0]); + expect($value)->toBeLessThanOrEqual($range[1]); + } +}); + +test('getRange with decimals', function () { + $range = [10, 20]; + $decimals = 2; + + $result = FakeCarHelper::getRange($range, $decimals); + + expect($result)->toBeString(); + expect((float) $result)->toBeGreaterThanOrEqual($range[0]); + expect((float) $result)->toBeLessThanOrEqual($range[1]); +}); + +test('getRange with invalid range', function () { + $range = [20, 10]; + $decimals = 0; + + expect(fn () => FakeCarHelper::getRange($range, $decimals)) + ->toThrow(RandomException::class); +}); + +test('getRange with invalid decimals', function () { + $range = [10, 20]; + $decimals = -1; + + expect(fn () => FakeCarHelper::getRange($range, $decimals)) + ->toThrow(InvalidArgumentException::class); +}); + +test('getRange with range count not equal to 2', function () { + $range = [10]; + $decimals = 0; + + expect(fn () => FakeCarHelper::getRange($range, $decimals)) + ->toThrow(RandomException::class); +}); + +test('getRangeWithUnit', function () { + $range = [10, 20]; + $unit = 'km/h'; + $decimals = 0; + + $result = FakeCarHelper::getRangeWithUnit($range, $unit, $decimals); + + expect($result)->toBeString(); + expect($result)->toContain($unit); + + $value = (int) str_replace($unit, '', $result); + expect($value)->toBeGreaterThanOrEqual($range[0]); + expect($value)->toBeLessThanOrEqual($range[1]); +}); + +test('getRangeWithUnit with decimals', function () { + $range = [10, 20]; + $unit = 'km/h'; + $decimals = 2; + + $result = FakeCarHelper::getRangeWithUnit($range, $unit, $decimals); + + expect($result)->toBeString(); + expect($result)->toContain($unit); + + $value = (float) str_replace($unit, '', $result); + expect($value)->toBeGreaterThanOrEqual($range[0]); + expect($value)->toBeLessThanOrEqual($range[1]); +}); + +test('getRangeWithUnit with invalid range', function () { + $range = [20, 10]; + $unit = 'km/h'; + $decimals = 0; + + expect(fn () => FakeCarHelper::getRangeWithUnit($range, $unit, $decimals)) + ->toThrow(RandomException::class); +}); + +test('getRangeWithUnit with invalid decimals', function () { + $range = [10, 20]; + $unit = 'km/h'; + $decimals = -1; + + expect(fn () => FakeCarHelper::getRangeWithUnit($range, $unit, $decimals)) + ->toThrow(InvalidArgumentException::class); +}); diff --git a/tests/Unit/VinTest.php b/tests/Unit/VinTest.php new file mode 100644 index 0000000..4ed22a8 --- /dev/null +++ b/tests/Unit/VinTest.php @@ -0,0 +1,74 @@ +addProvider(new FakeCar($faker)); + $this->faker = $faker; +}); + +test('valid vin', function ($vin, $valid) { + expect($this->faker->validateVin($vin))->toBe($valid); +})->with([ + ['z2j9hhgr8Ahl1e3g', false], // Too short + ['az2j9hhgr8Ahl1e3gs', false], // Too long + ['z2j9hhgr-8Ahl1e3gs', false], // Invalid format + ['z2j9hhgr2Ahl1e3gs', false], // Invalid check digit + ['z2j9hhgr8Ahl1e3gd', false], // Invalid + ['z2j9hhgr8Ahl1e3gs', true], // Valid VINs below + ['n7u30vns7Ajsrb1nc', true], + ['3julknxb0A06hj41x', true], + ['yj12c8z40Aca2x6p3', true], + ['y95wf7gm1A9g7pz5z', true], + ['355430557Azf4u0vr', true], +]); + +test('vin returns valid vin', function () { + $vin = $this->faker->vin(); + expect($this->faker->validateVin($vin))->toBeTrue(); +}); +test('model year', function ($year, $expected) { + $object = new FakeCar($this->faker); + + expect($this->callProtectedMethod([$year], 'encodeModelYear', $object))->toEqual($expected); +})->with([ + [1980, 'A'], + [2000, 'Y'], + [2017, 'H'], + [2018, 'J'], + [2019, 'K'], +]); +test('transliterate', function ($character, $expected) { + expect($this->callProtectedMethod( + [$character], + 'transliterate', + new FakeCar($this->faker)) + )->toEqual($expected); +})->with([ + ['O', 0], + ['A', 1], + ['K', 2], +]); + +test('check digit', function ($vin, $expected) { + expect($this->callProtectedMethod( + [$vin], + 'checkDigit', + new FakeCar($this->faker) + ))->toEqual($expected); +})->with([ + ['z2j9hhgr8Ahl1e3g', '4'], + ['n7u30vns7Ajsrb1n', '1'], + ['3julknxb0A06hj41', '8'], +]); + +test('vin', function () { + $vin = $this->faker->vin(); + expect($vin)->toMatch('/[a-zA-Z0-9]{17}/'); + expect($this->faker->validateVin($vin))->toBeTrue(); +}); From 9e3f860551178a1180178e634be9e61006ec578c Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 02:23:16 +0200 Subject: [PATCH 6/9] Add non strict validation for VIN --- src/FakeCar.php | 7 ++++++- src/FakeCarHelper.php | 1 - tests/Unit/VinTest.php | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/FakeCar.php b/src/FakeCar.php index a63c035..ee277d6 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -261,8 +261,13 @@ private static function transliterate(string $character): int return stripos(self::EBCDIC, $character) % 10; } - public static function validateVin(string $vin): bool + public static function validateVin(string $vin, bool $strict = true): bool { + if (! $strict) { + /** @var string $vin */ + $vin = preg_replace('/[^A-Za-z0-9]/', '', $vin); + } + if (strlen($vin) !== 17) { return false; } diff --git a/src/FakeCarHelper.php b/src/FakeCarHelper.php index d959d14..13fe675 100644 --- a/src/FakeCarHelper.php +++ b/src/FakeCarHelper.php @@ -137,7 +137,6 @@ public static function getRange(array $range, int $decimals = 0): int|string if ($decimals < 0) { throw new InvalidArgumentException('Invalid decimals'); } - if ($decimals > 0) { $factor = 10 ** $decimals; diff --git a/tests/Unit/VinTest.php b/tests/Unit/VinTest.php index 4ed22a8..23e5330 100644 --- a/tests/Unit/VinTest.php +++ b/tests/Unit/VinTest.php @@ -28,6 +28,14 @@ ['355430557Azf4u0vr', true], ]); +test('valid vin non strict', function ($vin, $valid) { + expect($this->faker->validateVin($vin, strict: false))->toBe($valid); +})->with([ + ['z2j9hhgr-8Ahl1e3gs', true], + ['z2j_9hhgr-8Ahl1/e3gs', true], + ['z2j9hhgr9Ahl1e3gs', false], // Invalid check digit +]); + test('vin returns valid vin', function () { $vin = $this->faker->vin(); expect($this->faker->validateVin($vin))->toBeTrue(); From 7e73848009b868cf9d473a9f50fddafc78b041ee Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 02:27:06 +0200 Subject: [PATCH 7/9] Delate scrutinizer config in repo --- .scrutinizer.yml | 66 ------------------------------------------------ 1 file changed, 66 deletions(-) delete mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index efedab8..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,66 +0,0 @@ -build: - environment: - php: - version: 8.2 - variables: - XDEBUG_MODE: coverage - nodes: - analysis: - project_setup: - override: - - 'true' - tests: - override: - - php-scrutinizer-run - tests: true - coverage: - tests: - override: - - command: vendor/bin/phpunit --coverage-clover=coverage.xml - coverage: - file: coverage.xml - format: clover - - -filter: - excluded_paths: - - bin/* - - tests/* - -checks: - php: - code_rating: true - custom_coding_standard: - ruleset_path: 'phpcs.xml' - -tools: - php_cs_fixer: - enabled: true - extensions: - - php - filter: - excluded_paths: - - bin/* - - tests/* - - php_sim: - enabled: true - min_mass: 30 - - php_pdepend: - enabled: true - configuration_file: null - suffixes: - - php - - php_analyzer: - enabled: true - extensions: - - php - - php_hhvm: - enabled: true - command: hhvm - extensions: - - php - From 391ce2696b4ccad0f2e0cde0e24c8fd8507b53fa Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 02:28:37 +0200 Subject: [PATCH 8/9] Chnage coverage badge --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 369b62d..f182e3b 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,7 @@ Faker provider for fake car data [![Build Status](https://scrutinizer-ci.com/g/pelmered/fake-car/badges/build.png?b=main)](https://scrutinizer-ci.com/g/pelmered/fake-car/build-status/main) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pelmered/fake-car/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/pelmered/fake-car/?branch=master) -[![Code Coverage](https://scrutinizer-ci.com/g/pelmered/fake-car/badges/coverage.png?b=main)](https://scrutinizer-ci.com/g/pelmered/fake-car/?branch=master) - +[![OtterWise Coverage](https://img.shields.io/endpoint?url=https://otterwise.app/badge/github/pelmered/fake-car/7e74d17a-eacc-4082-a465-5c5ac9583255)](https://otterwise.app/github/pelmered/fake-car) ## Installation From 3751cbf014292a3d83035608aca96734d16225a6 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 27 Sep 2024 03:22:37 +0200 Subject: [PATCH 9/9] Update badges --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f182e3b..cd69c74 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ Faker provider for fake car data [![Latest Stable Version](https://poser.pugx.org/pelmered/fake-car/v/stable)](https://packagist.org/packages/pelmered/fake-car) -[![Total Downloads](https://poser.pugx.org/pelmered/fake-car/d/total)](//packagist.org/packages/pelmered/fake-car) -[![Monthly Downloads](https://poser.pugx.org/pelmered/fake-car/d/monthly)](//packagist.org/packages/pelmered/fake-car) +[![Total Downloads](https://poser.pugx.org/pelmered/fake-car/d/total)](//packagist.org/packages/pelmered/fake-car/stats) +[![Monthly Downloads](https://poser.pugx.org/pelmered/fake-car/d/monthly)](//packagist.org/packages/pelmered/fake-car/stats) [![License](https://poser.pugx.org/pelmered/fake-car/license)](https://packagist.org/packages/pelmered/fake-car) [![Build Status](https://scrutinizer-ci.com/g/pelmered/fake-car/badges/build.png?b=main)](https://scrutinizer-ci.com/g/pelmered/fake-car/build-status/main) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pelmered/fake-car/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/pelmered/fake-car/?branch=master) -[![OtterWise Coverage](https://img.shields.io/endpoint?url=https://otterwise.app/badge/github/pelmered/fake-car/7e74d17a-eacc-4082-a465-5c5ac9583255)](https://otterwise.app/github/pelmered/fake-car) +[![OtterWise Coverage](https://img.shields.io/endpoint?url=https://otterwise.app/badge/github/pelmered/fake-car)](https://otterwise.app/github/pelmered/fake-car) ## Installation