Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2.0 #25

Merged
merged 25 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.3, 7.4, 8.0, 8.1]
os: [ubuntu-latest, windows-latest, macos-latest]
php: [8.1, 8.2, 8.3]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -37,7 +37,7 @@ jobs:
- name: Execute tests
run: vendor/bin/phpunit --coverage-clover=coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ coverage.clover
.php-cs-fixer.cache
ocular.phar
reports
outdated.txt
.phpunit.cache/
coverage/
10 changes: 9 additions & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
build:
nodes:
my-tests:
environment:
php:
version: 8.3

filter:
excluded_paths:
- bin/*
Expand Down Expand Up @@ -42,4 +49,5 @@ tools:
enabled: true
command: hhvm
extensions:
- php
- php

152 changes: 149 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ composer require pelmered/fake-car --dev
```
Remove the `--dev` flag if you need it in production.

## Upgrade to 2.x from 1.x

### Breaking changes:
1. Now requires PHP 8.1+ (previously 7.3+)
2. The provider name has changed from `Fakear` 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`
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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add all BCs

## Basic Usage

```php
$faker = (new \Faker\Factory())::create();
$faker->addProvider(new \Faker\Provider\Fakecar($faker));
$faker->addProvider(new \Faker\Provider\FakeCar($faker));


// generate matching automobile brand and model of car as a string
Expand Down Expand Up @@ -71,7 +83,7 @@ echo $faker->vehicleGearBoxType; //manual
namespace Database\Factories;

use App\Models\Vehicle;
use Faker\Provider\Fakecar;
use Faker\Provider\FakeCar;
use Illuminate\Database\Eloquent\Factories\Factory;

class VehicleFactory extends Factory
Expand All @@ -90,7 +102,7 @@ class VehicleFactory extends Factory
*/
public function definition()
{
$this->faker->addProvider(new Fakecar($this->faker));
$this->faker->addProvider(new FakeCar($this->faker));
$vehicle = $this->faker->vehicleArray();

return [
Expand All @@ -106,3 +118,137 @@ class VehicleFactory extends Factory
}
}
```

## Bring your own data

To bring you own data or override the default you can just provide your own data provider.

### Option 1: Provide your own data object:

First, create the data object:
```php
<?php
class BMWFakeCarData extends \Faker\Provider\FakeCarData
{
public static $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',
'320', '318', '328', '523', '740', '520', '728', '525', 'Isetta', '530', '528', '545', '535', 'Dixi',
'730', '745', '518', '524', '540', '116', '118', '120', '123', '125', '130', '135', '323', '330', '335',
'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'
],
];

public static $vehicleTypes = [
'hatchback', 'sedan', 'convertible', 'SUV', 'coupe',
];

public static $vehicleFuelTypes = [
'gasoline' => 40,
'electric' => 10,
'diesel' => 20,
];
}
```

And then add it to faker like this:
```php
$fakeCarDataProvider = new \Faker\Provider\FakeCarDataProvider(new BMWFakeCarData);

$faker = (new \Faker\Factory())::create();
$fakeCar = new \Faker\Provider\FakeCar($faker);
$fakeCar->setDataProvider($fakeCarDataProvider);
$faker->addProvider($fakeCar);

echo $faker->vehicleBrand; // BMW
```

### Option 2: Provide your own data provider:
```php
<?php
namespace FakeCar\Tests\TestProviders;

use Faker\Provider\FakeCarDataProviderInterface;
use Faker\Provider\FakeCarHelper;

class FerrariEnzoTestProvider implements FakeCarDataProviderInterface
{

public function getVehicleBrand(): string
{
return 'Ferrari';
}

public function getVehicleModel(): string
{
return 'Enzo';
}

public function getBrandsWithModels(): array
{
return [
'brand' => $this->getVehicleBrand(),
'model' => $this->getVehicleModel(),
];
}

public function getVehicleType(): string
{
return 'coupe';
}

public function getVehicleFuelType(): string|array
{
return 'gasoline';
}

public function getVehicleDoorCount(): int
{
return 2;
}

public function getVehicleSeatCount(): int
{
return 2;
}

public function getVehicleProperties(int $count = 0): array
{
return [
'Air condition',
'GPS',
'Leather seats',
];
}

public function getVehicleGearBoxType(): string
{
return FakeCarHelper::getWeighted([
'manual' => 70,
'automatic' => 30,
]);
}

}
```

And then add the provider to faker:
```php

$fakeCarDataProvider = new FerrariEnzoTestProvider();

$faker = (new \Faker\Factory())::create();
$fakeCar = new \Faker\Provider\FakeCar($faker);
$fakeCar->setDataProvider($fakeCarDataProvider);
$faker->addProvider($fakeCar);

echo $faker->vehicleBrand; // Ferrari
echo $faker->vehicleModel; // Enzo
```

Check the [FakeCarDataProviderTest]() for more examples.

10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
}
],
"require": {
"php": "^7.3||^8.0||^8.1",
"php": "^8.1",
"fakerphp/faker": "^1.10"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^10.1",
"larapack/dd": "^1.1",
"brianium/paratest": "^7.1"
},
"autoload": {
"psr-4": {
Expand All @@ -30,15 +32,15 @@
},
"autoload-dev": {
"psr-4": {
"Faker\\Tests\\Provider\\": "tests/Faker"
"FakeCar\\Tests\\": "tests"
}
},
"scripts": {
"test": [
"./vendor/bin/phpunit -c phpunit.xml"
],
"test-coverage": [
"./vendor/bin/phpunit --coverage-text -c phpunit.xml"
"./vendor/bin/phpunit -c phpunit.xml --coverage-text"
],
"phpmd": [
"php ./vendor/bin/phpmd src ansi ./phpmd-ruleset.xml --ignore-violations-on-exit"
Expand Down
30 changes: 23 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="tests">
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
</include>
</coverage>
</source>
<coverage includeUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true">
<report>
<clover outputFile="coverage/clover.xml"/>
<html outputDirectory="coverage/html" lowUpperBound="50" highLowerBound="90"/>
<text outputFile="coverage/coverage.txt" showUncoveredFiles="false" />
<xml outputDirectory="coverage/xml"/>
</report>
</coverage>
</phpunit>
Loading
Loading