-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned out database dir; added faker fixtures
- Loading branch information
1 parent
cbc4340
commit b296c82
Showing
7 changed files
with
177 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Database\DataFixtures\Faker\Faker; | ||
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | ||
use Doctrine\Common\DataFixtures\Purger\ORMPurger; | ||
use Doctrine\ORM\EntityManager; | ||
|
||
/** | ||
* In the default values listed below, ORM fixtures are configured. You may | ||
* configure other Doctrine fixture group types: | ||
* | ||
* ORMExecutor | PHPCRExecutor | MongoDBExecutor | ||
* ORMPurger | PHPCRPurger | MongoDBPurger | ||
*/ | ||
|
||
// The order of fixtures in the fixtures array is not the order in which | ||
// they will be executed. See | ||
// https://github.com/doctrine/data-fixtures#fixture-ordering | ||
return [ | ||
'default' => [ // Group name | ||
'objectManager' => EntityManager::class, | ||
'executor' => ORMExecutor::class, | ||
'purger' => ORMPurger::class, | ||
'fixtures' => [], | ||
], | ||
'faker' => [ // Group name | ||
'objectManager' => EntityManager::class, | ||
'executor' => ORMExecutor::class, | ||
'purger' => ORMPurger::class, | ||
'fixtures' => [Faker::class], | ||
], | ||
]; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\DataFixtures\Faker; | ||
|
||
use App\Doctrine\ORM\Entity\Artist as ArtistEntity; | ||
use DateTime; | ||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Doctrine\Laminas\Hydrator\DoctrineObject; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Exception; | ||
|
||
/** | ||
* DataFixtures MAY be used to "fake" data. | ||
* Sometimes unit tests are dependent on fake data. | ||
*/ | ||
final class Faker implements | ||
FixtureInterface | ||
{ | ||
public function load(ObjectManager $manager): void | ||
{ | ||
$data = [ | ||
[ | ||
'name' => 'Grateful Dead', | ||
'performances' => [ | ||
[ | ||
'performanceDate' => new DateTime('1995-02-21 00:00:00'), | ||
'venue' => 'Delta Center', | ||
'city' => 'Salt Lake City', | ||
'state' => 'UT', | ||
'recordings' => [ | ||
[ | ||
'source' => 'SBD> D> CD-R> EAC> SHN; via Jay Serafin, Brian ' | ||
. 'Walker; see info file and pub comments for notes; ' | ||
. 'possibly "click track" audible on a couple tracks', | ||
], | ||
['source' => 'DSBD > 1C > DAT; Seeded to etree by Dan Stephens'], | ||
], | ||
], | ||
[ | ||
'performanceDate' => new DateTime('1969-11-08 00:00:00'), | ||
'venue' => 'Fillmore Auditorium', | ||
'city' => 'San Francisco', | ||
'state' => 'CA', | ||
], | ||
[ | ||
'performanceDate' => new DateTime('1977-05-08 00:00:00'), | ||
'venue' => 'Barton Hall, Cornell University', | ||
'city' => 'Ithaca', | ||
'state' => 'NY', | ||
], | ||
[ | ||
'performanceDate' => new DateTime('1995-07-09 00:00:00'), | ||
'venue' => 'Soldier Field', | ||
'city' => 'Chicago', | ||
'state' => 'IL', | ||
], | ||
[ | ||
'performanceDate' => new DateTime('1995-08-09 00:00:00'), | ||
], | ||
], | ||
], | ||
[ | ||
'name' => 'Phish', | ||
'performances' => [ | ||
[ | ||
'performanceDate' => new DateTime('1998-11-02 00:00:00'), | ||
'venue' => 'The E Centre', | ||
'city' => 'West Valley City', | ||
'state' => 'UT', | ||
'recordings' => [ | ||
['source' => 'AKG480 > Aerco preamp > SBM-1'], | ||
], | ||
], | ||
[ | ||
'performanceDate' => new DateTime('1999-12-31 00:00:00'), | ||
'city' => 'Big Cypress', | ||
'state' => 'FL', | ||
], | ||
], | ||
], | ||
[ | ||
'name' => 'String Cheese Incident', | ||
'performances' => [ | ||
[ | ||
'performanceDate' => new DateTime('2002-06-21 00:00:00'), | ||
'venue' => 'Bonnaroo', | ||
'city' => 'Manchester', | ||
'state' => 'TN', | ||
], | ||
], | ||
], | ||
[ | ||
'name' => 'The Beatles', | ||
'performances' => [ | ||
[ | ||
'venue' => 'The Ed Sullivan Show', | ||
'city' => 'New York', | ||
'state' => 'NY', | ||
'performanceDate' => new DateTime('1964-02-09 00:00:00'), | ||
], | ||
], | ||
], | ||
]; | ||
|
||
/** | ||
* Use the DoctrineObject hydrator to hydrate the entity. | ||
* This maintains a consistent pattern in all DataFixtures. | ||
*/ | ||
$hydrator = new DoctrineObject($manager, false); | ||
|
||
foreach ($data as $row) { | ||
$artist = $manager | ||
->getRepository(ArtistEntity::class) | ||
->findOneBy(['name' => $row['name']]); | ||
|
||
if ($artist) { | ||
throw new Exception('Faker data already exists in the database. Aborting.'); | ||
} | ||
|
||
$artist = new ArtistEntity(); | ||
|
||
// Magic? No! The hydrator understands the entity and its associations. | ||
$hydrator->hydrate($row, $artist); | ||
$manager->persist($artist); | ||
|
||
foreach ($artist->performances as $performance) { | ||
$performance->artist = $artist; | ||
$manager->persist($performance); | ||
|
||
foreach ($performance->recordings as $recording) { | ||
$recording->performance = $performance; | ||
$manager->persist($recording); | ||
} | ||
} | ||
} | ||
|
||
$manager->flush(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
database/migrations/0001_01_01_000000_create_users_table.php
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
database/migrations/0001_01_01_000001_create_cache_table.php
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
database/migrations/0001_01_01_000002_create_jobs_table.php
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.