Skip to content

Commit

Permalink
feat: added tests
Browse files Browse the repository at this point in the history
feat: added tests
  • Loading branch information
lukas-frey authored Jul 3, 2024
2 parents 85ea90a + 207b301 commit 1bdba5a
Show file tree
Hide file tree
Showing 93 changed files with 6,021 additions and 1,255 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/node_modules
/.idea
**/.DS_Store
/.phpunit.result.cache
/build
3 changes: 3 additions & 0 deletions .phpunit-watcher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
notifications:
passingTests: false
failingTests: false
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ You can install the package with composer:
composer require guava/laravel-populator
```


You can optionally publish the database migrations if you plan to enable [tracking](#tracking-inserted-models)

```bash
php artisan vendor:publish --provider 'Guava\LaravelPopulator\PopulatorServiceProvider' --tag 'migrations'
php artisan migrate
```

or publish the config

```bash
php artisan vendor:publish --provider 'Guava\LaravelPopulator\PopulatorServiceProvider' --tag 'config'
```

which currently provides the following options

**config/populator.php**
```php
return [
'tracking' => false,
'population_model' => '\Guava\LaravelPopulator\Models\Population',
];
```

## How it works

There are three major terms that Laravel Populator introduces:
Expand Down Expand Up @@ -137,6 +161,42 @@ Populator::make('v1')

This will call your populator and all it's defined bundles (more information in the Bundles section)

### Reversing a populator
The records inserted by a populator can be removed if [tracking](#enabling-tracking) is enabled when the populator was run.

```php
Populator::make('v1')
->bundles([//your bundles to reverse or leave blank for all bundles in the populator])
->rollback()
```

Rollbacks will filter using the following condition

1. Population populator name
2. Bundle model classes
3. Bundle name

This allows you to control what bundles are rolled back from a populator.

For example you can rollback a subset of the bundles from the populator

```php

Populator::make('initial')
->bundles([
Bundle::make(User::class),
Bundle::make(Post::class),
])
->call();
//User and Post entries now exist

Populator::make('initial')
->bundles([Bundle::make(Post::class)])
->rollback();
//Post entries were removed
```


### Environment
Populators can be set to be executed only on specific environments. You might most likely want to seed different data for your local environment and your production environment.

Expand Down Expand Up @@ -211,6 +271,22 @@ Bundle::make(User::class)
...
```

## Override insert behavior

If you need to customize the insertion behavior for records you can call `performInsertUsing()` on a bundle.

For example, to perform an updateOrCreate instead of an insert

```php
Bundle::make(User::class)
->performInsertUsing(function (array $data, Bundle $bundle) {
return $bundle->model::updateOrCreate(
Arr::only($data, ['email']),
Arr::except($data, ['email'])
)->getKey();
});
```

## Relations
Records can of course have relations with other records. Currently supported relations are:

Expand Down Expand Up @@ -342,6 +418,85 @@ return [
```
This will automatically create two Like's with the defined attributes and a relationship to the post they have been created in.

## Tracking inserted models

Inserted models by a populator can be tracked in the database by enabling the tracking feature in Laravel populator.

```php
\Guava\LaravelPopulator\Models\Population::first()
->populatable; //provides access to the model that was inserted by Laravel populator.
```

Laravel populator provides a trait `[HasPopulation.php](src%2FConcerns%2FHasPopulation.php)` to access
the Population from models

```php
class User extends Model implements TracksPopulatedEntries
{
use HasPopulation;
}
```

The model then has access to the Population relationship by `$model->population`

### Enabling tracking
Tracking can be enabled either by enabling the feature inside the published configuration file or by enabling it
in the boot method of a service provider.

*Enabling by config*:

**config/populator.php**
```php
return [
'tracking' => true,
//...other options
];
```

*Enabling by provider*:

**AppServiceProvider.php**
```php
public function boot() {
\Guava\LaravelPopulator\Facades\Feature::enableTrackingFeature();
}
```

### Marking models for tracking

Models must implement [TracksPopulatedEntries.php](src%2FContracts%2FTracksPopulatedEntries.php) to opt in for saving populations.

```php
class User extends Model implements TracksPopulatedEntries
{
}
```

### Swapping the Population model

The population model can be changed by setting the model class inside the published configuration file or by enabling it
in the boot method of a service provider

*Enabling by config*:

**config/populator.php**
```php
return [
'population_model' => SomeModel::class,
//...other options
];
```

*Enabling by provider*:

**AppServiceProvider.php**
```php
public function boot() {
\Guava\LaravelPopulator\Facades\Feature::customPopulationModel(SomeModel::class);
}
```


## Other packages
- [Filament Icon Picker](https://github.com/LukasFreyCZ/filament-icon-picker)
- [Filament Drafts](https://github.com/GuavaCZ/filament-drafts)
22 changes: 20 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@
"email": "[email protected]"
}
],
"require": {},
"scripts": {
"analyze": "vendor/bin/phpstan analyze",
"format": "vendor/bin/pint",
"test": "vendor/bin/phpunit",
"coverage": "vendor/bin/phpunit -d xdebug.mode=coverage",
"dev-test": "vendor/bin/phpunit-watcher watch"
},
"require-dev": {
"orchestra/testbench": "^8.0"
"orchestra/testbench": "^8.0",
"phpstan/phpstan": "^1.11",
"larastan/larastan": "^2.9",
"spatie/phpunit-watcher": "^1.24",
"laravel/pint": "^1.16"
},
"autoload": {
"psr-4": {
"Guava\\LaravelPopulator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Guava\\LaravelPopulator\\": "src/",
"Guava\\LaravelPopulator\\Database\\Factories\\": "database/factories",
"Tests\\Database\\Factories\\": "tests/Fixtures/database/factories"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
Loading

0 comments on commit 1bdba5a

Please sign in to comment.