Skip to content

Commit

Permalink
Cleaned out database dir; added faker fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Oct 23, 2024
1 parent cbc4340 commit b296c82
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 208 deletions.
36 changes: 36 additions & 0 deletions config/doctrine-data-fixtures.php
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],
],
];


141 changes: 141 additions & 0 deletions database/data-fixtures/Faker/Faker.php
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();
}
}
44 changes: 0 additions & 44 deletions database/factories/UserFactory.php

This file was deleted.

49 changes: 0 additions & 49 deletions database/migrations/0001_01_01_000000_create_users_table.php

This file was deleted.

35 changes: 0 additions & 35 deletions database/migrations/0001_01_01_000001_create_cache_table.php

This file was deleted.

57 changes: 0 additions & 57 deletions database/migrations/0001_01_01_000002_create_jobs_table.php

This file was deleted.

23 changes: 0 additions & 23 deletions database/seeders/DatabaseSeeder.php

This file was deleted.

0 comments on commit b296c82

Please sign in to comment.