Skip to content

Commit 1bdba5a

Browse files
authored
feat: added tests
feat: added tests
2 parents 85ea90a + 207b301 commit 1bdba5a

File tree

93 files changed

+6021
-1255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+6021
-1255
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/node_modules
33
/.idea
44
**/.DS_Store
5+
/.phpunit.result.cache
6+
/build

.phpunit-watcher.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
notifications:
2+
passingTests: false
3+
failingTests: false

README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ You can install the package with composer:
1818
composer require guava/laravel-populator
1919
```
2020

21+
22+
You can optionally publish the database migrations if you plan to enable [tracking](#tracking-inserted-models)
23+
24+
```bash
25+
php artisan vendor:publish --provider 'Guava\LaravelPopulator\PopulatorServiceProvider' --tag 'migrations'
26+
php artisan migrate
27+
```
28+
29+
or publish the config
30+
31+
```bash
32+
php artisan vendor:publish --provider 'Guava\LaravelPopulator\PopulatorServiceProvider' --tag 'config'
33+
```
34+
35+
which currently provides the following options
36+
37+
**config/populator.php**
38+
```php
39+
return [
40+
'tracking' => false,
41+
'population_model' => '\Guava\LaravelPopulator\Models\Population',
42+
];
43+
```
44+
2145
## How it works
2246

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

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

164+
### Reversing a populator
165+
The records inserted by a populator can be removed if [tracking](#enabling-tracking) is enabled when the populator was run.
166+
167+
```php
168+
Populator::make('v1')
169+
->bundles([//your bundles to reverse or leave blank for all bundles in the populator])
170+
->rollback()
171+
```
172+
173+
Rollbacks will filter using the following condition
174+
175+
1. Population populator name
176+
2. Bundle model classes
177+
3. Bundle name
178+
179+
This allows you to control what bundles are rolled back from a populator.
180+
181+
For example you can rollback a subset of the bundles from the populator
182+
183+
```php
184+
185+
Populator::make('initial')
186+
->bundles([
187+
Bundle::make(User::class),
188+
Bundle::make(Post::class),
189+
])
190+
->call();
191+
//User and Post entries now exist
192+
193+
Populator::make('initial')
194+
->bundles([Bundle::make(Post::class)])
195+
->rollback();
196+
//Post entries were removed
197+
```
198+
199+
140200
### Environment
141201
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.
142202

@@ -211,6 +271,22 @@ Bundle::make(User::class)
211271
...
212272
```
213273

274+
## Override insert behavior
275+
276+
If you need to customize the insertion behavior for records you can call `performInsertUsing()` on a bundle.
277+
278+
For example, to perform an updateOrCreate instead of an insert
279+
280+
```php
281+
Bundle::make(User::class)
282+
->performInsertUsing(function (array $data, Bundle $bundle) {
283+
return $bundle->model::updateOrCreate(
284+
Arr::only($data, ['email']),
285+
Arr::except($data, ['email'])
286+
)->getKey();
287+
});
288+
```
289+
214290
## Relations
215291
Records can of course have relations with other records. Currently supported relations are:
216292

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

421+
## Tracking inserted models
422+
423+
Inserted models by a populator can be tracked in the database by enabling the tracking feature in Laravel populator.
424+
425+
```php
426+
\Guava\LaravelPopulator\Models\Population::first()
427+
->populatable; //provides access to the model that was inserted by Laravel populator.
428+
```
429+
430+
Laravel populator provides a trait `[HasPopulation.php](src%2FConcerns%2FHasPopulation.php)` to access
431+
the Population from models
432+
433+
```php
434+
class User extends Model implements TracksPopulatedEntries
435+
{
436+
use HasPopulation;
437+
}
438+
```
439+
440+
The model then has access to the Population relationship by `$model->population`
441+
442+
### Enabling tracking
443+
Tracking can be enabled either by enabling the feature inside the published configuration file or by enabling it
444+
in the boot method of a service provider.
445+
446+
*Enabling by config*:
447+
448+
**config/populator.php**
449+
```php
450+
return [
451+
'tracking' => true,
452+
//...other options
453+
];
454+
```
455+
456+
*Enabling by provider*:
457+
458+
**AppServiceProvider.php**
459+
```php
460+
public function boot() {
461+
\Guava\LaravelPopulator\Facades\Feature::enableTrackingFeature();
462+
}
463+
```
464+
465+
### Marking models for tracking
466+
467+
Models must implement [TracksPopulatedEntries.php](src%2FContracts%2FTracksPopulatedEntries.php) to opt in for saving populations.
468+
469+
```php
470+
class User extends Model implements TracksPopulatedEntries
471+
{
472+
}
473+
```
474+
475+
### Swapping the Population model
476+
477+
The population model can be changed by setting the model class inside the published configuration file or by enabling it
478+
in the boot method of a service provider
479+
480+
*Enabling by config*:
481+
482+
**config/populator.php**
483+
```php
484+
return [
485+
'population_model' => SomeModel::class,
486+
//...other options
487+
];
488+
```
489+
490+
*Enabling by provider*:
491+
492+
**AppServiceProvider.php**
493+
```php
494+
public function boot() {
495+
\Guava\LaravelPopulator\Facades\Feature::customPopulationModel(SomeModel::class);
496+
}
497+
```
498+
499+
345500
## Other packages
346501
- [Filament Icon Picker](https://github.com/LukasFreyCZ/filament-icon-picker)
347502
- [Filament Drafts](https://github.com/GuavaCZ/filament-drafts)

composer.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,33 @@
1313
"email": "[email protected]"
1414
}
1515
],
16-
"require": {},
16+
"scripts": {
17+
"analyze": "vendor/bin/phpstan analyze",
18+
"format": "vendor/bin/pint",
19+
"test": "vendor/bin/phpunit",
20+
"coverage": "vendor/bin/phpunit -d xdebug.mode=coverage",
21+
"dev-test": "vendor/bin/phpunit-watcher watch"
22+
},
1723
"require-dev": {
18-
"orchestra/testbench": "^8.0"
24+
"orchestra/testbench": "^8.0",
25+
"phpstan/phpstan": "^1.11",
26+
"larastan/larastan": "^2.9",
27+
"spatie/phpunit-watcher": "^1.24",
28+
"laravel/pint": "^1.16"
1929
},
2030
"autoload": {
2131
"psr-4": {
2232
"Guava\\LaravelPopulator\\": "src/"
2333
}
2434
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Tests\\": "tests/",
38+
"Guava\\LaravelPopulator\\": "src/",
39+
"Guava\\LaravelPopulator\\Database\\Factories\\": "database/factories",
40+
"Tests\\Database\\Factories\\": "tests/Fixtures/database/factories"
41+
}
42+
},
2543
"extra": {
2644
"laravel": {
2745
"providers": [

0 commit comments

Comments
 (0)