Skip to content

Commit

Permalink
Imporve tests + lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 26, 2024
1 parent 52d0067 commit b8f2af0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 50 deletions.
9 changes: 6 additions & 3 deletions src/FakeCarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int|string, int> $arrayData */
$data = static::getWeighted($arrayData, $count);
} else {
$data = static::getRandomElementsFromArray($arrayData, $count);
}

if (is_array($data) && $count === 1) {
return array_values($data)[0];
Expand Down
11 changes: 6 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

abstract class TestCase extends BaseTestCase
{
/**
* @throws Exception
*/
public function callProtectedMethod($args, $method, $object = null)
{
if (is_null($object)) {
Expand All @@ -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();
}
}
}
72 changes: 30 additions & 42 deletions tests/Unit/FakeCarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
$this->faker = $faker;
});




test('vehicle', function () {
$this->faker->seed(random_int(1, 9999));

Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b8f2af0

Please sign in to comment.