From b9a6042e5ad49951a05b769f6398483b6a440aec Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Tue, 26 Mar 2024 12:20:03 +0100 Subject: [PATCH 01/24] We shouldn't change the interface in a minor version --- src/FakeCarDataProviderInterface.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/FakeCarDataProviderInterface.php b/src/FakeCarDataProviderInterface.php index ab5f5bc..0fb9f5a 100644 --- a/src/FakeCarDataProviderInterface.php +++ b/src/FakeCarDataProviderInterface.php @@ -13,6 +13,4 @@ public function getVehicleDoorCount(): int; public function getVehicleSeatCount(): int; public function getVehicleProperties(int $count = 0): array; public function getVehicleGearBoxType(): string; - public function getVehicleEnginePower(): string; - public function getVehicleEngineTorque(): string; } From 6e3ba5dff1f0cad785551e21ab90ba63693335ec Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Sun, 31 Mar 2024 00:45:19 +0100 Subject: [PATCH 02/24] Add more data types --- README.md | 21 +++++ composer.json | 2 +- src/FakeCar.php | 44 ++++++++++ src/FakeCarData.php | 31 ++++++- src/FakeCarDataProvider.php | 17 ++++ src/FakeCarHelper.php | 36 +++++++- tests/FakeCarTest.php | 165 +++++++++++++++++++++++++----------- 7 files changed, 261 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index c7517f4..e5ca41a 100644 --- a/README.md +++ b/README.md @@ -81,8 +81,29 @@ echo $faker->vehicleGearBoxType; // manual // generate automobile engine power echo $faker->vehicleEnginePower; // 250 hp +// generate automobile engine power without unit +echo $faker->vehicleEnginePowerValue; // 175 + // generate automobile engine torque echo $faker->vehicleEngineTorque; // 300 nm + +// generate automobile engine power without unit +echo $faker->vehicleEngineTorqueValue; // 450 + +// generate automobile engine displacement +echo $faker->vehicleEngineDisplacement; // 2.0 l + +// generate automobile engine displacement without unit +echo $faker->vehicleEngineDisplacementValue; // 2.0 + +// generate automobile engine fuel consumption +echo $faker->vehicleFuelConsumption; // 5.0 l/100km + +// generate automobile engine fuel consumption without unit +echo $faker->vehicleFuelConsumptionValue; // 5.0 + +// generate automobile engine fuel consumption without unit +echo $faker->vehicleEngineCylinders; // 4 ``` ### Laravel factory example diff --git a/composer.json b/composer.json index 37a7dcb..dbd2c16 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "fakerphp/faker": "^1.10" }, "require-dev": { diff --git a/src/FakeCar.php b/src/FakeCar.php index efdec22..c78414a 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -173,6 +173,17 @@ public function vehicleGearBoxType(): string return $this->dataProvider->getVehicleGearBoxType(); } + /** + * Get random vehicle gearbox type without unit + * + * @return mixed + * @throws Exception + */ + public function vehicleGearBoxTypeValue(): string + { + return $this->dataProvider->getVehicleGearBoxType(); + } + /** * Get engine torque @@ -185,6 +196,17 @@ public function vehicleEngineTorque(): string return $this->dataProvider->getVehicleEngineTorque(); } + /** + * Get engine torque without unit + * + * @return mixed + * @throws Exception + */ + public function vehicleEngineTorqueValue(): string + { + return $this->dataProvider->getVehicleEngineTorque(); + } + /** * Get engine power (horsepower or kilowatts) * @@ -196,6 +218,28 @@ public function vehicleEnginePower(): string return $this->dataProvider->getVehicleEnginePower(); } + /** + * Get engine power without unit + * + * @return mixed + * @throws Exception + */ + public function vehicleEnginePowerValue(): string + { + $this->isSupported(__FUNCTION__); + + return $this->dataProvider->getVehicleEnginePowerValue(); + } + + public function isSupported($method): bool + { + if (method_exists($this->dataProvider, $method)) { + return true; + } + + throw new \RuntimeException('Method not supported be data provider. Please implement '.$method.'() in your data provider.'); + } + /** * @param int $year diff --git a/src/FakeCarData.php b/src/FakeCarData.php index 80d86bd..2cffd10 100644 --- a/src/FakeCarData.php +++ b/src/FakeCarData.php @@ -821,14 +821,37 @@ class FakeCarData ]; public static $vehicleEnginePower = [ - 'range' => [100, 1500], - 'unit' => 'hp', + 'range' => [100, 1500], + 'unit' => 'hp', + 'rounding' => 0 ]; public static $vehicleEngineTorque = [ - 'range' => [100, 700], - 'unit' => 'nm', + 'range' => [100, 700], + 'unit' => 'nm', + 'decimals' => 0 ]; + public static $vehicleEngineDisplacement = [ + 'range' => [500, 8000], + 'unit' => 'cc', + 'decimals' => 1 + ]; + + public static $vehicleEngineCylinders = [ + '3' => 5, + '4' => 60, + '5' => 5, + '6' => 5, + '8' => 15, + '10' => 5, + '12' => 5, + ]; + + public static $vehicleEngineFuelConsumption = [ + 'range' => [4, 30], + 'unit' => 'l/100km', + 'decimals' => 1 + ]; } diff --git a/src/FakeCarDataProvider.php b/src/FakeCarDataProvider.php index 12484d0..143766b 100644 --- a/src/FakeCarDataProvider.php +++ b/src/FakeCarDataProvider.php @@ -94,6 +94,15 @@ public function getVehicleEnginePower(): string return FakeCarHelper::getRangeWithUnit($range, $unit); } + /** + * @throws Exception + */ + public function getVehicleEnginePowerValue(): string + { + ['range' => $range] = $this->vehicleData::$vehicleEnginePower; + return FakeCarHelper::getRange($range); + } + /** * @throws Exception */ @@ -102,6 +111,14 @@ public function getVehicleEngineTorque(): string ['range' => $range, 'unit' => $unit] = $this->vehicleData::$vehicleEngineTorque; return FakeCarHelper::getRangeWithUnit($range, $unit); } + /** + * @throws Exception + */ + public function getVehicleEngineTorqueValue(): string + { + ['range' => $range] = $this->vehicleData::$vehicleEngineTorque; + return FakeCarHelper::getRange($range); + } } diff --git a/src/FakeCarHelper.php b/src/FakeCarHelper.php index fc20d0c..e567f59 100644 --- a/src/FakeCarHelper.php +++ b/src/FakeCarHelper.php @@ -118,15 +118,47 @@ public static function getWeighted(array $values, int $count = 1): string return ''; } + /** + * @param array $range + * @param int $decimals + * + * @return string + * @throws RandomException + */ + public static function getRange(array $range, int $decimals = 0): string + { + if (count($range) !== 2) { + throw new RandomException('Invalid range'); + } + + if ($range[0] > $range[1]) { + throw new RandomException('Invalid range'); + } + + if ($decimals < 0) { + throw new InvalidArgumentException('Invalid decimals'); + } + + if ($decimals > 0) { + $factor = 10 ** $decimals; + + return number_format(random_int($range[0] * $factor, $range[1] * $factor) / $factor, $decimals); + } + + return random_int($range[0], $range[1]); + } + /** * @param array $range * @param string $unit + * @param int $decimals * * @return string * @throws RandomException */ - public static function getRangeWithUnit(array $range, string $unit): string + public static function getRangeWithUnit(array $range, string $unit, int $decimals = 0): string { - return random_int($range[0], $range[1]) . ' ' . $unit; + return static::getRange($range, $decimals) . ' ' . $unit; } + } diff --git a/tests/FakeCarTest.php b/tests/FakeCarTest.php index c8fd90c..99601ee 100644 --- a/tests/FakeCarTest.php +++ b/tests/FakeCarTest.php @@ -42,23 +42,24 @@ public function getProtectedProperty($property, $object = null) return $reflection_property->getValue($object, $property); } - /** - * @throws ReflectionException - */ public function callProtectedMethod($args, $method, $object = null) { if (is_null($object)) { $object = new FakeCarDataProvider; } - $reflection = new \ReflectionClass($object); - $reflectionMethod = $reflection->getMethod($method); - $reflectionMethod->setAccessible(true); + try { + $reflection = new \ReflectionClass($object); + $reflectionMethod = $reflection->getMethod($method); + //$reflectionMethod->setAccessible(true); - return $reflectionMethod->invoke($object, ...$args); + return $reflectionMethod->invoke($object, ...$args); + } catch (Exception $e) { + return $e->getMessage(); + } } - public function testVehicle() + public function testVehicle(): void { $this->faker->seed(random_int(1, 9999)); @@ -77,7 +78,7 @@ public function testVehicle() } } - public function testVehicleArray() + public function testVehicleArray(): void { $vehicleArray = $this->faker->vehicleArray(); @@ -86,25 +87,18 @@ public function testVehicleArray() $brandsArray = (new FakeCarDataProvider)->getBrandsWithModels(); - $this->assertTrue( - in_array( - $vehicleArray['model'], - $brandsArray[$vehicleArray['brand']] - ) - ); + $this->assertContains($vehicleArray['model'], $brandsArray[$vehicleArray['brand']]); } - public function testVehicleBrand() + public function testVehicleBrand(): void { - $this->assertTrue( - array_key_exists( - $this->faker->vehicleBrand, - (new FakeCarDataProvider)->getBrandsWithModels() - ) + $this->assertArrayHasKey( + $this->faker->vehicleBrand, + (new FakeCarDataProvider)->getBrandsWithModels() ); } - public function testVehicleModel($make = null) + public function testVehicleModel($make = null): void { $this->faker->seed(random_int(1, 9999)); @@ -115,23 +109,23 @@ public function testVehicleModel($make = null) ); } - public function testVehicleRegistration() + public function testVehicleRegistration(): void { $this->assertMatchesRegularExpression('/[A-Z]{3}-[0-9]{3}/', $this->faker->vehicleRegistration()); $this->assertMatchesRegularExpression('/[A-Z]{2}-[0-9]{5}/', $this->faker->vehicleRegistration('[A-Z]{2}-[0-9]{5}')); } - public function testVehicleType() + public function testVehicleType(): void { $this->assertContains($this->faker->vehicleType, FakeCarData::$vehicleTypes); } - public function testVehicleFuelType() + public function testVehicleFuelType(): void { - $this->assertTrue(in_array($this->faker->vehicleFuelType, array_keys(FakeCarData::$vehicleFuelTypes), true)); + $this->assertContains($this->faker->vehicleFuelType, array_keys(FakeCarData::$vehicleFuelTypes)); } - public function testVehicleDoorCount() + public function testVehicleDoorCount(): void { for ($i = 0; $i<10; $i++) { $this->assertThat( @@ -145,7 +139,7 @@ public function testVehicleDoorCount() } } - public function testVehicleSeatCount() + public function testVehicleSeatCount(): void { for ($i = 0; $i<10; $i++) { $this->assertThat( @@ -159,34 +153,34 @@ public function testVehicleSeatCount() } } - public function testVehicleProperties() + public function testVehicleProperties(): void { $properties = $this->faker->vehicleProperties; - $this->assertTrue(is_array($properties)); + $this->assertIsArray($properties); $properties = $this->faker->vehicleProperties(2); - $this->assertTrue(is_array($properties)); + $this->assertIsArray($properties); $this->assertCount(2, $properties); $properties = $this->faker->vehicleProperties(5); - $this->assertTrue(is_array($properties)); + $this->assertIsArray($properties); $this->assertCount(5, $properties); //If we pass 0 we should get a random $properties = $this->faker->vehicleProperties(0); - $this->assertTrue(is_array($properties)); + $this->assertIsArray($properties); $this->assertGreaterThanOrEqual(0, count($properties)); } - public function testVehicleGearBox() + public function testVehicleGearBox(): void { - $this->assertTrue(in_array($this->faker->vehicleGearBoxType, array_keys(FakeCarData::$vehicleGearBoxType))); + $this->assertContains($this->faker->vehicleGearBoxType, array_keys(FakeCarData::$vehicleGearBoxType)); } /** * @throws Exception */ - public function testGetRandomElementsFromArray() + public function testGetRandomElementsFromArray(): void { $data = [ 'value1', @@ -227,7 +221,7 @@ public function testGetRandomElementsFromArray() /** * @throws ReflectionException */ - public function testGetWeighted() + public function testGetWeighted(): void { // NOTE: As this is based on random distribution this test might fail in extremely rare cases. $data = [ @@ -251,7 +245,7 @@ public function testGetWeighted() $this->assertEquals('', FakeCarHelper::getWeighted([])); } - public function testValidVin() + public function testValidVin(): void { //Too short $this->assertFalse($this->faker->validateVin('z2j9hhgr8Ahl1e3g')); @@ -271,23 +265,98 @@ public function testValidVin() $this->assertTrue($this->faker->validateVin('355430557Azf4u0vr')); } - public function testVinReturnsValidVin() + public function testVinReturnsValidVin(): void { $vin = $this->faker->vin(); $this->assertTrue($this->faker->validateVin($vin)); } - public function testModelYear() + public function testModelYear(): void { - $this->assertEquals($this->faker->modelYear(1980), 'A'); - $this->assertEquals($this->faker->modelYear(2000), 'Y'); - $this->assertEquals($this->faker->modelYear(2017), 'H'); - $this->assertEquals($this->faker->modelYear(2018), 'J'); - $this->assertEquals($this->faker->modelYear(2019), 'K'); + $this->assertEquals('A', $this->faker->modelYear(1980)); + $this->assertEquals('Y', $this->faker->modelYear(2000)); + $this->assertEquals('H', $this->faker->modelYear(2017)); + $this->assertEquals('J', $this->faker->modelYear(2018)); + $this->assertEquals('K', $this->faker->modelYear(2019)); } public function testTransliterate() { - $this->assertEquals($this->callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker)), 0); - $this->assertEquals($this->callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker)), 1); - $this->assertEquals($this->callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker)), 2); + $this->assertEquals(0, $this->callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker))); + $this->assertEquals(1, $this->callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker))); + $this->assertEquals(2, $this->callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker))); + } + + public function testCheckDigit() + { + $this->assertEquals('4', $this->callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker))); + $this->assertEquals('1', $this->callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker))); + $this->assertEquals('8', $this->callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker))); + } + + public function testVin() + { + $vin = $this->faker->vin(); + $this->assertMatchesRegularExpression('/[a-zA-Z0-9]{17}/', $vin); + $this->assertTrue($this->faker->validateVin($vin)); + } + + public function testEnginePower() + { + $power = $this->faker->vehicleEnginePower; + $this->assertMatchesRegularExpression('/^\d+ hp$/', $power); + $this->assertGreaterThanOrEqual(100, (int)explode(' ', $power)[0]); + $this->assertLessThanOrEqual(1500, (int)explode(' ', $power)[0]); + } + + public function testEngineTorque() + { + $torque = $this->faker->vehicleEngineTorque; + $this->assertMatchesRegularExpression('/^\d+ nm$/', $torque); + $this->assertGreaterThanOrEqual(100, (int)explode(' ', $torque)[0]); + $this->assertLessThanOrEqual(700, (int)explode(' ', $torque)[0]); + } + + public function testGetRange() + { + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRange([1, 100], 0); + $this->assertMatchesRegularExpression('/^\d+$/', $range); + $this->assertGreaterThanOrEqual(1, (int)$range); + $this->assertLessThanOrEqual(100, (int)$range); + } + + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRange([100, 150], 2); + + $this->assertMatchesRegularExpression('/^\d+\.\d+$/', $range); + $this->assertGreaterThanOrEqual(100, (int)$range); + $this->assertLessThanOrEqual(150, (int)$range); + } + } + public function testGetRangeInvalid() + { + $this->expectException('\Random\RandomException'); + FakeCarHelper::getRange([100, 50], 2); + + $this->expectException('\InvalidArgumentException'); + FakeCarHelper::getRange([100, 50], -2); + } + + public function testGetRangeWithUnit() + { + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRangeWithUnit([2065, 2450], 'l', 0); + + $this->assertMatchesRegularExpression('/^\d+ l$/', $range); + $this->assertGreaterThanOrEqual(2065, (int)$range); + $this->assertLessThanOrEqual(2450, (int)$range); + } + + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRangeWithUnit([200, 250], 'hp', 2); + + $this->assertMatchesRegularExpression('/^\d+\.\d+ hp$/', $range); + $this->assertGreaterThanOrEqual(200, (int)$range); + $this->assertLessThanOrEqual(250, (int)$range); + } } } From 2b3851a835d1ebdf5a376011555f994872d35720 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Sun, 31 Mar 2024 01:15:55 +0100 Subject: [PATCH 03/24] Remove PHP 8.1 from test matrix --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 8bf0aea..7e4ad31 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -14,7 +14,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest, macos-latest] - php: [8.1, 8.2, 8.3] + php: [8.2, 8.3] stability: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} From bbd2773c87b76f2af7c79aa3958f5e02ce05e3d8 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 19 Sep 2024 01:02:31 +0200 Subject: [PATCH 04/24] Convert test suite to Pest --- composer.json | 14 +- phpunit.xml | 10 +- tests/FakeCarDataProviderTest.php | 117 +++---- tests/FakeCarTest.php | 555 ++++++++++++++---------------- tests/Pest.php | 45 +++ tests/TestCase.php | 10 + tests/Unit/ArchTest.php | 6 + 7 files changed, 383 insertions(+), 374 deletions(-) create mode 100644 tests/Pest.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/ArchTest.php diff --git a/composer.json b/composer.json index dbd2c16..790e9d6 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,10 @@ "fakerphp/faker": "^1.10" }, "require-dev": { - "phpunit/phpunit": "^10.1", "larapack/dd": "^1.1", - "brianium/paratest": "^7.1" + "phpstan/phpstan": "^1.12", + "pestphp/pest": "^3.0", + "pestphp/pest-plugin-drift": "3.x-dev" }, "autoload": { "psr-4": { @@ -59,5 +60,12 @@ "all": [ "composer run test && composer run test-coverage && composer run phpmd && composer run codemetrics" ] - } + }, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } + }, + "minimum-stability": "beta", + "prefer-stable": true } diff --git a/phpunit.xml b/phpunit.xml index 8077277..e09676f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,19 +1,19 @@ + failOnWarning="true" + colors="true" +> tests - src diff --git a/tests/FakeCarDataProviderTest.php b/tests/FakeCarDataProviderTest.php index 72406b2..07f6b86 100644 --- a/tests/FakeCarDataProviderTest.php +++ b/tests/FakeCarDataProviderTest.php @@ -1,68 +1,61 @@ setDataProvider($fakeCarDataProvider); - $faker->addProvider($fakeCar); - - for ($i = 0; $i <= 100; $i++) { - $data = $faker->vehicleArray(); - $this->assertEquals('BMW', $data['brand']); - $this->assertEquals('BMW', $faker->vehicleBrand); - $this->assertContains($faker->vehicleModel, (BMWFakeCarData::$brandsWithModels)['BMW']); - $this->assertContains($faker->vehicleType, BMWFakeCarData::$vehicleTypes); - - $regex = '/^(?\d+) (?[a-zA-Z]+)$/'; - $this->assertMatchesRegularExpression($regex, $faker->vehicleEnginePower); - preg_match($regex, $faker->vehicleEnginePower, $matches); - $this->assertGreaterThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][0], $matches['value']); - $this->assertLessThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][1], $matches['value']); - $this->assertEquals('hp', $matches['unit']); - - $this->assertMatchesRegularExpression($regex, $faker->vehicleEngineTorque); - preg_match($regex, $faker->vehicleEngineTorque, $matches); - $this->assertGreaterThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][0], $matches['value']); - $this->assertLessThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][1], $matches['value']); - $this->assertEquals('nm', $matches['unit']); - } - } - - public function testCustomProviderClass() - { - $fakeCarDataProvider = new FerrariEnzoTestProvider(); - - $faker = (new \Faker\Factory())::create(); - $fakeCar = new \Faker\Provider\FakeCar($faker); - $fakeCar->setDataProvider($fakeCarDataProvider); - $faker->addProvider($fakeCar); - - $vehicleArray = $faker->vehicleArray(); - $this->assertEquals('Ferrari', $vehicleArray['brand']); - $this->assertEquals('Enzo', $vehicleArray['model']); - - $this->assertEquals('Ferrari Enzo', $faker->vehicle); - $this->assertEquals('Ferrari', $faker->vehicleBrand); - $this->assertEquals('Enzo', $faker->vehicleModel); - $this->assertEquals('coupe', $faker->vehicleType); - $this->assertEquals('gasoline', $faker->vehicleFuelType); - $this->assertEquals(2, $faker->vehicleDoorCount); - $this->assertEquals(2, $faker->vehicleSeatCount); - $this->assertEquals([ - 'Air condition', - 'GPS', - 'Leather seats', - ], $faker->vehicleProperties); +test('custom provider data source', function () { + $BMWCarData = new BMWFakeCarData; + $fakeCarDataProvider = new \Faker\Provider\FakeCarDataProvider($BMWCarData); + + $faker = (new \Faker\Factory())::create(); + $fakeCar = new \Faker\Provider\FakeCar($faker); + $fakeCar->setDataProvider($fakeCarDataProvider); + $faker->addProvider($fakeCar); + + for ($i = 0; $i <= 100; $i++) { + $data = $faker->vehicleArray(); + expect($data['brand'])->toEqual('BMW'); + expect($faker->vehicleBrand)->toEqual('BMW'); + expect((BMWFakeCarData::$brandsWithModels)['BMW'])->toContain($faker->vehicleModel); + expect(BMWFakeCarData::$vehicleTypes)->toContain($faker->vehicleType); + + $regex = '/^(?\d+) (?[a-zA-Z]+)$/'; + expect($faker->vehicleEnginePower)->toMatch($regex); + preg_match($regex, $faker->vehicleEnginePower, $matches); + expect($matches['value'])->toBeGreaterThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][0]); + expect($matches['value'])->toBeLessThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][1]); + expect($matches['unit'])->toEqual('hp'); + + expect($faker->vehicleEngineTorque)->toMatch($regex); + preg_match($regex, $faker->vehicleEngineTorque, $matches); + expect($matches['value'])->toBeGreaterThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][0]); + expect($matches['value'])->toBeLessThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][1]); + expect($matches['unit'])->toEqual('nm'); } -} +}); + +test('custom provider class', function () { + $fakeCarDataProvider = new FerrariEnzoTestProvider(); + + $faker = (new \Faker\Factory())::create(); + $fakeCar = new \Faker\Provider\FakeCar($faker); + $fakeCar->setDataProvider($fakeCarDataProvider); + $faker->addProvider($fakeCar); + + $vehicleArray = $faker->vehicleArray(); + expect($vehicleArray['brand'])->toEqual('Ferrari'); + expect($vehicleArray['model'])->toEqual('Enzo'); + + expect($faker->vehicle)->toEqual('Ferrari Enzo'); + expect($faker->vehicleBrand)->toEqual('Ferrari'); + expect($faker->vehicleModel)->toEqual('Enzo'); + expect($faker->vehicleType)->toEqual('coupe'); + expect($faker->vehicleFuelType)->toEqual('gasoline'); + expect($faker->vehicleDoorCount)->toEqual(2); + expect($faker->vehicleSeatCount)->toEqual(2); + expect($faker->vehicleProperties)->toEqual([ + 'Air condition', + 'GPS', + 'Leather seats', + ]); +}); diff --git a/tests/FakeCarTest.php b/tests/FakeCarTest.php index 99601ee..cb3db55 100644 --- a/tests/FakeCarTest.php +++ b/tests/FakeCarTest.php @@ -1,362 +1,309 @@ addProvider(new FakeCar($faker)); + $this->faker = $faker; +}); + +/** + * @throws ReflectionException + */ +function getProtectedProperty($property, $object = null) { - /** - * @var Generator - */ - private $faker; - - public function setUp(): void - { - $faker = Factory::create(); - $faker->addProvider(new FakeCar($faker)); - $this->faker = $faker; + if (is_null($object)) { + $object = new FakeCarDataProvider; } - /** - * @throws ReflectionException - */ - public function getProtectedProperty($property, $object = null) - { - if (is_null($object)) { - $object = new FakeCarDataProvider; - } + $reflection = new \ReflectionClass($object); + $reflection_property = $reflection->getProperty($property); + $reflection_property->setAccessible(true); - $reflection = new \ReflectionClass($object); - $reflection_property = $reflection->getProperty($property); - $reflection_property->setAccessible(true); + return $reflection_property->getValue($object, $property); +} - return $reflection_property->getValue($object, $property); +function callProtectedMethod($args, $method, $object = null) +{ + if (is_null($object)) { + $object = new FakeCarDataProvider; } - public function callProtectedMethod($args, $method, $object = null) - { - if (is_null($object)) { - $object = new FakeCarDataProvider; - } - - try { - $reflection = new \ReflectionClass($object); - $reflectionMethod = $reflection->getMethod($method); - //$reflectionMethod->setAccessible(true); + try { + $reflection = new \ReflectionClass($object); + $reflectionMethod = $reflection->getMethod($method); + //$reflectionMethod->setAccessible(true); - return $reflectionMethod->invoke($object, ...$args); - } catch (Exception $e) { - return $e->getMessage(); - } + return $reflectionMethod->invoke($object, ...$args); + } catch (Exception $e) { + return $e->getMessage(); } +} - public function testVehicle(): void - { - $this->faker->seed(random_int(1, 9999)); +test('vehicle', function () { + $this->faker->seed(random_int(1, 9999)); - $vehicleText = $this->faker->vehicle(); - $brands = (new FakeCarDataProvider)->getBrandsWithModels(); + $vehicleText = $this->faker->vehicle(); + $brands = (new FakeCarDataProvider)->getBrandsWithModels(); - foreach ($brands as $brand => $models) { - if (substr($vehicleText, 0, strlen($brand)) === $brand) { - foreach ($models as $model) { - if (substr($vehicleText, -strlen($model)) === $model) { - $this->assertStringEndsWith($model, $vehicleText); - break; - } + foreach ($brands as $brand => $models) { + if (substr($vehicleText, 0, strlen($brand)) === $brand) { + foreach ($models as $model) { + if (substr($vehicleText, -strlen($model)) === $model) { + expect($vehicleText)->toEndWith($model); + break; } } } } +}); - public function testVehicleArray(): void - { - $vehicleArray = $this->faker->vehicleArray(); +test('vehicle array', function () { + $vehicleArray = $this->faker->vehicleArray(); - $this->assertArrayHasKey('brand', $vehicleArray); - $this->assertArrayHasKey('model', $vehicleArray); + expect($vehicleArray)->toHaveKey('brand'); + expect($vehicleArray)->toHaveKey('model'); - $brandsArray = (new FakeCarDataProvider)->getBrandsWithModels(); + $brandsArray = (new FakeCarDataProvider)->getBrandsWithModels(); - $this->assertContains($vehicleArray['model'], $brandsArray[$vehicleArray['brand']]); - } + expect($brandsArray[$vehicleArray['brand']])->toContain($vehicleArray['model']); +}); - public function testVehicleBrand(): void - { - $this->assertArrayHasKey( - $this->faker->vehicleBrand, - (new FakeCarDataProvider)->getBrandsWithModels() - ); - } +test('vehicle brand', function () { + expect((new FakeCarDataProvider)->getBrandsWithModels())->toHaveKey($this->faker->vehicleBrand); +}); - public function testVehicleModel($make = null): void - { - $this->faker->seed(random_int(1, 9999)); +test('vehicle model', function () { + $this->faker->seed(random_int(1, 9999)); - $vehicleBrand = $this->faker->vehicleBrand(); + $vehicleBrand = $this->faker->vehicleBrand(); - $this->assertContains( - $this->faker->vehicleModel($vehicleBrand), ((new FakeCarDataProvider)->getBrandsWithModels())[$vehicleBrand] - ); - } + expect(((new FakeCarDataProvider)->getBrandsWithModels())[$vehicleBrand])->toContain($this->faker->vehicleModel($vehicleBrand)); +}); - public function testVehicleRegistration(): void - { - $this->assertMatchesRegularExpression('/[A-Z]{3}-[0-9]{3}/', $this->faker->vehicleRegistration()); - $this->assertMatchesRegularExpression('/[A-Z]{2}-[0-9]{5}/', $this->faker->vehicleRegistration('[A-Z]{2}-[0-9]{5}')); - } +test('vehicle registration', function () { + expect($this->faker->vehicleRegistration())->toMatch('/[A-Z]{3}-[0-9]{3}/'); + expect($this->faker->vehicleRegistration('[A-Z]{2}-[0-9]{5}'))->toMatch('/[A-Z]{2}-[0-9]{5}/'); +}); - public function testVehicleType(): void - { - $this->assertContains($this->faker->vehicleType, FakeCarData::$vehicleTypes); - } +test('vehicle type', function () { + expect(FakeCarData::$vehicleTypes)->toContain($this->faker->vehicleType); +}); - public function testVehicleFuelType(): void - { - $this->assertContains($this->faker->vehicleFuelType, array_keys(FakeCarData::$vehicleFuelTypes)); - } +test('vehicle fuel type', function () { + expect(array_keys(FakeCarData::$vehicleFuelTypes))->toContain($this->faker->vehicleFuelType); +}); - public function testVehicleDoorCount(): void - { - for ($i = 0; $i<10; $i++) { - $this->assertThat( - $this->faker->vehicleDoorCount, - $this->logicalAnd( - $this->isType('int'), - $this->greaterThanOrEqual(2), - $this->lessThanOrEqual(6) - ) - ); - } - } - - public function testVehicleSeatCount(): void - { - for ($i = 0; $i<10; $i++) { - $this->assertThat( - $this->faker->vehicleSeatCount, - $this->logicalAnd( - $this->isType('int'), - $this->greaterThanOrEqual(1), - $this->lessThanOrEqual(9) - ) - ); - } +test('vehicle door count', function () { + for ($i = 0; $i<10; $i++) { + expect($this->logicalAnd( + $this->isType('int'), + $this->greaterThanOrEqual(2), + $this->lessThanOrEqual(6) + ))->toMatchConstraint($this->faker->vehicleDoorCount); } - - public function testVehicleProperties(): void - { - $properties = $this->faker->vehicleProperties; - $this->assertIsArray($properties); - - $properties = $this->faker->vehicleProperties(2); - $this->assertIsArray($properties); - $this->assertCount(2, $properties); - - $properties = $this->faker->vehicleProperties(5); - $this->assertIsArray($properties); - $this->assertCount(5, $properties); - - //If we pass 0 we should get a random - $properties = $this->faker->vehicleProperties(0); - $this->assertIsArray($properties); - $this->assertGreaterThanOrEqual(0, count($properties)); - } - - public function testVehicleGearBox(): void - { - $this->assertContains($this->faker->vehicleGearBoxType, array_keys(FakeCarData::$vehicleGearBoxType)); +}); + +test('vehicle seat count', function () { + for ($i = 0; $i<10; $i++) { + expect($this->logicalAnd( + $this->isType('int'), + $this->greaterThanOrEqual(1), + $this->lessThanOrEqual(9) + ))->toMatchConstraint($this->faker->vehicleSeatCount); } - - /** - * @throws Exception - */ - public function testGetRandomElementsFromArray(): void - { - $data = [ - 'value1', - 'value2', - 'value3', - 'value4', - 'value5', - 'value6', - 'value7', - 'value8', - 'value9', - 'value10', - ]; - - $this->assertCount(1, FakeCarHelper::getRandomElementsFromArray($data, 1)); - $this->assertCount(3, FakeCarHelper::getRandomElementsFromArray($data, 3)); - $this->assertCount(6, FakeCarHelper::getRandomElementsFromArray($data, 6)); - $this->assertCount(10, FakeCarHelper::getRandomElementsFromArray($data, 10)); - $this->assertEquals([], FakeCarHelper::getRandomElementsFromArray($data, 0)); - - for ($i = 0; $i<50; $i++) { - $result6 = FakeCarHelper::getRandomElementsFromArray($data, null); - - $this->assertGreaterThanOrEqual(0, count($result6)); - $this->assertLessThanOrEqual(10, count($result6)); - - foreach ($result6 as $r) { - $this->assertContains($r, $data); - } +}); + +test('vehicle properties', function () { + $properties = $this->faker->vehicleProperties; + expect($properties)->toBeArray(); + + $properties = $this->faker->vehicleProperties(2); + expect($properties)->toBeArray(); + expect($properties)->toHaveCount(2); + + $properties = $this->faker->vehicleProperties(5); + expect($properties)->toBeArray(); + expect($properties)->toHaveCount(5); + + //If we pass 0 we should get a random + $properties = $this->faker->vehicleProperties(0); + expect($properties)->toBeArray(); + expect(count($properties))->toBeGreaterThanOrEqual(0); +}); + +test('vehicle gear box', function () { + expect(array_keys(FakeCarData::$vehicleGearBoxType))->toContain($this->faker->vehicleGearBoxType); +}); + +test('get random elements from array', function () { + $data = [ + 'value1', + 'value2', + 'value3', + 'value4', + 'value5', + 'value6', + 'value7', + 'value8', + 'value9', + 'value10', + ]; + + expect(FakeCarHelper::getRandomElementsFromArray($data, 1))->toHaveCount(1); + expect(FakeCarHelper::getRandomElementsFromArray($data, 3))->toHaveCount(3); + expect(FakeCarHelper::getRandomElementsFromArray($data, 6))->toHaveCount(6); + expect(FakeCarHelper::getRandomElementsFromArray($data, 10))->toHaveCount(10); + expect(FakeCarHelper::getRandomElementsFromArray($data, 0))->toEqual([]); + + for ($i = 0; $i<50; $i++) { + $result6 = FakeCarHelper::getRandomElementsFromArray($data, null); + + expect(count($result6))->toBeGreaterThanOrEqual(0); + expect(count($result6))->toBeLessThanOrEqual(10); + + foreach ($result6 as $r) { + expect($data)->toContain($r); } - - $this->expectException(\InvalidArgumentException::class); - - FakeCarHelper::getRandomElementsFromArray($data, 20); } + $this->expectException(\InvalidArgumentException::class); - /** - * @throws ReflectionException - */ - public function testGetWeighted(): void - { - // NOTE: As this is based on random distribution this test might fail in extremely rare cases. - $data = [ - 'key1' => 100, - 'key2' => 10, - 'key3' => 1, - ]; + FakeCarHelper::getRandomElementsFromArray($data, 20); +}); - for($x = 0; $x<10; $x++) { - $result = array_fill_keys(array_keys($data), 0); +test('get weighted', function () { + // NOTE: As this is based on random distribution this test might fail in extremely rare cases. + $data = [ + 'key1' => 100, + 'key2' => 10, + 'key3' => 1, + ]; - for ($i = 0; $i<1000; $i++) { - $result[FakeCarHelper::getWeighted($data)]++; - } + for($x = 0; $x<10; $x++) { + $result = array_fill_keys(array_keys($data), 0); - $this->assertGreaterThan($result['key2'], $result['key1']); - $this->assertGreaterThan($result['key3'], $result['key2']); - $this->assertGreaterThan($result['key3'], $result['key1']); + for ($i = 0; $i<1000; $i++) { + $result[FakeCarHelper::getWeighted($data)]++; } - $this->assertEquals('', FakeCarHelper::getWeighted([])); - } - - public function testValidVin(): void - { - //Too short - $this->assertFalse($this->faker->validateVin('z2j9hhgr8Ahl1e3g')); - //Too long - $this->assertFalse($this->faker->validateVin('az2j9hhgr8Ahl1e3gs')); - //Invalid check digit - $this->assertFalse($this->faker->validateVin('z2j9hhgr2Ahl1e3gs')); - //Invalid - $this->assertFalse($this->faker->validateVin('z2j9hhgr8Ahl1e3gd')); - - // Valid VINs - $this->assertTrue($this->faker->validateVin('z2j9hhgr8Ahl1e3gs')); - $this->assertTrue($this->faker->validateVin('n7u30vns7Ajsrb1nc')); - $this->assertTrue($this->faker->validateVin('3julknxb0A06hj41x')); - $this->assertTrue($this->faker->validateVin('yj12c8z40Aca2x6p3')); - $this->assertTrue($this->faker->validateVin('y95wf7gm1A9g7pz5z')); - $this->assertTrue($this->faker->validateVin('355430557Azf4u0vr')); - } - - public function testVinReturnsValidVin(): void - { - $vin = $this->faker->vin(); - $this->assertTrue($this->faker->validateVin($vin)); - } - public function testModelYear(): void - { - $this->assertEquals('A', $this->faker->modelYear(1980)); - $this->assertEquals('Y', $this->faker->modelYear(2000)); - $this->assertEquals('H', $this->faker->modelYear(2017)); - $this->assertEquals('J', $this->faker->modelYear(2018)); - $this->assertEquals('K', $this->faker->modelYear(2019)); - } - public function testTransliterate() - { - $this->assertEquals(0, $this->callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker))); - $this->assertEquals(1, $this->callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker))); - $this->assertEquals(2, $this->callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker))); - } - - public function testCheckDigit() - { - $this->assertEquals('4', $this->callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker))); - $this->assertEquals('1', $this->callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker))); - $this->assertEquals('8', $this->callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker))); + expect($result['key1'])->toBeGreaterThan($result['key2']); + expect($result['key2'])->toBeGreaterThan($result['key3']); + expect($result['key1'])->toBeGreaterThan($result['key3']); } - public function testVin() - { - $vin = $this->faker->vin(); - $this->assertMatchesRegularExpression('/[a-zA-Z0-9]{17}/', $vin); - $this->assertTrue($this->faker->validateVin($vin)); + expect(FakeCarHelper::getWeighted([]))->toEqual(''); +}); + +test('valid vin', function () { + //Too short + expect($this->faker->validateVin('z2j9hhgr8Ahl1e3g'))->toBeFalse(); + + //Too long + expect($this->faker->validateVin('az2j9hhgr8Ahl1e3gs'))->toBeFalse(); + + //Invalid check digit + expect($this->faker->validateVin('z2j9hhgr2Ahl1e3gs'))->toBeFalse(); + + //Invalid + expect($this->faker->validateVin('z2j9hhgr8Ahl1e3gd'))->toBeFalse(); + + // Valid VINs + expect($this->faker->validateVin('z2j9hhgr8Ahl1e3gs'))->toBeTrue(); + expect($this->faker->validateVin('n7u30vns7Ajsrb1nc'))->toBeTrue(); + expect($this->faker->validateVin('3julknxb0A06hj41x'))->toBeTrue(); + expect($this->faker->validateVin('yj12c8z40Aca2x6p3'))->toBeTrue(); + expect($this->faker->validateVin('y95wf7gm1A9g7pz5z'))->toBeTrue(); + expect($this->faker->validateVin('355430557Azf4u0vr'))->toBeTrue(); +}); + +test('vin returns valid vin', function () { + $vin = $this->faker->vin(); + expect($this->faker->validateVin($vin))->toBeTrue(); +}); +test('model year', function () { + expect($this->faker->modelYear(1980))->toEqual('A'); + expect($this->faker->modelYear(2000))->toEqual('Y'); + expect($this->faker->modelYear(2017))->toEqual('H'); + expect($this->faker->modelYear(2018))->toEqual('J'); + expect($this->faker->modelYear(2019))->toEqual('K'); +}); +test('transliterate', function () { + expect(callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker)))->toEqual(0); + expect(callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker)))->toEqual(1); + expect(callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker)))->toEqual(2); +}); + +test('check digit', function () { + expect(callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker)))->toEqual('4'); + expect(callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker)))->toEqual('1'); + expect(callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker)))->toEqual('8'); +}); + +test('vin', function () { + $vin = $this->faker->vin(); + expect($vin)->toMatch('/[a-zA-Z0-9]{17}/'); + expect($this->faker->validateVin($vin))->toBeTrue(); +}); + +test('engine power', function () { + $power = $this->faker->vehicleEnginePower; + expect($power)->toMatch('/^\d+ hp$/'); + expect((int)explode(' ', $power)[0])->toBeGreaterThanOrEqual(100); + expect((int)explode(' ', $power)[0])->toBeLessThanOrEqual(1500); +}); + +test('engine torque', function () { + $torque = $this->faker->vehicleEngineTorque; + expect($torque)->toMatch('/^\d+ nm$/'); + expect((int)explode(' ', $torque)[0])->toBeGreaterThanOrEqual(100); + expect((int)explode(' ', $torque)[0])->toBeLessThanOrEqual(700); +}); + +test('get range', function () { + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRange([1, 100], 0); + expect($range)->toMatch('/^\d+$/'); + expect((int)$range)->toBeGreaterThanOrEqual(1); + expect((int)$range)->toBeLessThanOrEqual(100); } - public function testEnginePower() - { - $power = $this->faker->vehicleEnginePower; - $this->assertMatchesRegularExpression('/^\d+ hp$/', $power); - $this->assertGreaterThanOrEqual(100, (int)explode(' ', $power)[0]); - $this->assertLessThanOrEqual(1500, (int)explode(' ', $power)[0]); - } + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRange([100, 150], 2); - public function testEngineTorque() - { - $torque = $this->faker->vehicleEngineTorque; - $this->assertMatchesRegularExpression('/^\d+ nm$/', $torque); - $this->assertGreaterThanOrEqual(100, (int)explode(' ', $torque)[0]); - $this->assertLessThanOrEqual(700, (int)explode(' ', $torque)[0]); + expect($range)->toMatch('/^\d+\.\d+$/'); + expect((int)$range)->toBeGreaterThanOrEqual(100); + expect((int)$range)->toBeLessThanOrEqual(150); } - - public function testGetRange() - { - for($x = 0; $x<100; $x++) { - $range = FakeCarHelper::getRange([1, 100], 0); - $this->assertMatchesRegularExpression('/^\d+$/', $range); - $this->assertGreaterThanOrEqual(1, (int)$range); - $this->assertLessThanOrEqual(100, (int)$range); - } - - for($x = 0; $x<100; $x++) { - $range = FakeCarHelper::getRange([100, 150], 2); - - $this->assertMatchesRegularExpression('/^\d+\.\d+$/', $range); - $this->assertGreaterThanOrEqual(100, (int)$range); - $this->assertLessThanOrEqual(150, (int)$range); - } +}); +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 () { + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRangeWithUnit([2065, 2450], 'l', 0); + + expect($range)->toMatch('/^\d+ l$/'); + expect((int)$range)->toBeGreaterThanOrEqual(2065); + expect((int)$range)->toBeLessThanOrEqual(2450); } - public function testGetRangeInvalid() - { - $this->expectException('\Random\RandomException'); - FakeCarHelper::getRange([100, 50], 2); - $this->expectException('\InvalidArgumentException'); - FakeCarHelper::getRange([100, 50], -2); - } + for($x = 0; $x<100; $x++) { + $range = FakeCarHelper::getRangeWithUnit([200, 250], 'hp', 2); - public function testGetRangeWithUnit() - { - for($x = 0; $x<100; $x++) { - $range = FakeCarHelper::getRangeWithUnit([2065, 2450], 'l', 0); - - $this->assertMatchesRegularExpression('/^\d+ l$/', $range); - $this->assertGreaterThanOrEqual(2065, (int)$range); - $this->assertLessThanOrEqual(2450, (int)$range); - } - - for($x = 0; $x<100; $x++) { - $range = FakeCarHelper::getRangeWithUnit([200, 250], 'hp', 2); - - $this->assertMatchesRegularExpression('/^\d+\.\d+ hp$/', $range); - $this->assertGreaterThanOrEqual(200, (int)$range); - $this->assertLessThanOrEqual(250, (int)$range); - } + expect($range)->toMatch('/^\d+\.\d+ hp$/'); + expect((int)$range)->toBeGreaterThanOrEqual(200); + expect((int)$range)->toBeLessThanOrEqual(250); } -} +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..fd279ad --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,45 @@ +extend(Tests\TestCase::class)->in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..cfb05b6 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,10 @@ +preset()->laravel(); +arch()->preset()->security()->ignoring('parse_str');; + +arch()->expect('dd')->not->toBeUsed(); From 1120aede42affb7eec132cfa2e3f1f0122e3bb43 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 00:44:16 +0200 Subject: [PATCH 05/24] Rewrite test suite to Pest --- tests/FakeCarDataProviderTest.php | 61 ----------- tests/Pest.php | 10 -- tests/TestCase.php | 2 +- tests/Unit/ArchTest.php | 6 - tests/Unit/FakeCarDataProviderTest.php | 61 +++++++++++ tests/{ => Unit}/FakeCarTest.php | 146 +++++++++++++------------ 6 files changed, 140 insertions(+), 146 deletions(-) delete mode 100644 tests/FakeCarDataProviderTest.php delete mode 100644 tests/Unit/ArchTest.php create mode 100644 tests/Unit/FakeCarDataProviderTest.php rename tests/{ => Unit}/FakeCarTest.php (61%) diff --git a/tests/FakeCarDataProviderTest.php b/tests/FakeCarDataProviderTest.php deleted file mode 100644 index 07f6b86..0000000 --- a/tests/FakeCarDataProviderTest.php +++ /dev/null @@ -1,61 +0,0 @@ -setDataProvider($fakeCarDataProvider); - $faker->addProvider($fakeCar); - - for ($i = 0; $i <= 100; $i++) { - $data = $faker->vehicleArray(); - expect($data['brand'])->toEqual('BMW'); - expect($faker->vehicleBrand)->toEqual('BMW'); - expect((BMWFakeCarData::$brandsWithModels)['BMW'])->toContain($faker->vehicleModel); - expect(BMWFakeCarData::$vehicleTypes)->toContain($faker->vehicleType); - - $regex = '/^(?\d+) (?[a-zA-Z]+)$/'; - expect($faker->vehicleEnginePower)->toMatch($regex); - preg_match($regex, $faker->vehicleEnginePower, $matches); - expect($matches['value'])->toBeGreaterThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][0]); - expect($matches['value'])->toBeLessThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][1]); - expect($matches['unit'])->toEqual('hp'); - - expect($faker->vehicleEngineTorque)->toMatch($regex); - preg_match($regex, $faker->vehicleEngineTorque, $matches); - expect($matches['value'])->toBeGreaterThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][0]); - expect($matches['value'])->toBeLessThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][1]); - expect($matches['unit'])->toEqual('nm'); - } -}); - -test('custom provider class', function () { - $fakeCarDataProvider = new FerrariEnzoTestProvider(); - - $faker = (new \Faker\Factory())::create(); - $fakeCar = new \Faker\Provider\FakeCar($faker); - $fakeCar->setDataProvider($fakeCarDataProvider); - $faker->addProvider($fakeCar); - - $vehicleArray = $faker->vehicleArray(); - expect($vehicleArray['brand'])->toEqual('Ferrari'); - expect($vehicleArray['model'])->toEqual('Enzo'); - - expect($faker->vehicle)->toEqual('Ferrari Enzo'); - expect($faker->vehicleBrand)->toEqual('Ferrari'); - expect($faker->vehicleModel)->toEqual('Enzo'); - expect($faker->vehicleType)->toEqual('coupe'); - expect($faker->vehicleFuelType)->toEqual('gasoline'); - expect($faker->vehicleDoorCount)->toEqual(2); - expect($faker->vehicleSeatCount)->toEqual(2); - expect($faker->vehicleProperties)->toEqual([ - 'Air condition', - 'GPS', - 'Leather seats', - ]); -}); diff --git a/tests/Pest.php b/tests/Pest.php index fd279ad..fdb64bb 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -11,8 +11,6 @@ | */ -// pest()->extend(Tests\TestCase::class)->in('Feature'); - /* |-------------------------------------------------------------------------- | Expectations @@ -24,9 +22,6 @@ | */ -expect()->extend('toBeOne', function () { - return $this->toBe(1); -}); /* |-------------------------------------------------------------------------- @@ -38,8 +33,3 @@ | global functions to help you to reduce the number of lines of code in your test files. | */ - -function something() -{ - // .. -} diff --git a/tests/TestCase.php b/tests/TestCase.php index cfb05b6..46d8de3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,6 +1,6 @@ preset()->laravel(); -arch()->preset()->security()->ignoring('parse_str');; - -arch()->expect('dd')->not->toBeUsed(); diff --git a/tests/Unit/FakeCarDataProviderTest.php b/tests/Unit/FakeCarDataProviderTest.php new file mode 100644 index 0000000..be606b4 --- /dev/null +++ b/tests/Unit/FakeCarDataProviderTest.php @@ -0,0 +1,61 @@ +setDataProvider($fakeCarDataProvider); + $faker->addProvider($fakeCar); + + for ($i = 0; $i <= 100; $i++) { + $data = $faker->vehicleArray(); + expect($data['brand'])->toEqual('BMW') + ->and($faker->vehicleBrand())->toEqual('BMW') + ->and((BMWFakeCarData::$brandsWithModels)['BMW'])->toContain($faker->vehicleModel()) + ->and(BMWFakeCarData::$vehicleTypes)->toContain($faker->vehicleType()); + + $regex = '/^(?\d+) (?[a-zA-Z]+)$/'; + expect($faker->vehicleEnginePower())->toMatch($regex); + preg_match($regex, $faker->vehicleEnginePower(), $matches); + expect($matches['value'])->toBeGreaterThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][0]) + ->and($matches['value'])->toBeLessThanOrEqual(BMWFakeCarData::$vehicleEnginePower['range'][1]) + ->and($matches['unit'])->toEqual('hp') + ->and($faker->vehicleEngineTorque())->toMatch($regex); + + preg_match($regex, $faker->vehicleEngineTorque(), $matches); + expect($matches['value'])->toBeGreaterThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][0]) + ->and($matches['value'])->toBeLessThanOrEqual(BMWFakeCarData::$vehicleEngineTorque['range'][1]) + ->and($matches['unit'])->toEqual('nm'); + } +}); + +test('custom provider class', function () { + $fakeCarDataProvider = new FerrariEnzoTestProvider(); + + $faker = (new \Faker\Factory())::create(); + $fakeCar = new \Faker\Provider\FakeCar($faker); + $fakeCar->setDataProvider($fakeCarDataProvider); + $faker->addProvider($fakeCar); + + $vehicleArray = $faker->vehicleArray(); + expect($vehicleArray['brand'])->toEqual('Ferrari') + ->and($vehicleArray['model'])->toEqual('Enzo') + ->and($faker->vehicle())->toEqual('Ferrari Enzo') + ->and($faker->vehicleBrand())->toEqual('Ferrari') + ->and($faker->vehicleModel())->toEqual('Enzo') + ->and($faker->vehicleType())->toEqual('coupe') + ->and($faker->vehicleFuelType())->toEqual('gasoline') + ->and($faker->vehicleDoorCount())->toEqual(2) + ->and($faker->vehicleSeatCount())->toEqual(2) + ->and($faker->vehicleProperties())->toEqual([ + 'Air condition', + 'GPS', + 'Leather seats', + ]); + +}); diff --git a/tests/FakeCarTest.php b/tests/Unit/FakeCarTest.php similarity index 61% rename from tests/FakeCarTest.php rename to tests/Unit/FakeCarTest.php index cb3db55..bd6dbec 100644 --- a/tests/FakeCarTest.php +++ b/tests/Unit/FakeCarTest.php @@ -76,7 +76,7 @@ function callProtectedMethod($args, $method, $object = null) }); test('vehicle brand', function () { - expect((new FakeCarDataProvider)->getBrandsWithModels())->toHaveKey($this->faker->vehicleBrand); + expect((new FakeCarDataProvider)->getBrandsWithModels())->toHaveKey($this->faker->vehicleBrand()); }); test('vehicle model', function () { @@ -93,35 +93,49 @@ function callProtectedMethod($args, $method, $object = null) }); test('vehicle type', function () { - expect(FakeCarData::$vehicleTypes)->toContain($this->faker->vehicleType); + expect(FakeCarData::$vehicleTypes)->toContain($this->faker->vehicleType()); }); test('vehicle fuel type', function () { - expect(array_keys(FakeCarData::$vehicleFuelTypes))->toContain($this->faker->vehicleFuelType); + expect(array_keys(FakeCarData::$vehicleFuelTypes))->toContain($this->faker->vehicleFuelType()); }); test('vehicle door count', function () { for ($i = 0; $i<10; $i++) { + + expect($this->faker->vehicleSeatCount())->toBeGreaterThanOrEqual(1) + ->and($this->faker->vehicleSeatCount())->toBeLessThanOrEqual(9) + ->and($this->faker->vehicleSeatCount())->toBeInt(); + + /* expect($this->logicalAnd( $this->isType('int'), $this->greaterThanOrEqual(2), $this->lessThanOrEqual(6) ))->toMatchConstraint($this->faker->vehicleDoorCount); + */ } }); test('vehicle seat count', function () { for ($i = 0; $i<10; $i++) { + + expect($this->faker->vehicleSeatCount())->toBeGreaterThanOrEqual(1) + ->and($this->faker->vehicleSeatCount())->toBeLessThanOrEqual(9) + ->and($this->faker->vehicleSeatCount())->toBeInt(); + + /* expect($this->logicalAnd( $this->isType('int'), $this->greaterThanOrEqual(1), $this->lessThanOrEqual(9) - ))->toMatchConstraint($this->faker->vehicleSeatCount); + ))->toBe($this->faker->vehicleSeatCount); + */ } }); test('vehicle properties', function () { - $properties = $this->faker->vehicleProperties; + $properties = $this->faker->vehicleProperties(); expect($properties)->toBeArray(); $properties = $this->faker->vehicleProperties(2); @@ -139,10 +153,10 @@ function callProtectedMethod($args, $method, $object = null) }); test('vehicle gear box', function () { - expect(array_keys(FakeCarData::$vehicleGearBoxType))->toContain($this->faker->vehicleGearBoxType); + expect(array_keys(FakeCarData::$vehicleGearBoxType))->toContain($this->faker->vehicleGearBoxType()); }); -test('get random elements from array', function () { +test( 'get random elements from array', function () { $data = [ 'value1', 'value2', @@ -156,17 +170,17 @@ function callProtectedMethod($args, $method, $object = null) 'value10', ]; - expect(FakeCarHelper::getRandomElementsFromArray($data, 1))->toHaveCount(1); - expect(FakeCarHelper::getRandomElementsFromArray($data, 3))->toHaveCount(3); - expect(FakeCarHelper::getRandomElementsFromArray($data, 6))->toHaveCount(6); - expect(FakeCarHelper::getRandomElementsFromArray($data, 10))->toHaveCount(10); - expect(FakeCarHelper::getRandomElementsFromArray($data, 0))->toEqual([]); + expect(FakeCarHelper::getRandomElementsFromArray($data, 1))->toHaveCount(1) + ->and(FakeCarHelper::getRandomElementsFromArray($data, 3))->toHaveCount(3) + ->and(FakeCarHelper::getRandomElementsFromArray($data, 6))->toHaveCount(6) + ->and(FakeCarHelper::getRandomElementsFromArray($data, 10))->toHaveCount(10) + ->and(FakeCarHelper::getRandomElementsFromArray($data, 0))->toEqual([]); for ($i = 0; $i<50; $i++) { $result6 = FakeCarHelper::getRandomElementsFromArray($data, null); - expect(count($result6))->toBeGreaterThanOrEqual(0); - expect(count($result6))->toBeLessThanOrEqual(10); + expect(count($result6))->toBeGreaterThanOrEqual(0) + ->and(count($result6))->toBeLessThanOrEqual(10); foreach ($result6 as $r) { expect($data)->toContain($r); @@ -193,9 +207,9 @@ function callProtectedMethod($args, $method, $object = null) $result[FakeCarHelper::getWeighted($data)]++; } - expect($result['key1'])->toBeGreaterThan($result['key2']); - expect($result['key2'])->toBeGreaterThan($result['key3']); - expect($result['key1'])->toBeGreaterThan($result['key3']); + expect($result['key1'])->toBeGreaterThan($result['key2']) + ->and($result['key2'])->toBeGreaterThan($result['key3']) + ->and($result['key1'])->toBeGreaterThan($result['key3']); } expect(FakeCarHelper::getWeighted([]))->toEqual(''); @@ -203,24 +217,20 @@ function callProtectedMethod($args, $method, $object = null) test('valid vin', function () { //Too short - expect($this->faker->validateVin('z2j9hhgr8Ahl1e3g'))->toBeFalse(); - - //Too long - expect($this->faker->validateVin('az2j9hhgr8Ahl1e3gs'))->toBeFalse(); - - //Invalid check digit - expect($this->faker->validateVin('z2j9hhgr2Ahl1e3gs'))->toBeFalse(); - - //Invalid - expect($this->faker->validateVin('z2j9hhgr8Ahl1e3gd'))->toBeFalse(); - - // Valid VINs - expect($this->faker->validateVin('z2j9hhgr8Ahl1e3gs'))->toBeTrue(); - expect($this->faker->validateVin('n7u30vns7Ajsrb1nc'))->toBeTrue(); - expect($this->faker->validateVin('3julknxb0A06hj41x'))->toBeTrue(); - expect($this->faker->validateVin('yj12c8z40Aca2x6p3'))->toBeTrue(); - expect($this->faker->validateVin('y95wf7gm1A9g7pz5z'))->toBeTrue(); - expect($this->faker->validateVin('355430557Azf4u0vr'))->toBeTrue(); + expect($this->faker->validateVin('z2j9hhgr8Ahl1e3g'))->toBeFalse() + //Too long + ->and($this->faker->validateVin('az2j9hhgr8Ahl1e3gs'))->toBeFalse() + //Invalid check digit + ->and($this->faker->validateVin('z2j9hhgr2Ahl1e3gs'))->toBeFalse() + //Invalid + ->and($this->faker->validateVin('z2j9hhgr8Ahl1e3gd'))->toBeFalse() + // Valid VINs + ->and($this->faker->validateVin('z2j9hhgr8Ahl1e3gs'))->toBeTrue() + ->and($this->faker->validateVin('n7u30vns7Ajsrb1nc'))->toBeTrue() + ->and($this->faker->validateVin('3julknxb0A06hj41x'))->toBeTrue() + ->and($this->faker->validateVin('yj12c8z40Aca2x6p3'))->toBeTrue() + ->and($this->faker->validateVin('y95wf7gm1A9g7pz5z'))->toBeTrue() + ->and($this->faker->validateVin('355430557Azf4u0vr'))->toBeTrue(); }); test('vin returns valid vin', function () { @@ -228,58 +238,58 @@ function callProtectedMethod($args, $method, $object = null) expect($this->faker->validateVin($vin))->toBeTrue(); }); test('model year', function () { - expect($this->faker->modelYear(1980))->toEqual('A'); - expect($this->faker->modelYear(2000))->toEqual('Y'); - expect($this->faker->modelYear(2017))->toEqual('H'); - expect($this->faker->modelYear(2018))->toEqual('J'); - expect($this->faker->modelYear(2019))->toEqual('K'); + expect($this->faker->modelYear(1980))->toEqual('A') + ->and($this->faker->modelYear(2000))->toEqual('Y') + ->and($this->faker->modelYear(2017))->toEqual('H') + ->and($this->faker->modelYear(2018))->toEqual('J') + ->and($this->faker->modelYear(2019))->toEqual('K'); }); test('transliterate', function () { - expect(callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker)))->toEqual(0); - expect(callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker)))->toEqual(1); - expect(callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker)))->toEqual(2); + expect(callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker)))->toEqual(0) + ->and(callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker)))->toEqual(1) + ->and(callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker)))->toEqual(2); }); test('check digit', function () { - expect(callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker)))->toEqual('4'); - expect(callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker)))->toEqual('1'); - expect(callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker)))->toEqual('8'); + expect(callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker)))->toEqual('4') + ->and(callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker)))->toEqual('1') + ->and(callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker)))->toEqual('8'); }); test('vin', function () { $vin = $this->faker->vin(); - expect($vin)->toMatch('/[a-zA-Z0-9]{17}/'); - expect($this->faker->validateVin($vin))->toBeTrue(); + 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$/'); - expect((int)explode(' ', $power)[0])->toBeGreaterThanOrEqual(100); - expect((int)explode(' ', $power)[0])->toBeLessThanOrEqual(1500); + $power = $this->faker->vehicleEnginePower(); + expect($power)->toMatch('/^\d+ hp$/') + ->and((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$/'); - expect((int)explode(' ', $torque)[0])->toBeGreaterThanOrEqual(100); - expect((int)explode(' ', $torque)[0])->toBeLessThanOrEqual(700); + $torque = $this->faker->vehicleEngineTorque(); + expect($torque)->toMatch('/^\d+ nm$/') + ->and((int) explode(' ', $torque)[0])->toBeGreaterThanOrEqual(100) + ->and((int) explode(' ', $torque)[0])->toBeLessThanOrEqual(700); }); test('get range', function () { for($x = 0; $x<100; $x++) { $range = FakeCarHelper::getRange([1, 100], 0); - expect($range)->toMatch('/^\d+$/'); - expect((int)$range)->toBeGreaterThanOrEqual(1); - expect((int)$range)->toBeLessThanOrEqual(100); + expect($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); - expect($range)->toMatch('/^\d+\.\d+$/'); - expect((int)$range)->toBeGreaterThanOrEqual(100); - expect((int)$range)->toBeLessThanOrEqual(150); + expect($range)->toMatch('/^\d+\.\d+$/') + ->and((int) $range)->toBeGreaterThanOrEqual(100) + ->and((int) $range)->toBeLessThanOrEqual(150); } }); test('get range invalid', function () { @@ -294,16 +304,16 @@ function callProtectedMethod($args, $method, $object = null) for($x = 0; $x<100; $x++) { $range = FakeCarHelper::getRangeWithUnit([2065, 2450], 'l', 0); - expect($range)->toMatch('/^\d+ l$/'); - expect((int)$range)->toBeGreaterThanOrEqual(2065); - expect((int)$range)->toBeLessThanOrEqual(2450); + expect($range)->toMatch('/^\d+ l$/') + ->and((int) $range)->toBeGreaterThanOrEqual(2065) + ->and((int) $range)->toBeLessThanOrEqual(2450); } for($x = 0; $x<100; $x++) { $range = FakeCarHelper::getRangeWithUnit([200, 250], 'hp', 2); - expect($range)->toMatch('/^\d+\.\d+ hp$/'); - expect((int)$range)->toBeGreaterThanOrEqual(200); - expect((int)$range)->toBeLessThanOrEqual(250); + expect($range)->toMatch('/^\d+\.\d+ hp$/') + ->and((int) $range)->toBeGreaterThanOrEqual(200) + ->and((int) $range)->toBeLessThanOrEqual(250); } }); From f2f282a8ea97ed1ef37054b8ae3e98c93cf34fc4 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 00:45:50 +0200 Subject: [PATCH 06/24] Change to pest in test workflow --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7e4ad31..9e7f877 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -36,7 +36,7 @@ jobs: - name: Install dependencies run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/phpunit --coverage-clover=coverage.xml + run: vendor/bin/pest --coverage-clover=coverage.xml - name: Upload coverage to Codecov uses: codecov/codecov-action@v4 with: From a70a99107efa209e10b0e985a8606be9ca26a5ab Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:06:59 +0200 Subject: [PATCH 07/24] Update PHPUnit config --- phpunit.xml | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index e09676f..535ba65 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,33 +1,29 @@ - - - tests - - - - - src - - - - - - - - - - + colors="true"> + + + tests + + + + + src + + + + + + + + + + From 0323e010800e1f604bb2893d7fca53eccaaf220b Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:09:52 +0200 Subject: [PATCH 08/24] Run tests on all pushes --- .github/workflows/run-tests.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 9e7f877..b019e73 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -3,9 +3,7 @@ name: run-tests on: workflow_dispatch: push: - branches: [main] pull_request: - branches: [main] jobs: test: From f37276415f69fcf6891f47aeaa08558b59d788dd Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:13:19 +0200 Subject: [PATCH 09/24] Remove Codecov upload step --- .github/workflows/run-tests.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b019e73..9a6fc17 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -35,13 +35,3 @@ jobs: run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests run: vendor/bin/pest --coverage-clover=coverage.xml - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4 - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: ./coverage.xml - flags: tests - name: codecov-umbrella - #yml: ./codecov.yml - fail_ci_if_error: true - From 7fccb88c8066335eae3a883a3da5a40550373cfb Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:26:56 +0200 Subject: [PATCH 10/24] Run tests on all pushes # Conflicts: # .github/workflows/run-tests.yml --- .github/workflows/run-tests.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 9a6fc17..647860a 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -3,7 +3,6 @@ name: run-tests on: workflow_dispatch: push: - pull_request: jobs: test: From 12266122bdac5da7772065b47545b5b8659f540c Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:31:16 +0200 Subject: [PATCH 11/24] Add code style checks --- .github/workflows/code-style.yml | 25 +++++++++++++++++++++++++ pint.json | 11 +++++++++++ 2 files changed, 36 insertions(+) create mode 100644 .github/workflows/code-style.yml create mode 100644 pint.json diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml new file mode 100644 index 0000000..31b1aad --- /dev/null +++ b/.github/workflows/code-style.yml @@ -0,0 +1,25 @@ +name: Code style + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + phplint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v44 + + - name: Run Laravel Pint + uses: aglipanci/laravel-pint-action@latest + with: + verboseMode: true + testMode: true + configPath: ./pint.json diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..1df0ef8 --- /dev/null +++ b/pint.json @@ -0,0 +1,11 @@ +{ + "preset": "laravel", + "rules": { + "binary_operator_spaces": { + "default": "align_single_space_minimal", + "operators": { + "=>": "align_single_space_minimal" + } + } + } +} From 104dff285057d076398eb05e1daab8c054b00284 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:36:37 +0200 Subject: [PATCH 12/24] Add code check tools --- composer.json | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 790e9d6..c35d2a5 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,10 @@ }, "require-dev": { "larapack/dd": "^1.1", - "phpstan/phpstan": "^1.12", "pestphp/pest": "^3.0", - "pestphp/pest-plugin-drift": "3.x-dev" + "pestphp/pest-plugin-drift": "3.x-dev", + "phpstan/phpstan": "^1.12", + "laravel/pint": "^1.18" }, "autoload": { "psr-4": { @@ -37,11 +38,17 @@ } }, "scripts": { + "lint": [ + "composer phpstan", + "composer pint" + ], + "phpstan": "vendor/bin/phpstan analyse src --level=8", + "pint": "vendor/bin/pint", "test": [ - "./vendor/bin/phpunit -c phpunit.xml" + "./vendor/bin/pest" ], "test-coverage": [ - "./vendor/bin/phpunit -c phpunit.xml --coverage-text" + "./vendor/bin/pest --coverage-text" ], "phpmd": [ "php ./vendor/bin/phpmd src ansi ./phpmd-ruleset.xml --ignore-violations-on-exit" From 04e30e54c633f8905c8fc777994ac7bfa243d867 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:38:31 +0200 Subject: [PATCH 13/24] Apply Pint auto fixes --- src/FakeCar.php | 76 +--- src/FakeCarData.php | 401 +++++++++--------- src/FakeCarDataProvider.php | 10 +- src/FakeCarDataProviderInterface.php | 8 + src/FakeCarHelper.php | 33 +- tests/Pest.php | 1 - tests/TestProviders/BMWFakeCarData.php | 9 +- .../TestProviders/FerrariEnzoTestProvider.php | 2 +- tests/Unit/FakeCarDataProviderTest.php | 8 +- tests/Unit/FakeCarTest.php | 31 +- 10 files changed, 272 insertions(+), 307 deletions(-) diff --git a/src/FakeCar.php b/src/FakeCar.php index c78414a..ad8b549 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -7,18 +7,16 @@ class FakeCar extends \Faker\Provider\Base { - protected const EBCDIC = "0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ"; - protected const MODEL_YEAR = "ABCDEFGHJKLMNPRSTVWXY123456789"; + protected const EBCDIC = '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ'; + + protected const MODEL_YEAR = 'ABCDEFGHJKLMNPRSTVWXY123456789'; protected FakeCarDataProviderInterface $dataProvider; - /** - * @param Generator $generator - */ public function __construct(Generator $generator) { parent::__construct($generator); - $this->setDataProvider(new FakeCarDataProvider()); + $this->setDataProvider(new FakeCarDataProvider); } public function setDataProvider(FakeCarDataProviderInterface $dataProvider): void @@ -28,8 +26,6 @@ public function setDataProvider(FakeCarDataProviderInterface $dataProvider): voi /** * Get vehicle string with brand and model - * - * @return string */ public function vehicle(): string { @@ -40,8 +36,6 @@ public function vehicle(): string /** * Get vehicle with brand and model as an array - * - * @return array */ public function vehicleArray(): array { @@ -49,14 +43,12 @@ public function vehicleArray(): array return [ 'brand' => $vehicleBrand, - 'model' => $this->vehicleModel($vehicleBrand) + 'model' => $this->vehicleModel($vehicleBrand), ]; } /** * Get random vehicle brand - * - * @return string */ public function vehicleBrand(): string { @@ -66,37 +58,32 @@ public function vehicleBrand(): string /** * Get random vehicle model * - * @param string $brand Get random model from specific brand (optional) - * + * @param string $brand Get random model from specific brand (optional) * @return mixed */ - public function vehicleModel(string $brand = null): string + public function vehicleModel(?string $brand = null): string { return (string) $this->dataProvider->getVehicleModel($brand); } /** * Generate VIN - * @link https://en.wikipedia.org/wiki/Vehicle_identification_number * - * @param int $year + * @link https://en.wikipedia.org/wiki/Vehicle_identification_number * * @return mixed */ public function vin(int $year = 1980): string { $modelYear = static::modelYear($year); - $regex = "([a-hj-npr-z0-9]{8})_{$modelYear}([a-hj-npr-z0-9]{7})"; - $vin = static::regexify($regex); + $regex = "([a-hj-npr-z0-9]{8})_{$modelYear}([a-hj-npr-z0-9]{7})"; + $vin = static::regexify($regex); + return str_replace('_', self::checkDigit($vin), $vin); } /** * Get vehicle registration number - * - * @param string $regex - * - * @return string */ public function vehicleRegistration(string $regex = '[A-Z]{3}-[0-9]{3}'): string { @@ -107,7 +94,6 @@ public function vehicleRegistration(string $regex = '[A-Z]{3}-[0-9]{3}'): string /** * Get vehicle type * - * @return string * @throws Exception */ public function vehicleType(): string @@ -117,10 +103,6 @@ public function vehicleType(): string /** * Get vehicle fuel type - * - * @param int $count - * - * @return string */ public function vehicleFuelType(int $count = 1): string { @@ -130,7 +112,6 @@ public function vehicleFuelType(int $count = 1): string /** * Get vehicle door count * - * @return int * @throws Exception */ public function vehicleDoorCount(): int @@ -141,7 +122,6 @@ public function vehicleDoorCount(): int /** * Get vehicle door count * - * @return int * @throws Exception */ public function vehicleSeatCount(): int @@ -152,9 +132,7 @@ public function vehicleSeatCount(): int /** * Get an array of random vehicle properties * - * @param int $count * - * @return array * @throws Exception */ public function vehicleProperties(int $count = 0): array @@ -166,6 +144,7 @@ public function vehicleProperties(int $count = 0): array * Get random vehicle gearbox type * * @return mixed + * * @throws Exception */ public function vehicleGearBoxType(): string @@ -177,6 +156,7 @@ public function vehicleGearBoxType(): string * Get random vehicle gearbox type without unit * * @return mixed + * * @throws Exception */ public function vehicleGearBoxTypeValue(): string @@ -184,11 +164,11 @@ public function vehicleGearBoxTypeValue(): string return $this->dataProvider->getVehicleGearBoxType(); } - /** * Get engine torque * * @return mixed + * * @throws Exception */ public function vehicleEngineTorque(): string @@ -200,6 +180,7 @@ public function vehicleEngineTorque(): string * Get engine torque without unit * * @return mixed + * * @throws Exception */ public function vehicleEngineTorqueValue(): string @@ -211,6 +192,7 @@ public function vehicleEngineTorqueValue(): string * Get engine power (horsepower or kilowatts) * * @return mixed + * * @throws Exception */ public function vehicleEnginePower(): string @@ -222,6 +204,7 @@ public function vehicleEnginePower(): string * Get engine power without unit * * @return mixed + * * @throws Exception */ public function vehicleEnginePowerValue(): string @@ -240,22 +223,11 @@ public function isSupported($method): bool throw new \RuntimeException('Method not supported be data provider. Please implement '.$method.'() in your data provider.'); } - - /** - * @param int $year - * - * @return string - */ public static function modelYear(int $year = 1980): string { return substr(self::MODEL_YEAR, ($year - 1980) % 30, 1); } - /** - * @param string $character - * - * @return string - */ private static function transliterate(string $character): string { return stripos(self::EBCDIC, $character) % 10; @@ -268,21 +240,17 @@ private static function transliterate(string $character): string */ private static function checkDigit(string $vin): string { - $map = "0123456789X"; - $weights = "8765432X098765432"; - $sum = 0; - for ($i=0; $i < 17; $i++) { + $map = '0123456789X'; + $weights = '8765432X098765432'; + $sum = 0; + for ($i = 0; $i < 17; $i++) { $sum += self::transliterate(substr($vin, $i, 1)) * stripos($map, $weights[$i]); } + return $map[$sum % 11]; } - /** - * @param $vin - * - * @return bool - */ public static function validateVin(string $vin): bool { if (strlen($vin) !== 17) { diff --git a/src/FakeCarData.php b/src/FakeCarData.php index 2cffd10..0e2b607 100644 --- a/src/FakeCarData.php +++ b/src/FakeCarData.php @@ -6,81 +6,81 @@ class FakeCarData { public static $brandsWithModels = [ 'Abarth' => [ - 'Fiat 595' + 'Fiat 595', ], 'Acura' => [ 'CL', 'Integra', 'MDX', 'NSX', 'RL', 'RSX', 'TL', 'TSX', 'Legend', 'RDX', 'SLX', 'Vigor', 'ZDX', 'EL', - 'ILX', 'RLX', 'TLX' + 'ILX', 'RLX', 'TLX', ], 'Adler' => [ - 'Trumpf', 'Stromform' + 'Trumpf', 'Stromform', ], 'Aero' => [ - '30' + '30', ], 'Aixam' => [ - '400', '500', 'Scouty', 'City' + '400', '500', 'Scouty', 'City', ], 'Alfa Romeo' => [ '145', '146', '147', '155', '156', '164', '166', '33', '75', '90', 'Alfasud', 'Alfetta', 'Arna', 'Giulietta', 'Gold Cloverleaf', 'GTV', 'Spider', 'Sprint', 'SZ', 'GT', 'Imola', '1333', 'Das', 'AR', 'Giulia', 'GTA', '2600', 'Montreal', '159', 'Brera', '169', '149', 'Junior', 'Mito', 'Crosswagon', - '6 (119)', '4C', 'Stelvio' + '6 (119)', '4C', 'Stelvio', ], 'Alpine' => [ - 'A110', 'A310', 'A610' + 'A110', 'A310', 'A610', ], 'Altamarea' => [ - '2E' + '2E', ], 'Aro' => [ - '10', '24', 'Spartana', '461', '104', '244', '243', '240', '245', '246' + '10', '24', 'Spartana', '461', '104', '244', '243', '240', '245', '246', ], 'Artega' => [ - 'GT' + 'GT', ], 'Asia' => [ - 'Rocsta', 'Topic', 'Towner', 'Cosmos' + 'Rocsta', 'Topic', 'Towner', 'Cosmos', ], 'Aston Martin' => [ 'DB7', 'Lagonda', 'V8', 'Vanquish', 'Vantage', 'Virage', 'Volante', 'V12', 'DB9', 'Bulldog', 'Tick', - 'Tickford Capri', 'Zagato', 'V8 Vantage', 'DBS', 'Rapide', 'Cygnet', 'DB11' + 'Tickford Capri', 'Zagato', 'V8 Vantage', 'DBS', 'Rapide', 'Cygnet', 'DB11', ], 'Audi' => [ '100', '200', '80', '90', 'A2', 'A3', 'A4', 'A6', 'A8', 'RS6', 'RS4', 'S3', 'S4', 'S6', 'S8', 'V8', 'TT', 'Q7', 'A5', 'R8', 'S5', 'Q5', 'TTS', 'A4 Allroad', 'A6 Allroad', 'S2', 'A1', 'A7', 'RS5', 'DKW', 'TT RS', 'TT (все)', 'RS3', 'Q3', 'S7 Sportback', 'SQ', '50', 'RS7', 'RS 4 Avant', 'RS Q3', 'SQ5', 'Q7 E-tron', - 'A3 Sportback E-tron', 'SQ7', 'Q2' + 'A3 Sportback E-tron', 'SQ7', 'Q2', ], 'Austin' => [ 'Allegro', 'Ambassador', 'Maestro', 'Maxi', 'Maxi 2', 'Metro', 'Mini', 'Mini Classic', 'Montego', - 'Princess', 'Mini MK', 'Montego Kombi', 'Princess 2', 'Rover', 'FX' + 'Princess', 'Mini MK', 'Montego Kombi', 'Princess 2', 'Rover', 'FX', ], 'Austin-Healey' => [ - '3000' + '3000', ], 'Autobianchi' => [ - 'A 112' + 'A 112', ], 'Barkas (Баркас)' => [ - 'B1000', '1001', 'VEB', '1990' + 'B1000', '1001', 'VEB', '1990', ], 'Beijing' => [ - 'BJ 2020', 'Land King', 'BJ 2021', 'BW4Y' + 'BJ 2020', 'Land King', 'BJ 2021', 'BW4Y', ], 'Bentley' => [ 'Arnage', 'Azure', 'Brooklands', 'Continental', 'Corniche', 'Eight', 'Mulsanne', 'Series II', 'Turbo R', 'Turbo RT', 'T 2', 'T 1', 'S 2', 'S 1', 'Mark VI', 'Speed 8', 'Continental Supersports', 'Flying Spur', - 'Bentayga', 'Flying Spur V8', 'Continental GT V8', 'Continental GT V8 S' + 'Bentayga', 'Flying Spur V8', 'Continental GT V8', 'Continental GT V8 S', ], 'Bertone' => [ - 'Freeclimber' + 'Freeclimber', ], 'Bio Auto' => [ - 'evA-5', 'evA-2', 'evA-4' + 'evA-5', 'evA-2', 'evA-4', ], 'Blonell' => [ - 'TF 2000 MK1' + 'TF 2000 MK1', ], 'BMW' => [ '8 Series', 'M1', 'X5', 'Z1', 'Z3', 'Z4', 'Z8', 'Alpina', 'E', 'X3', 'M', 'X6', '1 Series', '5 Series', @@ -90,27 +90,27 @@ class FakeCarData '550', '628', '630', '633', '635', '645', '650', '640', '760', '735', '732', '725', 'X series', 'X8', '340', 'RR', '1 Series М', '321', '315', '6 Series Gran Coupe', 'X2', '4 Series', '428', '435', '420', '2 Series', '3 Series GT', 'X4', '4 Series Gran Coupe', '326', 'I8', '5 Series GT', 'I3', 'M2', 'M4', - 'Neue Klasse', '1602', 'Active Hybrid 7', '2002', '2000', 'F10', 'X7', '128', '6 Series GT' + 'Neue Klasse', '1602', 'Active Hybrid 7', '2002', '2000', 'F10', 'X7', '128', '6 Series GT', ], 'Bristol' => [ - '412', '603', 'Beaufighter', 'Blenheim', 'Brigand', 'Britannia', 'Fighter', 'Speedster' + '412', '603', 'Beaufighter', 'Blenheim', 'Brigand', 'Britannia', 'Fighter', 'Speedster', ], 'Bugatti' => [ - 'EB 110', 'EB 112', 'Veyron', 'Galibier', 'Chiron' + 'EB 110', 'EB 112', 'Veyron', 'Galibier', 'Chiron', ], 'Buick' => [ 'Century', 'GL 8', 'LaCrosse', 'LE Sabre', 'Park Avenue', 'Rainer', 'Reatta', 'Regal', 'Rendezvous', 'Riviera', 'Enclave', 'LuCerne', 'Enclave USA', 'LaCrosse USA', 'Skylark', 'Wildcat', 'Roadmaster', 'Special', 'Limitet', 'Encore', 'Super', 'Electra', 'Skyhawk', 'Regal GS', 'Cascada', 'Verano', 'Eight', - 'Envision' + 'Envision', ], 'Cadillac' => [ 'Seville', 'Allante', 'Brougham', 'Catera', 'CTS', 'DE Ville', 'Eldorado', 'Escalade', 'Evoq', 'LSE', 'SRX', 'Vizon', 'XLR', 'STS', 'DTS', 'Fleetwood', 'CTS-V Coupe', 'Cimarron', 'Convertible', 'BLS', 'XTS', - 'ATS', 'Eureka', 'ELR', 'XT5', 'CT6' + 'ATS', 'Eureka', 'ELR', 'XT5', 'CT6', ], 'Caterham' => [ - '7', '21', 'Classic' + '7', '21', 'Classic', ], 'Chevrolet' => [ 'Blazer', 'Camaro', 'Corvette', 'Alero', 'Astra', 'Astro пасс.', 'Aveo', 'Beretta', 'Caprice', 'Cavalier', @@ -121,14 +121,14 @@ class FakeCarData 'Geo Metro', 'Opala', 'Prizm', 'Astro груз.', 'Express груз.', 'TrailBlazer', 'Bel Air', 'Celebrity', 'Chance', 'Spark', 'Citation', 'Volt', 'Convertible', 'R3500', 'Orlando', 'Master De luxe', 'El Camino', 'Explorer', 'Uplander', 'Camaro Convertible', 'Trans Sport', 'Vandura', 'Spectrum', 'Tracker', 'SS', - 'Master', 'Beauville', 'Rezzo', 'Chevelle', 'Kalos', 'Trax', 'Sonic', 'Bolt EV', 'Van G-20' + 'Master', 'Beauville', 'Rezzo', 'Chevelle', 'Kalos', 'Trax', 'Sonic', 'Bolt EV', 'Van G-20', ], 'Chrysler' => [ '180', 'Avenger', 'Grand Voyager', 'New Yorker', 'PT Cruiser', 'Viper', 'Voyager', 'Cirrus', 'Concorde', 'Crossfire', 'Daytona Shelby', 'LE Baron', 'LHS', 'Neon', 'Pacifica', 'Prowler', 'Stratus', 'Town & Country', 'Vision', 'Jeep Cherokee', 'Intrepid', 'Sebring', 'Saratoga', 'Aspen', '300 M', 'Simca', 'Reliant', 'Sunbeam', 'Imperial', 'HHR', 'ES', '200', 'Tolbot', 'Phantom', 'Dynasty', 'Laser', '300 C', - '300', '300 S', 'Royal', '160' + '300', '300 S', 'Royal', '160', ], 'Citroen' => [ 'Athena', 'AX', 'Berlingo пасс.', 'BX', 'C3', 'C5', 'C8', 'CX', 'Dyane', 'GSA', 'LNA', 'Reflex', 'Saxo', @@ -136,68 +136,68 @@ class FakeCarData 'GS', 'Evasion', 'C6', 'C4', 'C2', 'C15', 'AMI', 'Acadiane', 'Oltcit', 'C1', 'Berlingo груз.', 'Nemo груз.', 'C-Crosser', 'Grand C4 Picasso', 'C4 Picasso', 'Jumpy груз.', 'DS3', 'Nemo пасс.', 'Jumper пасс.', 'C3 Picasso', 'CQ', 'DS4', 'DS5', '2CV', 'C-Elysee', 'Axel', 'C-Zero', 'C4 Cactus', - 'Rosalie', 'C4 Aircross', 'Traction Avant', 'Space Tourer' + 'Rosalie', 'C4 Aircross', 'Traction Avant', 'Space Tourer', ], 'Dacia' => [ 'Denem', 'Duster', '1300', '1310', '1325', '1410', 'Nova', 'Solenza', 'Clima', 'SuperNova', 'Rapsodie', - 'Logan', 'Sandero', 'Express', '1304', 'Lodgy', 'Dokker', 'Sandero StepWay' + 'Logan', 'Sandero', 'Express', '1304', 'Lodgy', 'Dokker', 'Sandero StepWay', ], 'Dadi' => [ - 'Shuttle', 'Aurora', 'Smoothing', 'City Leading', 'Bliss', 'Suv', 'BDD', 'Groz', 'Huabey', 'Soyat' + 'Shuttle', 'Aurora', 'Smoothing', 'City Leading', 'Bliss', 'Suv', 'BDD', 'Groz', 'Huabey', 'Soyat', ], 'Daewoo' => [ 'Espero', 'Kalos', 'Korando', 'Lanos', 'Leganza', 'Matiz', 'Musso', 'Nexia', 'Nubira', 'Tacuma', 'Arcadia', 'Charman', 'Evanda', 'Lacetti', 'LE Mans', 'Magnus', 'Racer', 'Sens', 'Tico', 'Polonez', 'Damas', 'Prince', - 'Lublin груз.', 'Nubira Sx', 'BV', 'Super Salon', 'Royale', 'Brougham', 'Gentra', 'Cielo', 'Tosca' + 'Lublin груз.', 'Nubira Sx', 'BV', 'Super Salon', 'Royale', 'Brougham', 'Gentra', 'Cielo', 'Tosca', ], 'Daf' => [ - '200', '600', '46' + '200', '600', '46', ], 'Dagger' => [ - 'GT' + 'GT', ], 'Daihatsu' => [ 'Applause', 'Charade', 'Charmant', 'Cuore', 'Domino', 'Fourtrak', 'Gran Move', 'Hijet', 'Mira', 'Move', 'Sirion', 'Sportrak', 'Terios', 'YRV', 'Altis', 'Atrai/extol', 'Copen', 'Delta', 'Feroza', 'Leeza', 'MAX', - 'IRV', 'Materia', 'Rocky', 'Tianjin', 'Taft', 'Trevis', 'Ayla', 'Sigra' + 'IRV', 'Materia', 'Rocky', 'Tianjin', 'Taft', 'Trevis', 'Ayla', 'Sigra', ], 'Daimler' => [ 'Limousine', 'Series III', 'Sovereign', 'XJ Series', 'XJ12', 'XJ6', 'Coupe', 'Daimler', 'Landaulette', - 'Double Six' + 'Double Six', ], 'Datsun' => [ - 'on-DO', '100' + 'on-DO', '100', ], 'De Lorean' => [ - 'DMC' + 'DMC', ], 'Detroit Electric' => [ - 'SP:01' + 'SP:01', ], 'Dodge' => [ 'Avenger', 'Caravan', 'Dakota', 'Durango', 'Intrepid', 'Magnum', 'Monaco', 'Neon', 'RAM', 'Ramcharger', 'Shadow', 'Spirit', 'Stealth', 'Stratus', 'Viper', 'Charger', 'Caliber', 'D6', 'Nitro', 'Ram Van', 'Power Wagon', 'Daytona', 'Challenger', 'Charger Daytona', 'Journey', 'Polara', 'Arrow', 'Sprinter пасс.', - 'Aries', 'Dynasty', 'SXT', 'Diplomat', 'Grand Caravan', 'Omni', 'Dart', 'WC', 'Aspen', 'М 886', 'Colt', 'D2' + 'Aries', 'Dynasty', 'SXT', 'Diplomat', 'Grand Caravan', 'Omni', 'Dart', 'WC', 'Aspen', 'М 886', 'Colt', 'D2', ], 'Dr. Motor' => [ - 'DR5' + 'DR5', ], 'DS' => [ - '3', '4' + '3', '4', ], 'Eagle' => [ - 'Premier', 'Summit', 'Talon', 'Vision' + 'Premier', 'Summit', 'Talon', 'Vision', ], 'FAW' => [ - '6371 груз.', '6350', 'Vita (C1)', 'Besturn', 'HQ3', 'CA 6371 Cargo', '6371 пасс.', 'V2', 'V5', 'Oley' + '6371 груз.', '6350', 'Vita (C1)', 'Besturn', 'HQ3', 'CA 6371 Cargo', '6371 пасс.', 'V2', 'V5', 'Oley', ], 'Ferrari' => [ '208/308', '328', '348', '360', '400', '412', '456', '456M', '512', '550', '575M', 'F355', 'F40', 'F50', 'F512', 'Mondial', 'Testarossa', '612 Scaglietti', 'Barchetta', 'Dino', 'Enzo', 'Maranello California', 'Fiorano', 'Modena Spider', 'Maranello California USA', 'Scuderia Spider 16M Convertible', 'F430', '458 Italia', 'California', 'FF', 'F12', '250 GTO', '458', 'LaFerrari', '599 GTO', '599', '488 GTB', - '488 Spider' + '488 Spider', ], 'Fiat' => [ '126', '127', '128', '132', 'Abarth', 'Argenta', 'Barchetta', 'Brava', 'Bravo', 'Cinquecento', 'Coupe', @@ -208,13 +208,13 @@ class FakeCarData 'Ducato груз.', 'Ducato пасс.', 'Scudo пасс.', 'Campagnola', 'Talento пасс.', 'Talento груз.', 'Doblo груз.', 'Doblo пасс.', 'Ducato', 'Scudo', 'Torino', '500 C', 'Mirafiori', 'Freemont', 'Elba', 'Simca', 'Fiorino груз.', '850', 'FSO Polonez', '600', '133', '500 L', '508', '500 X', '1500', 'Punto Evo', - 'Qubo груз.', 'Fullback' + 'Qubo груз.', 'Fullback', ], 'Fiat-Abarth' => [ - '500', '595', '695', '750', '850 TC', '1000 Berlina', '700 spider' + '500', '595', '695', '750', '850 TC', '1000 Berlina', '700 spider', ], 'Fisker' => [ - 'Karma' + 'Karma', ], 'Ford' => [ 'Capri', 'Cortina', 'Cougar', 'Escort', 'Explorer', 'Fiesta', 'Focus', 'Fusion', 'Galaxy', 'Granada', 'KA', @@ -229,45 +229,45 @@ class FakeCarData 'Transit', 'Transit Connect', 'F-550', 'Diamant', 'Antara', 'Cabster', 'Escort van', 'Raptor', 'Cobra', 'Model A', 'Fairlane', 'Gran Torino', 'LTD', 'Fairmont', 'Т', 'Freestyle', 'Falcon', 'B-Max', 'Transit Custom', 'Tourneo Custom', 'V8', 'Model T', 'EcoSport', 'Tourneo Courier', 'F-Series', - 'Escort Express', 'Focus Electric', 'C-Max Energi', 'Transit Courier', 'Torino' + 'Escort Express', 'Focus Electric', 'C-Max Energi', 'Transit Courier', 'Torino', ], 'Fornasari' => [ - 'RR' + 'RR', ], 'FSO' => [ - '125P', '1300', 'Caro', 'Polonez', '126P', '127P', '132P', 'Warszawa', 'Syrena' + '125P', '1300', 'Caro', 'Polonez', '126P', '127P', '132P', 'Warszawa', 'Syrena', ], 'FUQI' => [ - 'FQ' + 'FQ', ], 'Gac' => [ - 'Gonow' + 'Gonow', ], 'Geely' => [ 'HA', 'HS', 'UL', 'CK1', 'BO', 'CK', 'MK', 'FC', 'MK-2', 'CK-2', 'Maple', 'JL', 'MR', 'FS', 'Emgrand 7 (EC7)', 'SL', 'MK Cross', 'SC', 'SMA', 'LC', 'Vision', 'Emgrand X7', 'GC2', 'GХ2', 'Safe', - 'Emgrand 8', 'GC6', 'GC5', 'GC7', 'Panda', 'Emgrand X9' + 'Emgrand 8', 'GC6', 'GC5', 'GC7', 'Panda', 'Emgrand X9', ], 'Geo' => [ - 'Storm', 'Metro', 'Prizm', 'Tracker' + 'Storm', 'Metro', 'Prizm', 'Tracker', ], 'GMC' => [ 'Envoy', 'Jimmy', 'Safari', 'Savana', 'Sierra', 'Sonoma', 'Yukon', 'Acadia', 'Canyon', 'Vandura пасс.', - 'T6500', 'Acadia USA', 'Vandura груз.', 'Delorean', 'Terrain', 'C', 'Suburban', '100' + 'T6500', 'Acadia USA', 'Vandura груз.', 'Delorean', 'Terrain', 'C', 'Suburban', '100', ], 'Gonow' => [ - 'DianGo', 'Troy Suv', 'Jetstar', 'Victor Suv', 'GX6' + 'DianGo', 'Troy Suv', 'Jetstar', 'Victor Suv', 'GX6', ], 'Great Wall' => [ 'Deer', 'Safe', 'Hover', 'Pegasus', 'SoCool', 'Wingle', 'Hover F&L', 'Cowry', 'Tianma', 'SUV', 'Florid', 'Haval', 'Voleex', 'CC', 'Sing', 'Haval H3', 'Haval H5', 'Haval H6', 'Haval M2', 'Haval M4', 'C30', 'М4', - 'H6', 'Voleex C10' + 'H6', 'Voleex C10', ], 'Hafei' => [ - 'Ruiyi', 'Minyi', 'Zhongyi', 'Saibao', 'Lobo', 'Sigma', 'Saima', 'Princip' + 'Ruiyi', 'Minyi', 'Zhongyi', 'Saibao', 'Lobo', 'Sigma', 'Saima', 'Princip', ], 'Haima' => [ - '3' + '3', ], 'Honda' => [ 'Accord', 'Aerodeck', 'Brio', 'Ballade', 'Civic', 'Concerto', 'CR-V', 'CRX', 'HR-V', 'Insight', 'Integra', @@ -278,16 +278,16 @@ class FakeCarData 'Eve', 'Elysion', 'Freed', ], 'Huabei' => [ - 'HC', 'HG', 'Poni' + 'HC', 'HG', 'Poni', ], 'Humber' => [ - 'Sceptre', 'Hawk' + 'Sceptre', 'Hawk', ], 'Hummer' => [ - 'Hummer', 'H3', 'H2', 'H1', 'H3X', 'H4' + 'Hummer', 'H3', 'H2', 'H1', 'H3X', 'H4', ], 'Humvee' => [ - 'C-Series', 'Marshal' + 'C-Series', 'Marshal', ], 'Hyundai' => [ 'Amica', 'Atos', 'Coupe', 'Lantra', 'Matrix', 'Pony', 'Santa FE', 'S-Coupe', 'Sonata', 'Stellar', 'Trajet', @@ -295,43 +295,43 @@ class FakeCarData 'XG', 'H 200 пасс.', 'Grandeur', 'Prest', 'Avante', 'Azera', 'i10', 'i20', 'i30', 'County', 'Tiburon', 'Genesis', 'Elantra', 'ix55 (Veracruz)', 'IX35', 'Starex', 'H1 груз.', 'H 100 груз.', 'H 200 груз.', 'Marcia', 'Excel', 'GLS', 'GX', 'Solaris', 'Getz', 'Veloster', 'Equus', 'H 300 груз.', 'i40', - 'Grand Starex', 'Grand Santa Fe', 'H 300 пасс.', 'H 150 пасс.', 'IX20', 'Creta', 'Ioniq', 'HLF', 'Kona' + 'Grand Starex', 'Grand Santa Fe', 'H 300 пасс.', 'H 150 пасс.', 'IX20', 'Creta', 'Ioniq', 'HLF', 'Kona', ], 'Infiniti' => [ 'FX', 'I', 'J', 'M30', 'M45', 'Q45', 'QX4', 'QX', 'G', 'M', 'EX', 'G25', 'Q', 'M25', 'M37', 'G35', 'G37', 'JX', 'Q50', 'Q60', 'Q70', 'QX50', 'QX60', 'QX70', 'QX80', 'EX 30', 'EX 35', 'EX 37', 'M35', 'Q30', 'QX56', - 'FX 30', 'FX 37', 'FX 35', 'FX 50', 'FX 45', 'EX 25', 'QX30' + 'FX 30', 'FX 37', 'FX 35', 'FX 50', 'FX 45', 'EX 25', 'QX30', ], 'Innocenti' => [ - 'Elba' + 'Elba', ], 'Iran Khodro' => [ - 'Runna', 'Samand', 'Soren' + 'Runna', 'Samand', 'Soren', ], 'Isuzu' => [ 'Piazza', 'Trooper', 'Ascender', 'Aska', 'Axiom', 'Campo', 'Gemini', 'Impulse', 'Midi пасс.', 'Rodeo', 'VehiCross', 'Bighorn', 'Fargo', 'D-Max', 'Amigo', 'Midi груз.', 'Florian', 'TFR', 'MD', 'FRR', 'Faster', - 'Hombre', 'Pick Up', 'Stylus', 'Panther' + 'Hombre', 'Pick Up', 'Stylus', 'Panther', ], 'ItalCar' => [ - 'Attiva' + 'Attiva', ], 'Iveco' => [ - 'Daily пасс.', 'Unic', 'Massif', 'Menarini', 'Daily 4x4' + 'Daily пасс.', 'Unic', 'Massif', 'Menarini', 'Daily 4x4', ], 'Jaguar' => [ 'S-Type', 'Sovereign', 'X-Type', 'XJ', 'XJ6', 'XJR', 'XJR-S', 'XJS', 'XKR', 'E-Type', 'XJ8', 'XF', '4000', - 'Mark', 'XK', 'SL', 'Vanden', 'Daimler', 'XFR', 'F-Type', 'DS', 'XJL', 'XE', 'F-Pace' + 'Mark', 'XK', 'SL', 'Vanden', 'Daimler', 'XFR', 'F-Type', 'DS', 'XJL', 'XE', 'F-Pace', ], 'Jeep' => [ 'Cherokee', 'Grand Cherokee', 'Wrangler', 'CJ', 'Liberty', 'Patriot', 'Compass', 'Commander', 'Willys', - 'Renegade', 'Comanche' + 'Renegade', 'Comanche', ], 'Jinbei Minibusus' => [ - 'SY6482Q2' + 'SY6482Q2', ], 'JMC' => [ - 'BD', 'YunBa', 'Baodian' + 'BD', 'YunBa', 'Baodian', ], 'Kia' => [ 'Carens', 'Clarus', 'Magentis', 'Mentor', 'Mentor II', 'Pride', 'Rio', 'Sedona', 'Shuma', 'Sorento', @@ -340,75 +340,75 @@ class FakeCarData 'Jumbo Titan', 'Optima', 'Ceed', 'Ceed SW', 'Pro Ceed', 'Borrego', 'Spectra', 'Soul', 'Mohave', 'Cerato Koup', 'Sephia II', 'Cadenza', 'Koup', 'Ceres', 'Venga', 'Pregio груз.', 'Kosmos', 'Carstar', 'Credos', 'Ceed Sportswagon', 'Towner', 'Quoris', 'Rio Hatchback 5D', 'Rio Hatchback 3D', 'Forte', - 'Niro', 'Stinger', 'Stonic' + 'Niro', 'Stinger', 'Stonic', ], 'King Long' => [ - 'Kingte' + 'Kingte', ], 'KingWoo' => [ - 'XD-BB', 'KYGDG11A', 'KYGDG08A', 'KYG5S', 'KW 500', 'KW 625', 'KW 625W' + 'XD-BB', 'KYGDG11A', 'KYGDG08A', 'KYG5S', 'KW 500', 'KW 625', 'KW 625W', ], 'Kirkham' => [ - '427 KMS' + '427 KMS', ], 'Koenigsegg' => [ - 'CCXR Trevita', 'CCX', 'Agera' + 'CCXR Trevita', 'CCX', 'Agera', ], 'Konecranes' => [ - 'Steyr 55' + 'Steyr 55', ], 'Lamborghini' => [ 'Countach', 'Diablo', 'Murcielago', 'Espada', 'Gallardo', 'Jalpa', 'Jarama', 'Lm-001', 'Lm-002', 'Urraco', - 'Gallardo LP 550-2', 'Reventon', 'Aventador', '400 GT', 'Urus', 'Huracan' + 'Gallardo LP 550-2', 'Reventon', 'Aventador', '400 GT', 'Urus', 'Huracan', ], 'Lancia' => [ 'Beta', 'Dedra', 'Delta', 'Gamma', 'Monte Carlo', 'Prisma', 'Thema', 'Trevi', 'Y10', 'A 112', 'Fulvia', - 'Kappa', 'Lybra', 'Musa', 'Phedra', 'Thesis', 'Y', 'Zeta', 'Ypsilon' + 'Kappa', 'Lybra', 'Musa', 'Phedra', 'Thesis', 'Y', 'Zeta', 'Ypsilon', ], 'Land Rover' => [ 'Discovery', 'Freelander', 'Range Rover', 'Range Rover Sport', 'Defender', 'Range Rover Evoque', - 'Discovery Sport', 'Range Rover Velar' + 'Discovery Sport', 'Range Rover Velar', ], 'LDV' => [ - 'Maxus', 'Pilot' + 'Maxus', 'Pilot', ], 'Lexus' => [ 'GS', 'IS', 'LS', 'RX', 'SC', 'ES', 'LX', 'GX', 'IS-F', 'HS', 'IS-C', 'RH', 'CT 200H', 'CT', 'LF', 'NX', 'RC', 'ES 250', 'ES 300', 'ES 350', 'ES 330', 'GS 250', 'GS 300', 'GS 350', 'GS 400', 'GS 430', 'GS 450', 'GS 460', 'IS 200', 'IS 220', 'IS 250', 'IS 300', 'IS 350', 'LS 400', 'LS 430', 'LS 460', 'LS 600', 'LX 450', 'LX 470', 'LX 570', 'NX 200', 'NX 300', 'RX 270', 'RX 300', 'RX 330', 'RX 350', 'RX 400', - 'RX 450', 'SC 400', 'SC 430', 'RX 200', 'SC 300', 'GS 200', 'ES 200', 'GS F' + 'RX 450', 'SC 400', 'SC 430', 'RX 200', 'SC 300', 'GS 200', 'ES 200', 'GS F', ], 'Lincoln' => [ 'Aviator', 'Blackwood', 'Continental', 'LS', 'Mark', 'Navigator', 'Town Car', 'MKZ', 'MKX', 'MKS', 'MKT', - 'Excalibur', 'Cartier', 'Mercury', 'Zephyr', 'MKC' + 'Excalibur', 'Cartier', 'Mercury', 'Zephyr', 'MKC', ], 'Lotus' => [ - 'Eclat', 'Elan', 'Elise', 'Elite', 'Esprit', 'Excel', 'Exige', 'Europa', 'Super Seven', 'Evora', 'Seven' + 'Eclat', 'Elan', 'Elise', 'Elite', 'Esprit', 'Excel', 'Exige', 'Europa', 'Super Seven', 'Evora', 'Seven', ], 'LTI' => [ - 'TX' + 'TX', ], 'Luxgen' => [ - '5', '7' + '5', '7', ], 'Marshell' => [ - 'DN', 'DG-C2' + 'DN', 'DG-C2', ], 'Mahindra' => [ 'Alturas G4', 'Bolero', 'KUV100', 'KUV100 NXT', 'Marazzo', 'Scorpio', 'Scorpio Getaway', 'TUV300', - 'TUV300 PLUS', 'Thar', 'Verito', 'Verito Vibe CS', 'XUV500', 'e20 NXT', 'e2o PLUS', 'eKUV100', 'XUV300' + 'TUV300 PLUS', 'Thar', 'Verito', 'Verito Vibe CS', 'XUV500', 'e20 NXT', 'e2o PLUS', 'eKUV100', 'XUV300', ], 'Maruti' => [ - '1000', '800', 'Alto', 'Baleno', 'Esteem', 'Gypsy', 'Omni', 'Versa', 'Wagon R', 'Zen', 'Suzuki' + '1000', '800', 'Alto', 'Baleno', 'Esteem', 'Gypsy', 'Omni', 'Versa', 'Wagon R', 'Zen', 'Suzuki', ], 'Maserati' => [ '222', '3200', '420/430', 'Biturbo', 'Coupe', 'Ghibli', 'Quattroporte', 'Shamal', 'Spyder', '228', 'Barchetta Stradale', 'Bora', 'Chubasco', 'GranSport', 'Indy', 'Karif', 'Khamsin', 'Kyalami', 'Merak', - 'Mexico', 'Royale', 'GranTurismo', 'Mc12', 'GranCabrio', 'Levante' + 'Mexico', 'Royale', 'GranTurismo', 'Mc12', 'GranCabrio', 'Levante', ], 'Maybach' => [ - '57', '62', 'landaulet', 'DS8 Zeppelin', '52', 'Exelero', 'S500', 'S600' + '57', '62', 'landaulet', 'DS8 Zeppelin', '52', 'Exelero', 'S500', 'S600', ], 'Mazda' => [ '121', '2', '323', '6', '626', '929', 'Demio', 'MPV', 'MX-3', 'MX-5', 'MX-6', 'Premacy', 'RX-7', 'RX-8', @@ -416,10 +416,10 @@ class FakeCarData 'Carol', 'Clef', 'Cronos', 'E-series пасс.', 'E-series груз.', 'Eunos 500', '3', '5', 'CX-7', 'CX-9', 'Millenia', 'Sentia', 'Familia', '3 MPS', 'Persona', 'Protege', 'BT-50', 'Capella', 'Eunos Presso', 'Eunos Cargo', 'Eunos', 'Eunos Cosmo', 'MS-8', 'Luce', '6 MPS', 'Xedos 6', 'Lantis', 'CX-5', 'MS-9', - 'MS-6', 'Cosmo', 'CX-3', 'Proceed' + 'MS-6', 'Cosmo', 'CX-3', 'Proceed', ], 'McLaren' => [ - 'MP4', 'F1', '650S', 'P1', '675LT', '570 GT' + 'MP4', 'F1', '650S', 'P1', '675LT', '570 GT', ], 'Mercedes-Benz' => [ '190', '200', '220', '230', '240', '250', '260', '280', '300', '320', '400', '420', '500', '560', '600', @@ -430,18 +430,18 @@ class FakeCarData 'V 220', 'V 280', 'GLK 200', 'GLK 220', 'GLK 250', 'GLK 280', 'GLK 300', 'GLK 320', 'GLK 350', 'B 150', 'B 160', 'B 170', 'B 180', 'B 200', 'CLC 160', 'CLC 180', 'CLC 200', 'CLC 230', 'CLC 250', 'CLC 350', 'CLC 220', 'R 280', 'R 300', 'R 320', 'R 350', 'R 500', 'R 63 AMG', 'SL 280', 'SL 300', 'SL 320', 'SL 350', - 'SL 380', 'SL 420', 'SL 450', 'SL 500 (550)', 'SL 55 AMG', 'SL 560', 'SL 60 AMG', 'SL 600', 'SL 63 AMG', - 'SL 65 AMG', 'SL 70 AMG', 'SL 73 AMG', 'CLK 200', 'CLK 220', 'CLK 230', 'CLK 240', 'CLK 270', 'CLK 280', - 'CLK 320', 'CLK 350', 'CLK 430', 'CLK 500', 'CLK 55 AMG', 'CLK 63 AMG', '206 пасс.', 'Citan', 'G 230', - 'G 240', 'G 250', 'G 270', 'G 280', 'G 290', 'G 300', 'G 320', 'G 350', 'G 400', 'G 500', 'G 55 AMG', - 'G 63 AMG', 'G 65 AMG', 'SLK 200', 'SLK 230', 'SLK 250', 'SLK 280', 'SLK 300', 'SLK 32 AMG', 'SLK 320', - 'SLK 350', 'SLK 55 AMG', 'CLS 250', 'CLS 280', 'CLS 300', 'CLS 320', 'CLS 350', 'CLS 500', 'CLS 55 AMG', - 'CLS 63 AMG', 'A 140', 'A 150', 'A 160', 'A 170', 'A 180', 'A 190', 'A 200', 'A 210', 'A 220', 'A 250', - 'ML 230', 'ML 250', 'ML 270', 'ML 280', 'ML 300', 'ML 320', 'ML 350', 'ML 400', 'ML 420', 'ML 430', - 'ML 500', 'ML 55 AMG', 'ML 63 AMG', 'GL 320', 'GL 350', 'GL 420', 'GL 450', 'GL 500', 'GL 55 AMG', - 'GL 63 AMG', 'GL 550', 'CL 160', 'CL 180', 'CL 200', 'CL 220', 'CL 230', 'CL 320', 'CL 420', 'CL 500', - 'CL 55 AMG', 'CL 600', 'CL 63 AMG', 'CL 65 AMG', 'CLA-Class', 'CLA 180', 'CLA 200', 'CLA 220', 'CLA 250', - 'S 250', 'S 260', 'S 280', 'S 300', 'S 320', 'S 350', 'S 400', 'S 420', 'S 430', 'S 450', 'S 500', 'S 55', + 'SL 380', 'SL 420', 'SL 450', 'SL 500 (550)', 'SL 55 AMG', 'SL 560', 'SL 60 AMG', 'SL 600', 'SL 63 AMG', + 'SL 65 AMG', 'SL 70 AMG', 'SL 73 AMG', 'CLK 200', 'CLK 220', 'CLK 230', 'CLK 240', 'CLK 270', 'CLK 280', + 'CLK 320', 'CLK 350', 'CLK 430', 'CLK 500', 'CLK 55 AMG', 'CLK 63 AMG', '206 пасс.', 'Citan', 'G 230', + 'G 240', 'G 250', 'G 270', 'G 280', 'G 290', 'G 300', 'G 320', 'G 350', 'G 400', 'G 500', 'G 55 AMG', + 'G 63 AMG', 'G 65 AMG', 'SLK 200', 'SLK 230', 'SLK 250', 'SLK 280', 'SLK 300', 'SLK 32 AMG', 'SLK 320', + 'SLK 350', 'SLK 55 AMG', 'CLS 250', 'CLS 280', 'CLS 300', 'CLS 320', 'CLS 350', 'CLS 500', 'CLS 55 AMG', + 'CLS 63 AMG', 'A 140', 'A 150', 'A 160', 'A 170', 'A 180', 'A 190', 'A 200', 'A 210', 'A 220', 'A 250', + 'ML 230', 'ML 250', 'ML 270', 'ML 280', 'ML 300', 'ML 320', 'ML 350', 'ML 400', 'ML 420', 'ML 430', + 'ML 500', 'ML 55 AMG', 'ML 63 AMG', 'GL 320', 'GL 350', 'GL 420', 'GL 450', 'GL 500', 'GL 55 AMG', + 'GL 63 AMG', 'GL 550', 'CL 160', 'CL 180', 'CL 200', 'CL 220', 'CL 230', 'CL 320', 'CL 420', 'CL 500', + 'CL 55 AMG', 'CL 600', 'CL 63 AMG', 'CL 65 AMG', 'CLA-Class', 'CLA 180', 'CLA 200', 'CLA 220', 'CLA 250', + 'S 250', 'S 260', 'S 280', 'S 300', 'S 320', 'S 350', 'S 400', 'S 420', 'S 430', 'S 450', 'S 500', 'S 55', 'S 550', 'S 600', 'S 63 AMG', 'S 65 AMG', 'S 67', '210', 'Sprinter 208 груз.', 'Sprinter 209 груз.', 'Sprinter 210 груз.', 'Sprinter 211 груз.', 'Sprinter 212 груз.', 'Sprinter 213 груз.', 'Sprinter 216 груз.', 'Sprinter 308 груз.', 'Sprinter 310 груз.', 'Sprinter 311 груз.', @@ -458,21 +458,21 @@ class FakeCarData 'Maybach', 'GL 400', 'CLS 400', 'GLC-Class', 'Sprinter 324 пасс.', 'GLS-Class', 'GLS 350', 'GLS 400', 'GLS 500', 'GLS 63', 'S-Guard', 'B-Class Electric Drive', 'Sprinter 513 пасс.', 'Electric Drive', 'B 220', '170', 'A45 AMG', 'SL 400', '450', 'SLC-Class', 'N 1300', 'S 220', 'Sprinter 415 пасс.', '1628', 'GLS 450', - 'Sprinter 219 груз.', 'N 1000', 'SE', 'X-Class' + 'Sprinter 219 груз.', 'N 1000', 'SE', 'X-Class', ], 'Mercury' => [ 'Cougar', 'Grand Marquis', 'Marauder', 'Montego', 'Monterey', 'Mountaineer', 'Mystique', 'Sable', 'Topaz', - 'Tracer', 'Villager', 'Mariner', '50ELPTO', '90ELPTO', 'Zephyr', '50', 'Black Max' + 'Tracer', 'Villager', 'Mariner', '50ELPTO', '90ELPTO', 'Zephyr', '50', 'Black Max', ], 'MG' => [ - 'TF', '550', 'ZT', 'F', '6', '6 5D', '350', 'Montego', '3', '5', 'ZR', '750', 'ZS', 'Maestro' + 'TF', '550', 'ZT', 'F', '6', '6 5D', '350', 'Montego', '3', '5', 'ZR', '750', 'ZS', 'Maestro', ], 'Miles' => [ - 'ZX40', 'OR70' + 'ZX40', 'OR70', ], 'MINI' => [ 'Cooper', 'Mini', 'One', 'Cabrio', 'Clubman', 'Cooper S', 'Cooper D', 'Rover', 'Countryman', 'Paceman', - 'Coupe', 'Roadster', 'Hatch' + 'Coupe', 'Roadster', 'Hatch', ], 'Mitsubishi' => [ '3000 GT', 'Carisma', 'Celeste', 'Challenger', 'Colt', 'Cordia', 'FTO', 'Galant', 'Lancer', 'Sapporo', @@ -487,13 +487,13 @@ class FakeCarData 'Xpander', 'Eclipse Cross', 'Xpander Cross', ], 'Morgan' => [ - 'Four Four', 'Aero 8', 'Plus 4', 'Plus 8', 'Aero Supersports' + 'Four Four', 'Aero 8', 'Plus 4', 'Plus 8', 'Aero Supersports', ], 'Morris' => [ - 'Ital', 'Marina', 'Minor' + 'Ital', 'Marina', 'Minor', ], 'MPM Motors' => [ - 'PS 160' + 'PS 160', ], 'Nissan' => [ '100NX', '120Y Sunny', '140J Violet', '140Y Sunny', '160B Bluebird', '160J Violet', '180B Bluebird', '200', @@ -508,18 +508,18 @@ class FakeCarData 'Vanette груз.', 'Serena груз.', 'Primastar груз.', 'Sima', 'Bassara', 'Stagea', 'Pixo', 'Auster', 'Gazelle', '100NS', 'Largo', 'Leaf', 'NV', 'Homy', 'Presage', 'Wingroad', 'Cube', 'Arna', 'e-NV200', 'Datsun on-DO', 'Datsun MI-DO', 'Fuga', 'Juke Nismo', 'Caravan', 'Liberty', 'Safari', 'Grand Livina', - 'Xtrail', 'Kicks' + 'Xtrail', 'Kicks', ], 'Norster' => [ - '600R' + '600R', ], 'Oldsmobile' => [ 'Achieva', 'Alero', 'Aurora', 'Bravada', 'Cutlass', 'Eighty-Eight', 'Intrigue', 'Silhouette', 'Omega', 'Regency', 'Delta', 'Ninety Eidht', 'Holiday', 'Royal', 'Tornado', 'Super 88', '98', 'Urbee', - 'Cutlass Ciera' + 'Cutlass Ciera', ], 'Oltcit' => [ - 'Club' + 'Club', ], 'Opel' => [ 'Commodore', 'Kadett', 'Manta', 'Monza', 'Rekord', 'Senator', 'Admiral', 'Agila', 'Arena пасс.', 'Ascona', @@ -528,16 +528,16 @@ class FakeCarData 'Olimpia', 'Kapitan', 'Astra G', 'Antara', 'P4', 'Super 6', 'Astra F', 'Vectra B', 'Vectra A', 'Movano груз.', 'Movano пасс.', 'Vivaro пасс.', 'Vivaro груз.', 'Combo пасс.', 'Arena груз.', 'Capitan', 'Chevette', 'Saturn', 'Astra J', 'Diamant', 'Komador', 'GT', 'Cascada', 'Mokka', 'Adam', 'Orion', 'Ampera', - 'Corsa OPC', 'Astra K', 'Astra H OPC', 'Ranger' + 'Corsa OPC', 'Astra K', 'Astra H OPC', 'Ranger', ], 'Packard' => [ - 'Super Eight', 'One Twenty', 'Hawk' + 'Super Eight', 'One Twenty', 'Hawk', ], 'Pagani' => [ - 'Huayra', 'Zonda' + 'Huayra', 'Zonda', ], 'Peg-Perego' => [ - 'Gaucho', 'Ranger' + 'Gaucho', 'Ranger', ], 'Peugeot' => [ '104', '106', '205', '206', '206 SW', '304', '305', '306', '306 Sedan', '307', '309', '405', '406', '504', @@ -548,40 +548,40 @@ class FakeCarData '407 Sedan', '407 SW', 'RCZ', 'Boxer пасс.', 'Bipper груз.', 'Expert пасс.', 'Partner груз.', '3008', '308 SW', '308 CC', '307 CC', 'Boxer', '117', '508', '1007', '4008', '5008', '203', '308 Sportium', '403', '408', 'Ranch', '301', '208', 'P4', '208 Hatchback (5d)', '208 GTI', 'BB1', '2008', '508 RXH', 'iOn', - '108', '206 СС', 'Traveller', '207 CC' + '108', '206 СС', 'Traveller', '207 CC', ], 'Pininfarina' => [ - 'Cambiano' + 'Cambiano', ], 'Pinzgauer' => [ - '710' + '710', ], 'Plymouth' => [ 'Coltvista', 'Prowler', 'Acclaim', 'Conquest', 'Colt', 'Caravelle', 'Breeze', 'Gran Fury', 'Grand Voyager', 'Horizon', 'Laser', 'Neon', 'Reliant', 'Sapporo', 'Scamp', 'Sundance', 'Turismo', 'Voyager', 'Barracuda', - 'Satellite', 'Fury', 'valare' + 'Satellite', 'Fury', 'valare', ], 'Pontiac' => [ '6000', 'Aztec', 'Bonneville', 'Firebird', 'Grand AM', 'Grand Prix', 'GTO', 'Montana', 'Phoenix', 'Sunbird', 'Sunfire', 'Trans Sport', 'Vibe', 'Solstice', 'Fiero', 'G8', 'G6', 'Tempest', 'Beaumont', 'G5', - 'Lemans', 'Catalina', 'Laurentian', 'Strato Chief', 'Parisienne', 'Sunburst' + 'Lemans', 'Catalina', 'Laurentian', 'Strato Chief', 'Parisienne', 'Sunburst', ], 'Porsche' => [ '911', '924', '928', '944', '968', 'Boxster', 'Cayenne', '959', 'Cayman', 'Panamera', '356', '997', '550', - '964', 'Macan', '996', '918 Spyder' + '964', 'Macan', '996', '918 Spyder', ], 'Praga Baby' => [ - 'Tudor', 'Baby' + 'Tudor', 'Baby', ], 'Proton' => [ 'Compact', 'Coupe', 'Impian', 'Persona', 'Proton', 'Satria', 'Wira', 'Iswara', 'Juara', 'Perdana', 'Saga', - 'Saloon', 'Waja', '415' + 'Saloon', 'Waja', '415', ], 'Ram' => [ - '1500', '2500', '3500', 'Chassis Cab', 'Promaster City', 'Promaster' + '1500', '2500', '3500', 'Chassis Cab', 'Promaster City', 'Promaster', ], 'Ravon' => [ - 'R2', 'Nexia', 'Gentra', 'Matiz', 'R4' + 'R2', 'Nexia', 'Gentra', 'Matiz', 'R4', ], 'Renault' => [ '11', '12', '14', '15', '16', '17', '18', '19', '20', '21', '25', '30', '4', '5', '6', '9', 'A610', @@ -591,62 +591,62 @@ class FakeCarData 'Kangoo груз.', 'Sandero', 'Fluence', 'Trafic пасс.', 'Duster', 'Master пасс.', 'Chamade', 'Latitude', 'Alliance', 'Thalia', 'Nevada', 'Sandero StepWay', 'Supernova', 'Megane RS', 'Twizy', 'Zoe', 'Lodgy', 'Dokker пасс.', 'Captur', 'Wind', 'Dokker VAN', 'Florida', 'Fluence Z.E', '8', 'Grand Modus', - 'Scenic Conquest', 'Kadjar', 'Talisman' + 'Scenic Conquest', 'Kadjar', 'Talisman', ], 'Renault Samsung Motors' => [ - 'SM3', 'SM5', 'SM7', 'QM5' + 'SM3', 'SM5', 'SM7', 'QM5', ], 'Rezvani' => [ - 'Beast' + 'Beast', ], 'Rolls-Royce' => [ 'Carmargue', 'Corniche', 'Flying Spur', 'Limousine', 'Park Ward', 'Phantom VI', 'Silver Dawn', 'Silver Seraph', 'Silver Shadow', 'Silver Spirit', 'Silver Spur', 'Silver Wraith', 'Ghost', 'Phantom V', - 'Phantom VII', 'Drophead', 'Silver Cloud', 'Wraith', 'Phantom', 'Dawn' + 'Phantom VII', 'Drophead', 'Silver Cloud', 'Wraith', 'Phantom', 'Dawn', ], 'Rover' => [ '100', '200', '2000', '2300', '2400', '25', '2600', '3500', '400', '45', '600', '75', '75 Tourer', '800', 'Cabriolet', 'Coupe', 'Metro', 'Tourer', 'Maestro', 'MGF', 'Mini MK', 'Montego', 'Land Rover', '414', 'Freelander', 'Range Rover', '214', '216', '416', '620', '420', '827', '825', '820', 'Vitesse', '213', - '218', '220', 'Targa', 'Streetwise', '618', 'SD1', '114', '418' + '218', '220', 'Targa', 'Streetwise', '618', 'SD1', '114', '418', ], 'Saab' => [ - '9-3', '9-5', '90', '900', '9000', '99', '9-7X', '96', '9-3 X', 'Griffin', 'Aero', '9-2' + '9-3', '9-5', '90', '900', '9000', '99', '9-7X', '96', '9-3 X', 'Griffin', 'Aero', '9-2', ], 'Saipa' => [ - 'Tiba' + 'Tiba', ], 'Saleen' => [ - 'S7', 'S281', 'S331' + 'S7', 'S281', 'S331', ], 'Samand' => [ - 'LX', 'TAXI', 'EL', 'Soren', 'SPG', 'SE', 'Runna' + 'LX', 'TAXI', 'EL', 'Soren', 'SPG', 'SE', 'Runna', ], 'Samson' => [ - 'F' + 'F', ], 'Sceo' => [ - 'C3', 'Shuanghuan' + 'C3', 'Shuanghuan', ], 'Scion' => [ - 'tC', 'xB', 'xD', 'xA' + 'tC', 'xB', 'xD', 'xA', ], 'Seat' => [ 'Alhambra', 'Arosa', 'Cordoba', 'Ibiza', 'Leon', 'Malaga', 'Marbella', 'Terra', 'Toledo', '133', 'Fura', 'Inca', 'Ronda', 'Altea', '127', 'Exeo', 'Freetrack', 'Cupra', '124', '131', 'Leon X-Perience', 'Altea XL', - 'Mii', '132L', 'Ateca', 'Arona' + 'Mii', '132L', 'Ateca', 'Arona', ], 'Secma' => [ - 'F16', 'Extrem 500', 'Fun Family' + 'F16', 'Extrem 500', 'Fun Family', ], 'Shelby' => [ - 'Cobra', 'Cobra Mk III' + 'Cobra', 'Cobra Mk III', ], 'Shuanghuan' => [ - 'SCEO' + 'SCEO', ], 'Sidetracker' => [ - '418' + '418', ], 'Skoda' => [ '105', '120', '130', 'Estelle', 'Fabia', 'Favorit', 'Felicia', 'Octavia', 'S 100', 'S 110', 'SuperB New', @@ -654,54 +654,54 @@ class FakeCarData 'Octavia Scout', 'Octavia RS', 'Octavia A5', 'Fabia Combi', 'Octavia A5 Combi', 'Octavia Combi', 'Superb', 'Octavia Combi NEW', 'Octavia NEW', 'Yeti', 'Octavia Elegance', 'Praktik', 'SuperВ Combi', '1202', 'Scout', 'Taz', 'Popular', 'Octavia A7', 'Pickup', '440', 'Citigo', 'Octavia A7 Combi', '1201', 'Spaceback', - '1000 MB', 'Kodiaq', 'Karoq' + '1000 MB', 'Kodiaq', 'Karoq', ], 'Smart' => [ - 'City', 'Crossblade', 'Roadster', 'Pulse', 'Forfour', 'Fortwo', 'Cabrio', 'MCC', 'Kitas', 'Fortwo ED' + 'City', 'Crossblade', 'Roadster', 'Pulse', 'Forfour', 'Fortwo', 'Cabrio', 'MCC', 'Kitas', 'Fortwo ED', ], 'SouEast' => [ - 'Lioncel', 'V3', 'Delica' + 'Lioncel', 'V3', 'Delica', ], 'Soyat' => [ - 'Unique Van', 'Yuejin' + 'Unique Van', 'Yuejin', ], 'SsangYong' => [ 'Korando', 'Musso', 'Chairman', 'Family', 'Istana', 'Kallista', 'Rexton', 'Actyon Sports', 'KYRON DELUXE', - 'Rodius', 'Kyron', 'Actyon', 'Rexton II', 'SCEO', 'Rexton W', 'Tivoli', 'XLV' + 'Rodius', 'Kyron', 'Actyon', 'Rexton II', 'SCEO', 'Rexton W', 'Tivoli', 'XLV', ], 'Studebaker' => [ - 'Lark', 'Diktator', 'Starlight' + 'Lark', 'Diktator', 'Starlight', ], 'Subaru' => [ '1600', '1800', 'Forester', 'Impreza', 'Justy', 'Legacy', 'SVX', 'Vivio', 'Baja', 'Bistro', 'Domingo', 'Leone', 'Libero', 'Pleo', 'Traviq', 'XT', 'Outback', 'Tribeca', 'Legacy Wagon', 'Legacy Outback', 'Impreza Sedan', 'Impreza Hatchback', 'WRX STI Hatchback', 'WRX', 'Legacy NEW', 'Alcyone', 'Impreza XV', 'Mini Jumbo', 'WRX STI Sedan', 'Impreza WRX Sedan', 'Rex', 'BRZ', 'XV', 'Impreza WRX STI', 'Sambar', - 'Trezia', 'Crosstrek', 'Levorg' + 'Trezia', 'Crosstrek', 'Levorg', ], 'Suzuki' => [ 'Alto', 'Baleno', 'Cappuccino', 'Grand Vitara', 'Ignis', 'Jimny', 'Liana', 'SA310 Swift', 'Samurai', 'SC100', 'Swift', 'Vitara', 'Wagon R', 'X90', 'Aerio', 'Carry', 'Cultus', 'Dingo', 'Every Landy', 'KEI', 'LJ 80', 'MR Wagon', 'Super Carry Bus', 'XL7', 'SX4', 'Forenza', 'Splash', 'Kizashi', 'Geo Tracker', - 'Cervo', 'Reno', 'Celerio', 'Fronte', 'Esteem', 'UE', 'Ignis II', 'Sidekick', 'Ertiga', 'Karimun' + 'Cervo', 'Reno', 'Celerio', 'Fronte', 'Esteem', 'UE', 'Ignis II', 'Sidekick', 'Ertiga', 'Karimun', ], 'Talbot' => [ - 'Alpine', 'Avenger', 'Horizon', 'Matra', 'Samba', 'Solara', 'Sunbeam', 'Tagora', 'Simca', '1510' + 'Alpine', 'Avenger', 'Horizon', 'Matra', 'Samba', 'Solara', 'Sunbeam', 'Tagora', 'Simca', '1510', ], 'Tarpan Honker' => [ - 'PW', '237' + 'PW', '237', ], 'TATA' => [ - 'Gurkha', 'Safari', 'Telcoline', 'Nano', 'Indica', 'Xenon', 'Indigo' + 'Gurkha', 'Safari', 'Telcoline', 'Nano', 'Indica', 'Xenon', 'Indigo', ], 'Think Global' => [ - 'City' + 'City', ], 'Thunder Tiger' => [ - 'Gonow' + 'Gonow', ], 'Tofas' => [ - 'Sahin', 'Dogan' + 'Sahin', 'Dogan', ], 'Toyota' => [ '4Runner', 'Avensis', 'Avensis Verso', 'Camry', 'Carina', 'Carina E', 'Celica', 'Corolla', 'Corolla Verso', @@ -720,27 +720,27 @@ class FakeCarData 'Proace', '7FBMF16', 'Duet', '8FBMT16', 'Cami', 'Agya', 'Cayla', 'Rush', 'Vios', 'Corolla Cross', ], 'Triumph' => [ - '1500', 'Acclaim', 'Dolomite', 'Spitfire', 'Stag', 'Toledo', 'TR7', 'Daytona', 'Thruxton' + '1500', 'Acclaim', 'Dolomite', 'Spitfire', 'Stag', 'Toledo', 'TR7', 'Daytona', 'Thruxton', ], 'TVR' => [ '2500M', '280i', '3000', '3000M', '350i', '390', '400', '420', '450', 'Cerbera', 'Chimaera', 'Griffith', 'S', 'S2', 'S3', 'S4c', 'Speed Eight', 'T350c', 'T400R', 'T440R', 'Taimar', 'Tamora', 'Tasmin', 'Tuscan', - 'Tuscan R', 'V8' + 'Tuscan R', 'V8', ], 'Ultima' => [ - 'GTR' + 'GTR', ], 'Vauxhall' => [ 'Agila', 'Astra', 'Astra Belmont', 'Belmont', 'Calibra', 'Carlton', 'Cavalier', 'Chevette', 'Corsa', 'Frontera', 'Lotus Carlton', 'Meriva', 'Monterey', 'Nova', 'Omega', 'Royale', 'Senator', 'Signum', 'Sintra', 'Tigra', 'Vectra', 'Viceroy', 'VX220', 'Zafira', 'Ventora', 'Viva', 'Movano', 'Vivaro', - '25 D type', 'VX 2300' + '25 D type', 'VX 2300', ], 'Venturi' => [ - 'Atlantique' + 'Atlantique', ], 'Vepr' => [ - 'Commander' + 'Commander', ], 'Volkswagen' => [ 'Beetle', 'Bora', 'Corrado', 'Derby', 'Golf I', 'Jetta', 'Lupo', 'Phaeton', 'Polo', 'Santana', 'Scirocco', @@ -752,50 +752,50 @@ class FakeCarData 'T4 (Transporter) пасс.', 'Amarok', 'Multivan', 'Cross Touran', 'Golf V', 'Golf VI', 'Taro', 'Kafer', 'Fox', 'T4', 'T5', 'LT', 'Caddy', 'Golf', 'T6', 'T6 (Transporter) груз', 'T6 (Transporter) пасс.', 'Fontana', 'Passat B7', 'Passat Alltrack', 'Passat', 'Passat B1', 'Syncro', 'T1 (Transporter)', 'Golf VII', - 'Up', 'K70', 'Golf R', 'Garbus', 'e-Golf', 'Golf Sportsvan', 'Passat B8', 'Arteon', 'Golf SportWagen' + 'Up', 'K70', 'Golf R', 'Garbus', 'e-Golf', 'Golf Sportsvan', 'Passat B8', 'Arteon', 'Golf SportWagen', ], 'Volvo' => [ '240', '244', '245', '260', '264', '265', '340', '343', '345', '360', '440', '460', '480', '740', '760', '850', '940', '960', 'C70', 'S40', 'S60', 'S70', 'S80', 'S90', 'V40', 'V70', 'V90', 'XC70', 'XC90', '1800', 'VHD', 'C30', 'XC60', '145', 'V50', '142', '144', '670', '610', '140', 'GX', '744', '780', '66', 'V60', - '164', '242', 'FS', 'FS 10', '344' + '164', '242', 'FS', 'FS 10', '344', ], 'Wanfeng' => [ - 'SHK' + 'SHK', ], 'Wuling' => [ - 'Sunshine', 'Xingwang', 'LZW', 'Confero', 'Cortez', 'Almaz' + 'Sunshine', 'Xingwang', 'LZW', 'Confero', 'Cortez', 'Almaz', ], 'Xiaolong' => [ - 'XLW' + 'XLW', ], 'Yugo' => [ - '311', '45', '511', '513', '55', '65', 'Sana', 'Tempo', 'ZLC', 'ZLM', 'ZLX', 'ZLXE', 'Florida' + '311', '45', '511', '513', '55', '65', 'Sana', 'Tempo', 'ZLC', 'ZLM', 'ZLX', 'ZLXE', 'Florida', ], 'Zastava' => [ - 'Yugo Florida', '750', 'Yugo', '128', '1100' + 'Yugo Florida', '750', 'Yugo', '128', '1100', ], 'Zimmer' => [ - 'Golden Spirit' + 'Golden Spirit', ], 'Zotye' => [ - 'Nomad', 'Z300', 'Z100', 'T600' - ] + 'Nomad', 'Z300', 'Z100', 'T600', + ], ]; public static $vehicleTypes = [ - 'hatchback', 'sedan', 'small', 'convertible', 'SUV', 'MPV', 'coupe', 'station wagon' + 'hatchback', 'sedan', 'small', 'convertible', 'SUV', 'MPV', 'coupe', 'station wagon', ]; // Based on 2021 EU sales data // https://www.acea.auto/fuel-pc/fuel-types-of-new-cars-battery-electric-9-1-hybrid-19-6-and-petrol-40-0-market-share-full-year-2021/ public static $vehicleFuelTypes = [ - 'gasoline' => 40, - 'electric' => 10, - 'diesel' => 20, - 'hybrid' => 19.5, + 'gasoline' => 40, + 'electric' => 10, + 'diesel' => 20, + 'hybrid' => 19.5, 'plugin-hybrid' => 10, - 'natural-gas' => 0.5, + 'natural-gas' => 0.5, ]; public static $vehicleDoorCount = [ @@ -812,7 +812,7 @@ class FakeCarData ]; public static $vehicleProperties = [ - 'Towbar', 'Air condition', 'GPS', 'Leather seats', 'Roof Rack' + 'Towbar', 'Air condition', 'GPS', 'Leather seats', 'Roof Rack', ]; public static $vehicleGearBoxType = [ @@ -823,27 +823,27 @@ class FakeCarData public static $vehicleEnginePower = [ 'range' => [100, 1500], 'unit' => 'hp', - 'rounding' => 0 + 'rounding' => 0, ]; public static $vehicleEngineTorque = [ 'range' => [100, 700], 'unit' => 'nm', - 'decimals' => 0 + 'decimals' => 0, ]; public static $vehicleEngineDisplacement = [ 'range' => [500, 8000], 'unit' => 'cc', - 'decimals' => 1 + 'decimals' => 1, ]; public static $vehicleEngineCylinders = [ - '3' => 5, - '4' => 60, - '5' => 5, - '6' => 5, - '8' => 15, + '3' => 5, + '4' => 60, + '5' => 5, + '6' => 5, + '8' => 15, '10' => 5, '12' => 5, ]; @@ -851,7 +851,6 @@ class FakeCarData public static $vehicleEngineFuelConsumption = [ 'range' => [4, 30], 'unit' => 'l/100km', - 'decimals' => 1 + 'decimals' => 1, ]; - } diff --git a/src/FakeCarDataProvider.php b/src/FakeCarDataProvider.php index 143766b..a53efc6 100644 --- a/src/FakeCarDataProvider.php +++ b/src/FakeCarDataProvider.php @@ -3,7 +3,6 @@ namespace Faker\Provider; use Exception; -use InvalidArgumentException; class FakeCarDataProvider implements FakeCarDataProviderInterface { @@ -25,7 +24,7 @@ public function getVehicleBrand(): string /** * @throws Exception */ - public function getVehicleModel(string $brand = null): string + public function getVehicleModel(?string $brand = null): string { $brandsWithModels = $this->vehicleData::$brandsWithModels; @@ -91,6 +90,7 @@ public function getVehicleGearBoxType(): string public function getVehicleEnginePower(): string { ['range' => $range, 'unit' => $unit] = $this->vehicleData::$vehicleEnginePower; + return FakeCarHelper::getRangeWithUnit($range, $unit); } @@ -100,6 +100,7 @@ public function getVehicleEnginePower(): string public function getVehicleEnginePowerValue(): string { ['range' => $range] = $this->vehicleData::$vehicleEnginePower; + return FakeCarHelper::getRange($range); } @@ -109,16 +110,17 @@ public function getVehicleEnginePowerValue(): string public function getVehicleEngineTorque(): string { ['range' => $range, 'unit' => $unit] = $this->vehicleData::$vehicleEngineTorque; + return FakeCarHelper::getRangeWithUnit($range, $unit); } + /** * @throws Exception */ public function getVehicleEngineTorqueValue(): string { ['range' => $range] = $this->vehicleData::$vehicleEngineTorque; + return FakeCarHelper::getRange($range); } - - } diff --git a/src/FakeCarDataProviderInterface.php b/src/FakeCarDataProviderInterface.php index 0fb9f5a..683996b 100644 --- a/src/FakeCarDataProviderInterface.php +++ b/src/FakeCarDataProviderInterface.php @@ -5,12 +5,20 @@ interface FakeCarDataProviderInterface { public function getVehicleBrand(): string; + public function getVehicleModel(): string; + public function getBrandsWithModels(): array; + public function getVehicleType(): string; + public function getVehicleFuelType(): string|array; + public function getVehicleDoorCount(): int; + public function getVehicleSeatCount(): int; + public function getVehicleProperties(int $count = 0): array; + public function getVehicleGearBoxType(): string; } diff --git a/src/FakeCarHelper.php b/src/FakeCarHelper.php index e567f59..f73cd1a 100644 --- a/src/FakeCarHelper.php +++ b/src/FakeCarHelper.php @@ -28,9 +28,6 @@ public static function getArrayData(array $arrayData, int $count = 1) * Determines if an array is weighted(associative). * * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. - * - * @param array $array - * @return bool */ public static function isWeighted(array $array): bool { @@ -42,7 +39,7 @@ public static function isWeighted(array $array): bool /** * Returns a random element from a passed array * - * @param array $values + * @param array $values * * @throws Exception */ @@ -56,11 +53,11 @@ public static function getRandomElementFromArray($values) /** * Get random elements from input array * - * @param array $values The input array - * @param int $count The number of random elements you want to get in your response. - * Leave out or set to 0 for random. - * + * @param array $values The input array + * @param int $count The number of random elements you want to get in your response. + * Leave out or set to 0 for random. * @return mixed + * * @throws InvalidArgumentException|Exception */ public static function getRandomElementsFromArray(array $values, ?int $count = 1): array @@ -76,7 +73,7 @@ public static function getRandomElementsFromArray(array $values, ?int $count = 1 return []; } - if (!$count) { + if (! $count) { $count = random_int(1, $valuesLength); } @@ -92,16 +89,14 @@ public static function getRandomElementsFromArray(array $values, ?int $count = 1 * Get one element out of an input array with specified weights to get the distribution * of the generated elements as you want them. * - * @param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70] - * @param int $count + * @param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70] * - * @return string * @throws Exception */ public static function getWeighted(array $values, int $count = 1): string { $currentTotal = 0; - $firstRand = random_int(1, 100); + $firstRand = random_int(1, 100); $total = array_sum($values); @@ -119,10 +114,8 @@ public static function getWeighted(array $values, int $count = 1): string } /** - * @param array $range - * @param int $decimals + * @param array $range * - * @return string * @throws RandomException */ public static function getRange(array $range, int $decimals = 0): string @@ -149,16 +142,12 @@ public static function getRange(array $range, int $decimals = 0): string } /** - * @param array $range - * @param string $unit - * @param int $decimals + * @param array $range * - * @return string * @throws RandomException */ public static function getRangeWithUnit(array $range, string $unit, int $decimals = 0): string { - return static::getRange($range, $decimals) . ' ' . $unit; + return static::getRange($range, $decimals).' '.$unit; } - } diff --git a/tests/Pest.php b/tests/Pest.php index fdb64bb..3edb844 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -22,7 +22,6 @@ | */ - /* |-------------------------------------------------------------------------- | Functions diff --git a/tests/TestProviders/BMWFakeCarData.php b/tests/TestProviders/BMWFakeCarData.php index 29abd97..202f824 100644 --- a/tests/TestProviders/BMWFakeCarData.php +++ b/tests/TestProviders/BMWFakeCarData.php @@ -1,4 +1,5 @@ 40, 'electric' => 10, - 'diesel' => 20, + 'diesel' => 20, ]; public static $vehicleEnginePower = [ - 'range' => [150,1200], + 'range' => [150, 1200], 'unit' => 'hp', ]; public static $vehicleEngineTorque = [ - 'range' => [300,700], + 'range' => [300, 700], 'unit' => 'nm', ]; } diff --git a/tests/TestProviders/FerrariEnzoTestProvider.php b/tests/TestProviders/FerrariEnzoTestProvider.php index cb90eb2..27acd3c 100644 --- a/tests/TestProviders/FerrariEnzoTestProvider.php +++ b/tests/TestProviders/FerrariEnzoTestProvider.php @@ -1,4 +1,5 @@ setDataProvider($fakeCarDataProvider); $faker->addProvider($fakeCar); @@ -35,9 +35,9 @@ }); test('custom provider class', function () { - $fakeCarDataProvider = new FerrariEnzoTestProvider(); + $fakeCarDataProvider = new FerrariEnzoTestProvider; - $faker = (new \Faker\Factory())::create(); + $faker = (new \Faker\Factory)::create(); $fakeCar = new \Faker\Provider\FakeCar($faker); $fakeCar->setDataProvider($fakeCarDataProvider); $faker->addProvider($fakeCar); diff --git a/tests/Unit/FakeCarTest.php b/tests/Unit/FakeCarTest.php index bd6dbec..66a9ef3 100644 --- a/tests/Unit/FakeCarTest.php +++ b/tests/Unit/FakeCarTest.php @@ -1,7 +1,6 @@ getProperty($property); $reflection_property->setAccessible(true); @@ -36,7 +35,7 @@ function callProtectedMethod($args, $method, $object = null) } try { - $reflection = new \ReflectionClass($object); + $reflection = new \ReflectionClass($object); $reflectionMethod = $reflection->getMethod($method); //$reflectionMethod->setAccessible(true); @@ -50,7 +49,7 @@ function callProtectedMethod($args, $method, $object = null) $this->faker->seed(random_int(1, 9999)); $vehicleText = $this->faker->vehicle(); - $brands = (new FakeCarDataProvider)->getBrandsWithModels(); + $brands = (new FakeCarDataProvider)->getBrandsWithModels(); foreach ($brands as $brand => $models) { if (substr($vehicleText, 0, strlen($brand)) === $brand) { @@ -101,7 +100,7 @@ function callProtectedMethod($args, $method, $object = null) }); test('vehicle door count', function () { - for ($i = 0; $i<10; $i++) { + for ($i = 0; $i < 10; $i++) { expect($this->faker->vehicleSeatCount())->toBeGreaterThanOrEqual(1) ->and($this->faker->vehicleSeatCount())->toBeLessThanOrEqual(9) @@ -118,7 +117,7 @@ function callProtectedMethod($args, $method, $object = null) }); test('vehicle seat count', function () { - for ($i = 0; $i<10; $i++) { + for ($i = 0; $i < 10; $i++) { expect($this->faker->vehicleSeatCount())->toBeGreaterThanOrEqual(1) ->and($this->faker->vehicleSeatCount())->toBeLessThanOrEqual(9) @@ -156,7 +155,7 @@ function callProtectedMethod($args, $method, $object = null) expect(array_keys(FakeCarData::$vehicleGearBoxType))->toContain($this->faker->vehicleGearBoxType()); }); -test( 'get random elements from array', function () { +test('get random elements from array', function () { $data = [ 'value1', 'value2', @@ -176,7 +175,7 @@ function callProtectedMethod($args, $method, $object = null) ->and(FakeCarHelper::getRandomElementsFromArray($data, 10))->toHaveCount(10) ->and(FakeCarHelper::getRandomElementsFromArray($data, 0))->toEqual([]); - for ($i = 0; $i<50; $i++) { + for ($i = 0; $i < 50; $i++) { $result6 = FakeCarHelper::getRandomElementsFromArray($data, null); expect(count($result6))->toBeGreaterThanOrEqual(0) @@ -200,16 +199,16 @@ function callProtectedMethod($args, $method, $object = null) 'key3' => 1, ]; - for($x = 0; $x<10; $x++) { + for ($x = 0; $x < 10; $x++) { $result = array_fill_keys(array_keys($data), 0); - for ($i = 0; $i<1000; $i++) { + for ($i = 0; $i < 1000; $i++) { $result[FakeCarHelper::getWeighted($data)]++; } expect($result['key1'])->toBeGreaterThan($result['key2']) - ->and($result['key2'])->toBeGreaterThan($result['key3']) - ->and($result['key1'])->toBeGreaterThan($result['key3']); + ->and($result['key2'])->toBeGreaterThan($result['key3']) + ->and($result['key1'])->toBeGreaterThan($result['key3']); } expect(FakeCarHelper::getWeighted([]))->toEqual(''); @@ -277,14 +276,14 @@ function callProtectedMethod($args, $method, $object = null) }); test('get range', function () { - for($x = 0; $x<100; $x++) { + for ($x = 0; $x < 100; $x++) { $range = FakeCarHelper::getRange([1, 100], 0); expect($range)->toMatch('/^\d+$/') ->and((int) $range)->toBeGreaterThanOrEqual(1) ->and((int) $range)->toBeLessThanOrEqual(100); } - for($x = 0; $x<100; $x++) { + for ($x = 0; $x < 100; $x++) { $range = FakeCarHelper::getRange([100, 150], 2); expect($range)->toMatch('/^\d+\.\d+$/') @@ -301,7 +300,7 @@ function callProtectedMethod($args, $method, $object = null) }); test('get range with unit', function () { - for($x = 0; $x<100; $x++) { + for ($x = 0; $x < 100; $x++) { $range = FakeCarHelper::getRangeWithUnit([2065, 2450], 'l', 0); expect($range)->toMatch('/^\d+ l$/') @@ -309,7 +308,7 @@ function callProtectedMethod($args, $method, $object = null) ->and((int) $range)->toBeLessThanOrEqual(2450); } - for($x = 0; $x<100; $x++) { + for ($x = 0; $x < 100; $x++) { $range = FakeCarHelper::getRangeWithUnit([200, 250], 'hp', 2); expect($range)->toMatch('/^\d+\.\d+ hp$/') From fed944d4479ec2334f0a76724868e1b196bf4ead Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 01:56:07 +0200 Subject: [PATCH 14/24] Remove unstable version badge --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index e5ca41a..9cf3a7b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ 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) -[![Latest Unstable Version](https://poser.pugx.org/pelmered/fake-car/v/unstable)](//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) [![License](https://poser.pugx.org/pelmered/fake-car/license)](https://packagist.org/packages/pelmered/fake-car) From c872db5b00fd3ebda58cf0449dfaba150ce16278 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 09:50:28 +0200 Subject: [PATCH 15/24] Improve readme and update examples to use function call (direct access is deprecated) --- README.md | 56 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 9cf3a7b..369b62d 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,11 @@ Remove the `--dev` flag if you need it in production. 1. Now requires PHP 8.1+ (previously 7.3+) 2. The provider name has changed from `Fakecar` to `FakeCar`. This will cause problems if you are on a case-sensitive filesystem, but it is strongly recommended to change this even if you are not. 3. The methods `transliterate` and `checkDigit` on the `FakeCar` provider class are now no longer publicly available (Visibility changed to private). -4. The public methods `getRandomElementsFromArray` and `getWeighted` on the `FakeCar` provider class has been moved to a helper class. Access them like this: `\Faker\Provider\FakeCarHelper::getWeighted` +4. The public methods `getRandomElementsFromArray` and `getWeighted` on the `FakeCar` provider class has been moved to a helper class. Access them like this: `\Faker\Provider\FakeCarHelper::getWeighted()` 5. The constants `EBCDIC` and `MODELYEAR` are no longer public. -3, 4 and 5 are changes to limited to undocumented features of the public API and should therefore not impact the typical use cases of this package. +3, 4 and 5 are changes limited to undocumented features of the public API, +and should therefore not impact the typical use cases of this package. ## Basic Usage @@ -38,71 +39,72 @@ $faker = (new \Faker\Factory())::create(); $faker->addProvider(new \Faker\Provider\FakeCar($faker)); -// generate matching automobile brand and model of car as a string -echo $faker->vehicle; // Volvo 740 +// generate matching automobile brand and model of a car as a string +echo $faker->vehicle(); // 'Volvo 740' -// generate matching automobile brand and model of car as an array -echo $faker->vehicleArray; // [ 'brand' => 'Hummer', 'model' => 'H1' ] +// generate matching automobile brand and model of a car as an array +echo $faker->vehicleArray(); // [ 'brand' => 'Hummer', 'model' => 'H1' ] // generate only automobile brand -echo $faker->vehicleBrand; // Ford +echo $faker->vehicleBrand(); // 'Ford' // generate automobile manufacturer and model of car -echo $faker->vehicleModel; // 488 Spider +echo $faker->vehicleModel(); // '488 Spider' // generate Vehicle Identification Number(VIN) - https://en.wikipedia.org/wiki/Vehicle_identification_number -echo $faker->vin; // d0vcddxpXAcz1utgz +echo $faker->vin(); // 'd0vcddxpXAcz1utgz' // generate automobile registration number -echo $faker->vehicleRegistration; // ABC-123 +echo $faker->vehicleRegistration(); // 'ABC-123' // generate automobile registration number with custom format echo $faker->vehicleRegistration('[A-Z]{2}-[0-9]{5}'); // AB-12345 // generate automobile model type -echo $faker->vehicleType; // hatchback +echo $faker->vehicleType(); // 'hatchback' // generate automobile fuel type -echo $faker->vehicleFuelType; // diesel +echo $faker->vehicleFuelType(); // 'diesel' +echo $faker->vehicleFuelType(2); // ['diesel', 'gasoline'] // generate automobile door count -echo $faker->vehicleDoorCount; // 4 +echo $faker->vehicleDoorCount(); // 4 // generate automobile seat count -echo $faker->vehicleSeatCount; // 5 +echo $faker->vehicleSeatCount(); // 5 // generate automobile properties -echo $faker->vehicleProperties; // ['Towbar','Aircondition','GPS', 'Leather seats'] +echo $faker->vehicleProperties(); // ['Towbar','Aircondition','GPS', 'Leather seats'] // generate automobile gear type (manual or automatic) -echo $faker->vehicleGearBoxType; // manual +echo $faker->vehicleGearBoxType(); // manual // generate automobile engine power -echo $faker->vehicleEnginePower; // 250 hp +echo $faker->vehicleEnginePower(); // '250 hp' -// generate automobile engine power without unit -echo $faker->vehicleEnginePowerValue; // 175 +// generate automobile engine power without a unit +echo $faker->vehicleEnginePowerValue(); // 175 // generate automobile engine torque -echo $faker->vehicleEngineTorque; // 300 nm +echo $faker->vehicleEngineTorque(); // '300 nm' -// generate automobile engine power without unit -echo $faker->vehicleEngineTorqueValue; // 450 +// generate automobile engine power without a unit +echo $faker->vehicleEngineTorqueValue(); // 450 // generate automobile engine displacement -echo $faker->vehicleEngineDisplacement; // 2.0 l +echo $faker->vehicleEngineDisplacement(); // '2.0 l' // generate automobile engine displacement without unit -echo $faker->vehicleEngineDisplacementValue; // 2.0 +echo $faker->vehicleEngineDisplacementValue(); // 2.0 // generate automobile engine fuel consumption -echo $faker->vehicleFuelConsumption; // 5.0 l/100km +echo $faker->vehicleFuelConsumption(); // '5.0 l/100km' // generate automobile engine fuel consumption without unit -echo $faker->vehicleFuelConsumptionValue; // 5.0 +echo $faker->vehicleFuelConsumptionValue(); // 5.0 // generate automobile engine fuel consumption without unit -echo $faker->vehicleEngineCylinders; // 4 +echo $faker->vehicleEngineCylinders(); // 4 ``` ### Laravel factory example From 5c357cd7d7bcba1786e20b94c554ca829ae61c8e Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 13:55:23 +0200 Subject: [PATCH 16/24] Code refactor/improvements. Type hints etc. --- src/FakeCar.php | 87 ++++++++++++----------- src/FakeCarData.php | 97 ++++++++++++++++++-------- src/FakeCarDataProvider.php | 16 +++-- src/FakeCarDataProviderInterface.php | 13 +++- src/FakeCarHelper.php | 32 +++++---- tests/TestProviders/BMWFakeCarData.php | 10 +-- 6 files changed, 156 insertions(+), 99 deletions(-) diff --git a/src/FakeCar.php b/src/FakeCar.php index ad8b549..bbd5a53 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -36,6 +36,8 @@ public function vehicle(): string /** * Get vehicle with brand and model as an array + * + * @return array{brand: string, model: string} */ public function vehicleArray(): array { @@ -58,24 +60,21 @@ public function vehicleBrand(): string /** * Get random vehicle model * - * @param string $brand Get random model from specific brand (optional) - * @return mixed + * @param ?string $brand Get a random model from specific brand (optional) */ public function vehicleModel(?string $brand = null): string { - return (string) $this->dataProvider->getVehicleModel($brand); + return $this->dataProvider->getVehicleModel($brand); } /** * Generate VIN * * @link https://en.wikipedia.org/wiki/Vehicle_identification_number - * - * @return mixed */ public function vin(int $year = 1980): string { - $modelYear = static::modelYear($year); + $modelYear = self::encodeModelYear($year); $regex = "([a-hj-npr-z0-9]{8})_{$modelYear}([a-hj-npr-z0-9]{7})"; $vin = static::regexify($regex); @@ -92,7 +91,7 @@ public function vehicleRegistration(string $regex = '[A-Z]{3}-[0-9]{3}'): string } /** - * Get vehicle type + * Get a vehicle type * * @throws Exception */ @@ -102,11 +101,13 @@ public function vehicleType(): string } /** - * Get vehicle fuel type + * Get vehicle fuel type(s) + * + * @return string|array */ - public function vehicleFuelType(int $count = 1): string + public function vehicleFuelType(int $count = 1): string|array { - return (string) $this->dataProvider->getVehicleFuelType($count); + return $this->dataProvider->getVehicleFuelType($count); } /** @@ -116,7 +117,7 @@ public function vehicleFuelType(int $count = 1): string */ public function vehicleDoorCount(): int { - return (int) $this->dataProvider->getVehicleDoorCount(); + return $this->dataProvider->getVehicleDoorCount(); } /** @@ -126,12 +127,13 @@ public function vehicleDoorCount(): int */ public function vehicleSeatCount(): int { - return (int) $this->dataProvider->getVehicleSeatCount(); + return $this->dataProvider->getVehicleSeatCount(); } /** * Get an array of random vehicle properties * + * @return array * * @throws Exception */ @@ -143,8 +145,6 @@ public function vehicleProperties(int $count = 0): array /** * Get random vehicle gearbox type * - * @return mixed - * * @throws Exception */ public function vehicleGearBoxType(): string @@ -153,9 +153,7 @@ public function vehicleGearBoxType(): string } /** - * Get random vehicle gearbox type without unit - * - * @return mixed + * Get a random vehicle gearbox type without a unit * * @throws Exception */ @@ -167,54 +165,56 @@ public function vehicleGearBoxTypeValue(): string /** * Get engine torque * - * @return mixed - * * @throws Exception */ public function vehicleEngineTorque(): string { + //TODO: Remove check and add to data provider interface in next major version + $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEngineTorque(); } /** - * Get engine torque without unit - * - * @return mixed + * Get engine torque without a unit * * @throws Exception */ public function vehicleEngineTorqueValue(): string { - return $this->dataProvider->getVehicleEngineTorque(); + //TODO: Remove check and add to data provider interface in next major version + $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ + return $this->dataProvider->getVehicleEngineTorqueValue(); } /** * Get engine power (horsepower or kilowatts) * - * @return mixed - * * @throws Exception */ public function vehicleEnginePower(): string { + //TODO: Remove check and add to data provider interface in next major version + $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEnginePower(); } /** - * Get engine power without unit - * - * @return mixed + * Get engine power without a unit * * @throws Exception */ public function vehicleEnginePowerValue(): string { + //TODO: Remove check and add to data provider interface in next major version $this->isSupported(__FUNCTION__); - + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEnginePowerValue(); } - public function isSupported($method): bool + public function isSupported(string $method): bool { if (method_exists($this->dataProvider, $method)) { return true; @@ -223,20 +223,18 @@ public function isSupported($method): bool throw new \RuntimeException('Method not supported be data provider. Please implement '.$method.'() in your data provider.'); } - public static function modelYear(int $year = 1980): string + /** + * Model year encoding for VIN generation. + * + * @link: https://en.wikipedia.org/wiki/Vehicle_identification_number#Model_year_encoding + */ + private static function encodeModelYear(int $modelYear = 1980): string { - return substr(self::MODEL_YEAR, ($year - 1980) % 30, 1); - } - - private static function transliterate(string $character): string - { - return stripos(self::EBCDIC, $character) % 10; + return substr(self::MODEL_YEAR, ($modelYear - 1980) % 30, 1); } /**php - * @param string $vin - * - * @return mixed + * @link: https://en.wikipedia.org/wiki/Vehicle_identification_number#Check-digit_calculation */ private static function checkDigit(string $vin): string { @@ -244,19 +242,24 @@ private static function checkDigit(string $vin): string $weights = '8765432X098765432'; $sum = 0; for ($i = 0; $i < 17; $i++) { - $sum += self::transliterate(substr($vin, $i, 1)) + $sum += self::transliterate($vin[$i]) * stripos($map, $weights[$i]); } return $map[$sum % 11]; } + private static function transliterate(string $character): int + { + return stripos(self::EBCDIC, $character) % 10; + } + public static function validateVin(string $vin): bool { if (strlen($vin) !== 17) { return false; } - return self::checkDigit($vin) == $vin[8]; + return self::checkDigit($vin) === $vin[8]; } } diff --git a/src/FakeCarData.php b/src/FakeCarData.php index 0e2b607..346b2e9 100644 --- a/src/FakeCarData.php +++ b/src/FakeCarData.php @@ -4,7 +4,10 @@ class FakeCarData { - public static $brandsWithModels = [ + /** + * @var array > + */ + public static array $brandsWithModels = [ 'Abarth' => [ 'Fiat 595', ], @@ -783,72 +786,106 @@ class FakeCarData ], ]; - public static $vehicleTypes = [ + /** + * @var string[] + */ + public static array $vehicleTypes = [ 'hatchback', 'sedan', 'small', 'convertible', 'SUV', 'MPV', 'coupe', 'station wagon', ]; - // Based on 2021 EU sales data - // https://www.acea.auto/fuel-pc/fuel-types-of-new-cars-battery-electric-9-1-hybrid-19-6-and-petrol-40-0-market-share-full-year-2021/ - public static $vehicleFuelTypes = [ + /** + * Based on 2021 EU sales data + * https://www.acea.auto/fuel-pc/fuel-types-of-new-cars-battery-electric-9-1-hybrid-19-6-and-petrol-40-0-market-share-full-year-2021/ + * + * @var array + */ + public static array $vehicleFuelTypes = [ 'gasoline' => 40, 'electric' => 10, 'diesel' => 20, - 'hybrid' => 19.5, + 'hybrid' => 20, 'plugin-hybrid' => 10, - 'natural-gas' => 0.5, + 'natural-gas' => 1, ]; - public static $vehicleDoorCount = [ - '4' => 75, - '2' => 15, - '6' => 10, + /** + * @var array + */ + public static array $vehicleDoorCount = [ + 2 => 15, + 4 => 75, + 6 => 10, ]; - public static $vehicleSeatCount = [ - '5' => 75, - '2' => 15, - '8' => 5, - '4' => 10, + /** + * @var array + */ + public static array $vehicleSeatCount = [ + 2 => 15, + 4 => 10, + 5 => 75, + 8 => 5, ]; - public static $vehicleProperties = [ + /** + * @var string[] + */ + public static array $vehicleProperties = [ 'Towbar', 'Air condition', 'GPS', 'Leather seats', 'Roof Rack', ]; - public static $vehicleGearBoxType = [ + /** + * @var array + */ + public static array $vehicleGearBoxType = [ 'manual' => 70, 'automatic' => 30, ]; - public static $vehicleEnginePower = [ + /** + * @var array + */ + public static array $vehicleEnginePower = [ 'range' => [100, 1500], 'unit' => 'hp', 'rounding' => 0, ]; - public static $vehicleEngineTorque = [ + /** + * @var array + */ + public static array $vehicleEngineTorque = [ 'range' => [100, 700], 'unit' => 'nm', 'decimals' => 0, ]; - public static $vehicleEngineDisplacement = [ + /** + * @var array + */ + public static array $vehicleEngineDisplacement = [ 'range' => [500, 8000], 'unit' => 'cc', 'decimals' => 1, ]; - public static $vehicleEngineCylinders = [ - '3' => 5, - '4' => 60, - '5' => 5, - '6' => 5, - '8' => 15, - '10' => 5, - '12' => 5, + /** + * @var array + */ + public static array $vehicleEngineCylinders = [ + 3 => 5, + 4 => 60, + 5 => 5, + 6 => 5, + 8 => 15, + 10 => 5, + 12 => 5, ]; - public static $vehicleEngineFuelConsumption = [ + /** + * @var array + */ + public static array $vehicleEngineFuelConsumption = [ 'range' => [4, 30], 'unit' => 'l/100km', 'decimals' => 1, diff --git a/src/FakeCarDataProvider.php b/src/FakeCarDataProvider.php index a53efc6..d816351 100644 --- a/src/FakeCarDataProvider.php +++ b/src/FakeCarDataProvider.php @@ -6,9 +6,9 @@ class FakeCarDataProvider implements FakeCarDataProviderInterface { - protected $vehicleData; + protected mixed $vehicleData; - public function __construct($vehicleData = null) + public function __construct(mixed $vehicleData = null) { $this->vehicleData = $vehicleData ?: FakeCarData::class; } @@ -31,6 +31,9 @@ public function getVehicleModel(?string $brand = null): string return (string) FakeCarHelper::getRandomElementFromArray($brandsWithModels[$brand ?: $this->getVehicleBrand()]); } + /** + * @return array> + */ public function getBrandsWithModels(): array { return $this->vehicleData::$brandsWithModels; @@ -46,6 +49,7 @@ public function getVehicleType(): string /** * @throws Exception + * @return string|array) */ public function getVehicleFuelType(int $count = 1): string|array { @@ -69,9 +73,11 @@ public function getVehicleSeatCount(): int } /** + * @return array + * * @throws Exception */ - public function getVehicleProperties(int $count = 0): array + public function getVehicleProperties(int $count = 1): array { return FakeCarHelper::getArrayData($this->vehicleData::$vehicleProperties, $count); } @@ -97,7 +103,7 @@ public function getVehicleEnginePower(): string /** * @throws Exception */ - public function getVehicleEnginePowerValue(): string + public function getVehicleEnginePowerValue(): int|string { ['range' => $range] = $this->vehicleData::$vehicleEnginePower; @@ -117,7 +123,7 @@ public function getVehicleEngineTorque(): string /** * @throws Exception */ - public function getVehicleEngineTorqueValue(): string + public function getVehicleEngineTorqueValue(): int|string { ['range' => $range] = $this->vehicleData::$vehicleEngineTorque; diff --git a/src/FakeCarDataProviderInterface.php b/src/FakeCarDataProviderInterface.php index 683996b..4f088f5 100644 --- a/src/FakeCarDataProviderInterface.php +++ b/src/FakeCarDataProviderInterface.php @@ -6,18 +6,27 @@ interface FakeCarDataProviderInterface { public function getVehicleBrand(): string; - public function getVehicleModel(): string; + public function getVehicleModel(?string $brand = null): string; + /** + * @return array> + */ public function getBrandsWithModels(): array; public function getVehicleType(): string; - public function getVehicleFuelType(): string|array; + /** + * @return string|array) + */ + public function getVehicleFuelType(int $count = 1): string|array; public function getVehicleDoorCount(): int; public function getVehicleSeatCount(): int; + /** + * @return array + */ public function getVehicleProperties(int $count = 0): array; public function getVehicleGearBoxType(): string; diff --git a/src/FakeCarHelper.php b/src/FakeCarHelper.php index f73cd1a..cc36625 100644 --- a/src/FakeCarHelper.php +++ b/src/FakeCarHelper.php @@ -9,9 +9,11 @@ class FakeCarHelper { /** + * @param array $arrayData + * * @throws Exception */ - public static function getArrayData(array $arrayData, int $count = 1) + public static function getArrayData(array $arrayData, int $count = 1): mixed { $data = static::isWeighted($arrayData) ? static::getWeighted($arrayData, $count) @@ -26,8 +28,9 @@ public static function getArrayData(array $arrayData, int $count = 1) /** * Determines if an array is weighted(associative). - * * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. + * + * @param array $array */ public static function isWeighted(array $array): bool { @@ -39,11 +42,11 @@ public static function isWeighted(array $array): bool /** * Returns a random element from a passed array * - * @param array $values + * @param array $values * - * @throws Exception + * @throws InvalidArgumentException|RandomException */ - public static function getRandomElementFromArray($values) + public static function getRandomElementFromArray(array $values): int|string { $elements = static::getRandomElementsFromArray($values, 1); @@ -53,16 +56,15 @@ public static function getRandomElementFromArray($values) /** * Get random elements from input array * - * @param array $values The input array - * @param int $count The number of random elements you want to get in your response. - * Leave out or set to 0 for random. - * @return mixed + * @param array $values The input array + * @param int|null $count The number of random elements you want to get in your response. + * Leave out or set to 0 for random. + * @return array * - * @throws InvalidArgumentException|Exception + * @throws RandomException */ public static function getRandomElementsFromArray(array $values, ?int $count = 1): array { - //dd('getRandomElementsFromArray', $values,$count); $valuesLength = count($values); if ($count > $valuesLength) { @@ -74,7 +76,7 @@ public static function getRandomElementsFromArray(array $values, ?int $count = 1 } if (! $count) { - $count = random_int(1, $valuesLength); + $count = $valuesLength === 1 ? 1 : random_int(1, $valuesLength); } return array_intersect_key( @@ -89,11 +91,11 @@ public static function getRandomElementsFromArray(array $values, ?int $count = 1 * Get one element out of an input array with specified weights to get the distribution * of the generated elements as you want them. * - * @param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70] + * @param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70] * * @throws Exception */ - public static function getWeighted(array $values, int $count = 1): string + public static function getWeighted(array $values, int $count = 1): string|int { $currentTotal = 0; $firstRand = random_int(1, 100); @@ -118,7 +120,7 @@ public static function getWeighted(array $values, int $count = 1): string * * @throws RandomException */ - public static function getRange(array $range, int $decimals = 0): string + public static function getRange(array $range, int $decimals = 0): int|string { if (count($range) !== 2) { throw new RandomException('Invalid range'); diff --git a/tests/TestProviders/BMWFakeCarData.php b/tests/TestProviders/BMWFakeCarData.php index 202f824..81e333d 100644 --- a/tests/TestProviders/BMWFakeCarData.php +++ b/tests/TestProviders/BMWFakeCarData.php @@ -4,7 +4,7 @@ class BMWFakeCarData extends \Faker\Provider\FakeCarData { - public static $brandsWithModels = [ + public static array $brandsWithModels = [ 'BMW' => [ '8 Series', 'M1', 'X5', 'Z1', 'Z3', 'Z4', 'Z8', 'Alpina', 'E', 'X3', 'M', 'X6', '1 Series', '5 Series', 'X5 M', 'M5', '750', '6 Series', '3 Series', 'M3', 'X6 M', 'M6', 'X1', '7 Series', '325', '324', '316', @@ -17,22 +17,22 @@ class BMWFakeCarData extends \Faker\Provider\FakeCarData ], ]; - public static $vehicleTypes = [ + public static array $vehicleTypes = [ 'hatchback', 'sedan', 'convertible', 'SUV', 'coupe', ]; - public static $vehicleFuelTypes = [ + public static array $vehicleFuelTypes = [ 'gasoline' => 40, 'electric' => 10, 'diesel' => 20, ]; - public static $vehicleEnginePower = [ + public static array $vehicleEnginePower = [ 'range' => [150, 1200], 'unit' => 'hp', ]; - public static $vehicleEngineTorque = [ + public static array $vehicleEngineTorque = [ 'range' => [300, 700], 'unit' => 'nm', ]; From 39492906a4540e39b5bd77002a017db1a01e868b Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 13:56:32 +0200 Subject: [PATCH 17/24] Pint fixes --- src/FakeCar.php | 4 ++++ src/FakeCarDataProvider.php | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/FakeCar.php b/src/FakeCar.php index bbd5a53..4c91530 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -171,6 +171,7 @@ public function vehicleEngineTorque(): string { //TODO: Remove check and add to data provider interface in next major version $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEngineTorque(); } @@ -184,6 +185,7 @@ public function vehicleEngineTorqueValue(): string { //TODO: Remove check and add to data provider interface in next major version $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEngineTorqueValue(); } @@ -197,6 +199,7 @@ public function vehicleEnginePower(): string { //TODO: Remove check and add to data provider interface in next major version $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEnginePower(); } @@ -210,6 +213,7 @@ public function vehicleEnginePowerValue(): string { //TODO: Remove check and add to data provider interface in next major version $this->isSupported(__FUNCTION__); + /** @phpstan-ignore method.notFound */ return $this->dataProvider->getVehicleEnginePowerValue(); } diff --git a/src/FakeCarDataProvider.php b/src/FakeCarDataProvider.php index d816351..72206c9 100644 --- a/src/FakeCarDataProvider.php +++ b/src/FakeCarDataProvider.php @@ -48,8 +48,9 @@ public function getVehicleType(): string } /** - * @throws Exception * @return string|array) + * + * @throws Exception */ public function getVehicleFuelType(int $count = 1): string|array { From bda3fd626f43d57f3b19f30917bade4bb66114ec Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 14:03:29 +0200 Subject: [PATCH 18/24] Move test data providers --- tests/{TestProviders => TestDataProviders}/BMWFakeCarData.php | 0 .../FerrariEnzoTestProvider.php | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename tests/{TestProviders => TestDataProviders}/BMWFakeCarData.php (100%) rename tests/{TestProviders => TestDataProviders}/FerrariEnzoTestProvider.php (91%) diff --git a/tests/TestProviders/BMWFakeCarData.php b/tests/TestDataProviders/BMWFakeCarData.php similarity index 100% rename from tests/TestProviders/BMWFakeCarData.php rename to tests/TestDataProviders/BMWFakeCarData.php diff --git a/tests/TestProviders/FerrariEnzoTestProvider.php b/tests/TestDataProviders/FerrariEnzoTestProvider.php similarity index 91% rename from tests/TestProviders/FerrariEnzoTestProvider.php rename to tests/TestDataProviders/FerrariEnzoTestProvider.php index 27acd3c..62b015a 100644 --- a/tests/TestProviders/FerrariEnzoTestProvider.php +++ b/tests/TestDataProviders/FerrariEnzoTestProvider.php @@ -12,7 +12,7 @@ public function getVehicleBrand(): string return 'Ferrari'; } - public function getVehicleModel(): string + public function getVehicleModel(?string $brand = null): string { return 'Enzo'; } @@ -30,7 +30,7 @@ public function getVehicleType(): string return 'coupe'; } - public function getVehicleFuelType(): string|array + public function getVehicleFuelType(int $count = 1): string|array { return 'gasoline'; } From fbc798a5e67f17da287396fdfacc7d865393993c Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 14:03:49 +0200 Subject: [PATCH 19/24] Move test data providers --- tests/TestDataProviders/BMWFakeCarData.php | 2 +- tests/TestDataProviders/FerrariEnzoTestProvider.php | 2 +- tests/Unit/FakeCarDataProviderTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/TestDataProviders/BMWFakeCarData.php b/tests/TestDataProviders/BMWFakeCarData.php index 81e333d..25e09dd 100644 --- a/tests/TestDataProviders/BMWFakeCarData.php +++ b/tests/TestDataProviders/BMWFakeCarData.php @@ -1,6 +1,6 @@ Date: Thu, 26 Sep 2024 14:04:07 +0200 Subject: [PATCH 20/24] fix isSupported helper --- src/FakeCar.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FakeCar.php b/src/FakeCar.php index 4c91530..1898e8c 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -220,6 +220,8 @@ public function vehicleEnginePowerValue(): string public function isSupported(string $method): bool { + $method = 'get'.ucfirst($method); + if (method_exists($this->dataProvider, $method)) { return true; } From 52d00677daed29da6bb98332d42e63742f9968ea Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 14:35:22 +0200 Subject: [PATCH 21/24] Fix tests --- src/FakeCar.php | 2 +- tests/TestCase.php | 35 ++++++++++++++++++++++- tests/Unit/FakeCarTest.php | 58 ++++++++++++++------------------------ 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/FakeCar.php b/src/FakeCar.php index 1898e8c..3f5f066 100644 --- a/src/FakeCar.php +++ b/src/FakeCar.php @@ -248,7 +248,7 @@ private static function checkDigit(string $vin): string $weights = '8765432X098765432'; $sum = 0; for ($i = 0; $i < 17; $i++) { - $sum += self::transliterate($vin[$i]) + $sum += self::transliterate($vin[$i] ?? '') * stripos($map, $weights[$i]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 46d8de3..afa5a48 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,42 @@ namespace FakeCar\Tests; +use Exception; +use Faker\Provider\FakeCarDataProvider; use PHPUnit\Framework\TestCase as BaseTestCase; +use ReflectionException; abstract class TestCase extends BaseTestCase { - // + /** + * @throws Exception + */ + public function callProtectedMethod($args, $method, $object = null) + { + if (is_null($object)) { + $object = new FakeCarDataProvider; + } + + try { + $reflection = new \ReflectionClass($object); + + return $reflection->getMethod($method)->invoke($object, ...$args); + } catch (Exception $e) { + return $e->getMessage(); + } + } + + /** + * @throws ReflectionException + */ + public function getProtectedProperty($property, $object = null) + { + if (is_null($object)) { + $object = new FakeCarDataProvider; + } + + $reflection = new \ReflectionClass($object); + + return $reflection->getProperty($property)->getValue($object, $property); + } } diff --git a/tests/Unit/FakeCarTest.php b/tests/Unit/FakeCarTest.php index 66a9ef3..e6e0440 100644 --- a/tests/Unit/FakeCarTest.php +++ b/tests/Unit/FakeCarTest.php @@ -1,49 +1,22 @@ addProvider(new FakeCar($faker)); $this->faker = $faker; }); -/** - * @throws ReflectionException - */ -function getProtectedProperty($property, $object = null) -{ - if (is_null($object)) { - $object = new FakeCarDataProvider; - } - - $reflection = new \ReflectionClass($object); - $reflection_property = $reflection->getProperty($property); - $reflection_property->setAccessible(true); - return $reflection_property->getValue($object, $property); -} -function callProtectedMethod($args, $method, $object = null) -{ - if (is_null($object)) { - $object = new FakeCarDataProvider; - } - - try { - $reflection = new \ReflectionClass($object); - $reflectionMethod = $reflection->getMethod($method); - //$reflectionMethod->setAccessible(true); - - return $reflectionMethod->invoke($object, ...$args); - } catch (Exception $e) { - return $e->getMessage(); - } -} test('vehicle', function () { $this->faker->seed(random_int(1, 9999)); @@ -237,22 +210,32 @@ function callProtectedMethod($args, $method, $object = null) expect($this->faker->validateVin($vin))->toBeTrue(); }); test('model year', function () { + + $object = new FakeCar($this->faker); + + expect($this->callProtectedMethod([1980], 'encodeModelYear', $object))->toEqual('A'); + + //expect($this->callProtectedMethod([1980], 'encodeModelYear', new FakeCar($this->faker)))->toEqual('A'); + + + /* expect($this->faker->modelYear(1980))->toEqual('A') ->and($this->faker->modelYear(2000))->toEqual('Y') ->and($this->faker->modelYear(2017))->toEqual('H') ->and($this->faker->modelYear(2018))->toEqual('J') ->and($this->faker->modelYear(2019))->toEqual('K'); + */ }); test('transliterate', function () { - expect(callProtectedMethod(['O'], 'transliterate', new FakeCar($this->faker)))->toEqual(0) - ->and(callProtectedMethod(['A'], 'transliterate', new FakeCar($this->faker)))->toEqual(1) - ->and(callProtectedMethod(['K'], 'transliterate', new FakeCar($this->faker)))->toEqual(2); + 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(callProtectedMethod(['z2j9hhgr8Ahl1e3g'], 'checkDigit', new FakeCar($this->faker)))->toEqual('4') - ->and(callProtectedMethod(['n7u30vns7Ajsrb1n'], 'checkDigit', new FakeCar($this->faker)))->toEqual('1') - ->and(callProtectedMethod(['3julknxb0A06hj41'], 'checkDigit', new FakeCar($this->faker)))->toEqual('8'); + 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 () { @@ -278,7 +261,8 @@ function callProtectedMethod($args, $method, $object = null) test('get range', function () { for ($x = 0; $x < 100; $x++) { $range = FakeCarHelper::getRange([1, 100], 0); - expect($range)->toMatch('/^\d+$/') + + expect((string) $range)->toMatch('/^\d+$/') ->and((int) $range)->toBeGreaterThanOrEqual(1) ->and((int) $range)->toBeLessThanOrEqual(100); } From b8f2af04a688b2e94a30cbdd2d972921bc56eb1f Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 17:24:33 +0200 Subject: [PATCH 22/24] Imporve tests + lint fixes --- src/FakeCarHelper.php | 9 +++-- tests/TestCase.php | 11 +++--- tests/Unit/FakeCarTest.php | 72 ++++++++++++++++---------------------- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/FakeCarHelper.php b/src/FakeCarHelper.php index cc36625..ba8d8b0 100644 --- a/src/FakeCarHelper.php +++ b/src/FakeCarHelper.php @@ -15,9 +15,12 @@ class FakeCarHelper */ public static function getArrayData(array $arrayData, int $count = 1): mixed { - $data = static::isWeighted($arrayData) - ? static::getWeighted($arrayData, $count) - : static::getRandomElementsFromArray($arrayData, $count); + if (static::isWeighted($arrayData)) { + /** @var array $arrayData */ + $data = static::getWeighted($arrayData, $count); + } else { + $data = static::getRandomElementsFromArray($arrayData, $count); + } if (is_array($data) && $count === 1) { return array_values($data)[0]; diff --git a/tests/TestCase.php b/tests/TestCase.php index afa5a48..872f1d1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,9 +9,6 @@ abstract class TestCase extends BaseTestCase { - /** - * @throws Exception - */ public function callProtectedMethod($args, $method, $object = null) { if (is_null($object)) { @@ -36,8 +33,12 @@ public function getProtectedProperty($property, $object = null) $object = new FakeCarDataProvider; } - $reflection = new \ReflectionClass($object); + try { + $reflection = new \ReflectionClass($object); - return $reflection->getProperty($property)->getValue($object, $property); + return $reflection->getProperty($property)->getValue($object, $property); + } catch (Exception $e) { + return $e->getMessage(); + } } } diff --git a/tests/Unit/FakeCarTest.php b/tests/Unit/FakeCarTest.php index e6e0440..6146399 100644 --- a/tests/Unit/FakeCarTest.php +++ b/tests/Unit/FakeCarTest.php @@ -15,9 +15,6 @@ $this->faker = $faker; }); - - - test('vehicle', function () { $this->faker->seed(random_int(1, 9999)); @@ -111,17 +108,17 @@ expect($properties)->toBeArray(); $properties = $this->faker->vehicleProperties(2); - expect($properties)->toBeArray(); - expect($properties)->toHaveCount(2); + expect($properties)->toBeArray() + ->and($properties)->toHaveCount(2); $properties = $this->faker->vehicleProperties(5); - expect($properties)->toBeArray(); - expect($properties)->toHaveCount(5); + expect($properties)->toBeArray() + ->and($properties)->toHaveCount(5); - //If we pass 0 we should get a random + // If we pass 0, we should get a random property $properties = $this->faker->vehicleProperties(0); - expect($properties)->toBeArray(); - expect(count($properties))->toBeGreaterThanOrEqual(0); + expect($properties)->toBeArray() + ->and(count($properties))->toBeGreaterThanOrEqual(0); }); test('vehicle gear box', function () { @@ -187,45 +184,36 @@ expect(FakeCarHelper::getWeighted([]))->toEqual(''); }); -test('valid vin', function () { - //Too short - expect($this->faker->validateVin('z2j9hhgr8Ahl1e3g'))->toBeFalse() - //Too long - ->and($this->faker->validateVin('az2j9hhgr8Ahl1e3gs'))->toBeFalse() - //Invalid check digit - ->and($this->faker->validateVin('z2j9hhgr2Ahl1e3gs'))->toBeFalse() - //Invalid - ->and($this->faker->validateVin('z2j9hhgr8Ahl1e3gd'))->toBeFalse() - // Valid VINs - ->and($this->faker->validateVin('z2j9hhgr8Ahl1e3gs'))->toBeTrue() - ->and($this->faker->validateVin('n7u30vns7Ajsrb1nc'))->toBeTrue() - ->and($this->faker->validateVin('3julknxb0A06hj41x'))->toBeTrue() - ->and($this->faker->validateVin('yj12c8z40Aca2x6p3'))->toBeTrue() - ->and($this->faker->validateVin('y95wf7gm1A9g7pz5z'))->toBeTrue() - ->and($this->faker->validateVin('355430557Azf4u0vr'))->toBeTrue(); -}); +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 () { - +test('model year', function ($year, $expected) { $object = new FakeCar($this->faker); - expect($this->callProtectedMethod([1980], 'encodeModelYear', $object))->toEqual('A'); - - //expect($this->callProtectedMethod([1980], 'encodeModelYear', new FakeCar($this->faker)))->toEqual('A'); - - - /* - expect($this->faker->modelYear(1980))->toEqual('A') - ->and($this->faker->modelYear(2000))->toEqual('Y') - ->and($this->faker->modelYear(2017))->toEqual('H') - ->and($this->faker->modelYear(2018))->toEqual('J') - ->and($this->faker->modelYear(2019))->toEqual('K'); - */ -}); + 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) From 2dd71c8e26b114bbe69ebdb7e3d0b273522525aa Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 17:26:42 +0200 Subject: [PATCH 23/24] Add auto commit to code style workflow --- .github/workflows/code-style.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index 31b1aad..ad56af4 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -13,13 +13,14 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Get changed files - id: changed-files - uses: tj-actions/changed-files@v44 - - name: Run Laravel Pint uses: aglipanci/laravel-pint-action@latest with: verboseMode: true - testMode: true configPath: ./pint.json + onlyDirty: true + + - name: Commit linted files + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "Fixes coding style" From 29d7869f93ca8c35ec5f8325dd51ad86cd8a32d7 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 26 Sep 2024 17:38:26 +0200 Subject: [PATCH 24/24] Improve null check --- src/FakeCarHelper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/FakeCarHelper.php b/src/FakeCarHelper.php index ba8d8b0..d959d14 100644 --- a/src/FakeCarHelper.php +++ b/src/FakeCarHelper.php @@ -78,7 +78,7 @@ public static function getRandomElementsFromArray(array $values, ?int $count = 1 return []; } - if (! $count) { + if ($count === null) { $count = $valuesLength === 1 ? 1 : random_int(1, $valuesLength); } @@ -100,6 +100,7 @@ public static function getRandomElementsFromArray(array $values, ?int $count = 1 */ public static function getWeighted(array $values, int $count = 1): string|int { + // TODO: Implement support for $count > 1 $currentTotal = 0; $firstRand = random_int(1, 100);