Skip to content

Commit

Permalink
Merge pull request #23 from TomHAnderson/feature/fixtures2
Browse files Browse the repository at this point in the history
Feature/fixtures2
  • Loading branch information
TomHAnderson authored Apr 11, 2024
2 parents f32748d + ee2a236 commit 1cddc1f
Show file tree
Hide file tree
Showing 83 changed files with 1,241 additions and 948 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ldog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ jobs:
env:
DB_PORT: ${{ job.services.mysql.ports['3306'] }}

- name: Import Fixturess
run: php artisan doctrine:data-fixtures:import default
- name: Import Fixtures
run: php artisan doctrine:data-fixtures:import faker
env:
DB_PORT: ${{ job.services.mysql.ports['3306'] }}

Expand Down
139 changes: 139 additions & 0 deletions app/Doctrine/ORM/DataFixtures/Faker/Faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

declare(strict_types=1);

namespace App\Doctrine\ORM\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',
'recordings' => [
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1995-02-21'),
'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' => DateTime::createFromFormat('Y-m-d', '1969-11-08'),
'venue' => 'Fillmore Auditorium',
'city' => 'San Francisco',
'state' => 'CA',
],
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1977-05-08'),
'venue' => 'Barton Hall, Cornell University',
'city' => 'Ithaca',
'state' => 'NY',
],
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1995-07-09'),
'venue' => 'Soldier Field',
'city' => 'Chicago',
'state' => 'IL',
],
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1995-08-09'),
],
],
],
[
'name' => 'Phish',
'performances' => [
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1998-11-02'),
'venue' => 'The E Centre',
'city' => 'West Valley City',
'state' => 'UT',
'recordings' => [
['source' => 'AKG480 > Aerco preamp > SBM-1'],
],
],
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1999-12-31'),
'city' => 'Big Cypress',
'state' => 'FL',
],
],
],
[
'name' => 'String Cheese Incident',
'performances' => [
[
'performanceDate' => DateTime::createFromFormat('Y-m-d', '2002-06-21'),
'venue' => 'Bonnaroo',
'city' => 'Manchester',
'state' => 'TN',
],
],
],
[
'name' => 'The Beatles',
'performances' => [
[
'venue' => 'The Ed Sullivan Show',
'city' => 'New York',
'state' => 'NY',
'performanceDate' => DateTime::createFromFormat('Y-m-d', '1964-02-09'),
],
],
],
];

/**
* Use the DoctrineObject hydrator to hydrate the entity.
* This maintains a consistent pattern in all DataFixtures.
*/
$hydrator = new DoctrineObject($manager);

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->getPerformances() as $performance) {
$manager->persist($performance);

foreach ($performance->getRecordings() as $recording) {
$manager->persist($recording);
}
}
}

$manager->flush();
}
}
19 changes: 19 additions & 0 deletions app/Doctrine/ORM/Entity/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,23 @@ public function getPerformances(): Collection
{
return $this->performances;
}

public function addPerformances(ArrayCollection $performances): self
{
foreach ($performances as $performance) {
$performance->setArtist($this);
$this->addPerformance($performance);
}

return $this;
}

public function removePerformances(ArrayCollection $performances): self
{
foreach ($performances as $performance) {
$this->removePerformance($performance);
}

return $this;
}
}
21 changes: 21 additions & 0 deletions app/Doctrine/ORM/Entity/Performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ public function removeRecording(Recording $recording): bool
return $this->recordings->removeElement($recording);
}

/** @param mixed[] $recordings */
public function addRecordings(Collection $recordings): self
{
foreach ($recordings as $recording) {
$recording->setPerformance($this);
$this->addRecording($recording);
}

return $this;
}

/** @param mixed[] $recordings */
public function removeRecordings(Collection $recordings): self
{
foreach ($recordings as $recording) {
$this->removeRecording($recording);
}

return $this;
}

/**
* Get recordings.
*
Expand Down
20 changes: 17 additions & 3 deletions app/Doctrine/ORM/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
#[GraphQL\Entity(description: 'User', typeName: 'user')]
class User
{
#[GraphQL\Field(description: 'User name')]
#[GraphQL\Field(description: 'Name')]
private string $name;

#[GraphQL\Field(description: 'User email')]
#[GraphQL\Field(description: 'Email')]
private string $email;

#[GraphQL\Field(description: 'User password')]
private string $password;

#[GraphQL\Field(description: 'Role')]
private string $role;

#[GraphQL\Field(description: 'Primary key')]
private int $id;

Expand Down Expand Up @@ -90,6 +92,18 @@ public function getPassword(): string
return $this->password;
}

public function setRole(string $role): self
{
$this->role = $role;

return $this;
}

public function getRole(): string
{
return $this->role;
}

/**
* Get id.
*/
Expand Down
15 changes: 9 additions & 6 deletions config/doctrine-data-fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

declare(strict_types=1);

use App\Doctrine\ORM\DataFixtures\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
Expand All @@ -16,16 +18,17 @@
// 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
/**
Fixture1::class,
Fixture2::class,
*/

return [
'default' => [ // Group name
'objectManager' => 'Doctrine\ORM\EntityManager',
'objectManager' => EntityManager::class,
'executor' => ORMExecutor::class,
'purger' => ORMPurger::class,
'fixtures' => [],
],
'faker' => [ // Group name
'objectManager' => EntityManager::class,
'executor' => ORMExecutor::class,
'purger' => ORMPurger::class,
'fixtures' => [Faker\Faker::class],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<field name="name" type="string" nullable="false"/>
<field name="email" type="string" unique="true" nullable="false"/>
<field name="password" type="string" nullable="false"/>
<field name="role" type="string" nullable="false"/>
<many-to-many field="recordings" target-entity="App\Doctrine\ORM\Entity\Recording" inversed-by="users">
<join-table name="RecordingToUser">
<join-columns>
Expand Down
Binary file modified database/database.sqlite
100755 → 100644
Binary file not shown.
7 changes: 4 additions & 3 deletions ldog.skipper
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<field name="name" type="string" required="true" uuid="7f1988fe-14c8-4d3b-87c8-56ca5ed86385"/>
<field name="email" type="string" required="true" unique="true" uuid="b7e9c340-9589-4400-8f97-1c2a8d95a758"/>
<field name="password" type="string" required="true" uuid="a249742e-2404-4844-994e-a7ceaf35705d"/>
<field name="role" type="string" enum-values="" required="true" uuid="e8d8604b-bc69-4dd4-a9ba-19514fb79623"/>
</entity>
<entity name="\App\Doctrine\ORM\Entity\RecordingToUser" local-name="RecordingToUser" namespace="\App\Doctrine\ORM\Entity" uuid="e5603f71-6798-480b-bcf8-0d185a08e653">
<field name="user_id" type="integer" required="true" primary="true" uuid="6e73b00a-d350-44aa-9c5c-df5e8bd1b767"/>
Expand All @@ -51,14 +52,14 @@
<visual-data>
<association uuid="4f0247e2-6d76-42f9-b56b-52306c2fa201" caption1-position-x="0" caption1-position-y="0" center-position-x="0" center-position-y="0" color="#969696"/>
<association uuid="ab34ba86-e61e-458a-87e7-9dfa2aa1f391" caption1-position-x="0" caption1-position-y="0" center-position-x="0" center-position-y="0" color="#969696"/>
<comment uuid="0e4ff410-5025-42fd-89ae-0d05319ab49b" bg-color="#FFFFE0" position-x="283" position-y="253" size-x="6" size-x2="128" size-y="-3" size-y2="63" txt-color="#000000"/>
<comment uuid="0e4ff410-5025-42fd-89ae-0d05319ab49b" bg-color="#FFFFE0" position-x="403" position-y="293" size-x="6" size-x2="128" size-y="-3" size-y2="63" txt-color="#000000"/>
<entity uuid="10de66e4-a8b3-44a9-a6d0-a9fd28338fe2" bg-color="#FFFFFF" hdr-color="#D2D2D2" position-x="-23" position-y="-44" size-x="0" size-x2="77" size-y="0" size-y2="45"/>
<entity uuid="568a03b7-3dd5-435a-9bc8-869bc673923c" bg-color="#FFFFFF" hdr-color="#D2D2D2" position-x="-23" position-y="136" size-x="0" size-x2="93" size-y="0" size-y2="73"/>
<entity uuid="8a04b312-72bb-4723-baac-75502524ae38" bg-color="#FFFFFF" hdr-color="#D2D2D2" position-x="177" position-y="-44" size-x="0" size-x2="136" size-y="0" size-y2="101"/>
<entity uuid="ad470906-9e63-41e7-9558-979789bc2d28" bg-color="#FFFFFF" hdr-color="#D2D2D2" position-x="177" position-y="136" size-x="0" size-x2="122" size-y="0" size-y2="59"/>
<entity uuid="e5603f71-6798-480b-bcf8-0d185a08e653" bg-color="#FFFFFF" hdr-color="#D2D2D2" position-x="77" position-y="256" size-x="0" size-x2="109" size-y="0" size-y2="60"/>
<many-to-many-association uuid="e4936d0d-1d1f-4fb8-af31-fa1d0665ccf7" color="#969696"/>
<module uuid="d5f257f8-d854-4bae-a05d-1ed8673c7eb5" bg-color="#E1EDF0" position-x="43" position-y="84" size-x="43" size-x2="437" size-y="84" size-y2="376"/>
<project uuid="25d2b513-c6b7-4071-9ada-7c48f127f426" size-x="50" size-x2="520" size-y="50" size-y2="500"/>
<module uuid="d5f257f8-d854-4bae-a05d-1ed8673c7eb5" bg-color="#E1EDF0" position-x="43" position-y="84" size-x="43" size-x2="577" size-y="84" size-y2="376"/>
<project uuid="25d2b513-c6b7-4071-9ada-7c48f127f426" size-x="50" size-x2="660" size-y="50" size-y2="500"/>
</visual-data>
</skipper>
27 changes: 24 additions & 3 deletions magidoc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ export default {
output: './public/docs',
options: {
siteRoot: `/docs`,
appTitle: 'ldog Stack',
appTitle: 'LDOG Stack',
appLogo: 'https://raw.githubusercontent.com/API-Skeletons/ldog/main/public/ldog.svg',
appFavicon: 'https://apiskeletons.com/images/favicon.ico',
pages: [
{
title: 'Welcome',
content: `
ldog Stack
LDOG Stack
==========
Laravel, Doctrine ORM, and GraphQL
----------------------------------
This is a template application for building GraphQL applications in
Laravel with Doctrine ORM. **Known as the ldog (el-dog) Stack.**
Laravel with Doctrine ORM. **Known as the LDOG (el-dog) Stack.**
To create a new project run
\`\`\`
Expand Down Expand Up @@ -173,6 +173,27 @@ To ensure code quality, run \`composer test\` to run the following:
---
A project of [API Skeletons](mailto:[email protected])
* https://github.com/api-skeletons/ldog
`
},
{
title: 'Fixtures',
content: `
Fixtures
--------
Included with LDOG is a Doctrine fixture library. There are two included fixtures. To rebuild the
SQLite database, copy \`.env.dev\` to \`.env\`, delete the \`~/database/database.sqlite\` file, and run
\`\`\`
rm database/database.sqlite
php artisan doctrine:schema:create
php artisan doctrine:data-fixture:import faker
\`\`\`
---
A project of [API Skeletons](mailto:[email protected])
* https://github.com/api-skeletons/ldog
`
Expand Down
Loading

0 comments on commit 1cddc1f

Please sign in to comment.