Skip to content

work with doctrine migrations in this project #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 65 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
6afcc6b
init defaults
benblub Jun 25, 2021
9516df2
fix replace typed property with docblock
benblub Jun 26, 2021
0c66803
csfixer
benblub Jun 26, 2021
65e224d
fix replace typed properties with docblock
benblub Jun 26, 2021
b2c1d3c
add some tests
benblub Jun 26, 2021
8b38806
csfixer
benblub Jun 26, 2021
1473ed4
add fakerTypes to DoctrineTypes
benblub Jun 27, 2021
e16e7a7
refactored reduce complexity, use only doctrineTypes
benblub Jun 27, 2021
d01e922
remove unused class
benblub Jun 27, 2021
a85b405
refactor rename methodname
benblub Jun 27, 2021
2c0065d
ref dont modify gitignore
benblub Jun 27, 2021
fb88157
ref dont modify composer.jsom (remove csfixer)
benblub Jun 27, 2021
369a8b4
add DATE to DoctrineTypes
benblub Jun 27, 2021
0744a7d
convert to UPPERCASE
benblub Jun 27, 2021
a0b6121
write fakerMethods for props in tpl
benblub Jun 27, 2021
af069ac
fix ident
benblub Jun 27, 2021
ab9738d
fix MakeFactoryTest has now Faker Method for defaults
benblub Jun 27, 2021
991f764
remove some ident
benblub Jun 27, 2021
d30ac97
fix ident in tests
benblub Jun 27, 2021
79b0ce2
ref cleanup, remove unused
benblub Jun 27, 2021
df9cb9e
ref add return type to method
benblub Jun 27, 2021
24b7e96
ref remove whats not used yet
benblub Jun 27, 2021
6e518c8
ref remove comment
benblub Jun 27, 2021
e8b3c31
ref remove comment
benblub Jun 29, 2021
504027c
fix typo
benblub Jun 29, 2021
3653f21
remove todos
benblub Jun 29, 2021
440521d
set comma in DoctrineTypes
benblub Jun 29, 2021
64ffaf7
remove unsupportet types from test
benblub Jun 29, 2021
8136e94
run test only if FoundryBundle is enabled
benblub Jun 29, 2021
4c4062f
run PropertyTest only if App Kernel is available
benblub Jun 29, 2021
faf9737
fix use static::$container
benblub Jun 29, 2021
7beee8a
ref remove unnsecessary
benblub Jun 29, 2021
de259ae
ref remove unused getter/setter
benblub Jun 29, 2021
326b5a5
ref replace class var with local
benblub Jun 29, 2021
36aded0
Merge branch 'zenstruck:master' into master
benblub Jun 29, 2021
f5c4108
Merge branch 'zenstruck:master' into master
benblub Jul 26, 2021
5be610f
Merge branch 'zenstruck:master' into master
benblub Dec 10, 2021
ba2dd57
init auto defaults for non-nullable 1-1/n-1 relationships
benblub Dec 10, 2021
753a30a
csfixer
benblub Dec 10, 2021
813dd31
csfix
benblub Dec 10, 2021
2f6c843
csfix
benblub Dec 10, 2021
42e4b90
add early returns
benblub Dec 10, 2021
4e8665e
fix targetEntity dont relay on fieldname
benblub Dec 10, 2021
a3f64aa
ref: early return
benblub Dec 10, 2021
71d73d7
add assert that PostFactory appears
benblub Dec 10, 2021
c35b4cc
add phpstorm to gitignore
benblub Dec 11, 2021
9ca0f80
Merge remote-tracking branch 'origin/master'
benblub Dec 11, 2021
81d00b0
csfix
benblub Dec 11, 2021
cf958e3
fix fieldname
benblub Dec 11, 2021
58fa27a
add fixtures
benblub Dec 11, 2021
f837a4e
fix var not exist & use lcfirst &
benblub Dec 11, 2021
dea3bff
add test for relations
benblub Dec 11, 2021
1d53b81
fix table name
benblub Dec 11, 2021
1fe7cd8
add phpunit to composer
benblub Dec 11, 2021
9e10178
add composer.lock to project
benblub Dec 11, 2021
cb8e76e
ref remove hardcoded search for factories
benblub Dec 11, 2021
64776e8
ref remove composer lock
benblub Dec 11, 2021
d1b75a5
ref remove phpunit from composer.json
benblub Dec 11, 2021
d8f994c
add more possible dirs
benblub Dec 11, 2021
1878b5e
test
benblub Dec 11, 2021
ddc07ad
quickfix
benblub Dec 11, 2021
0b9eb91
csfix
benblub Dec 11, 2021
67ab2cc
remove idea from gitignore
benblub Dec 11, 2021
02e94a9
ref replace createOne with new
benblub Dec 11, 2021
a13d593
work with doctrine migrations in this projects
benblub Dec 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/tests/Fixtures/tmp
/var/
/docs/output/
/credentials.php
14 changes: 14 additions & 0 deletions credentials.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
// rename this file to credentials.php
// add your database credentials here
// now you are able to use: php vendor/bin/doctrine-migrations

function getCredentials()
{
return [
'dbname' => '',
'user' => '',
'password' => '',
'host' => '',
];
}
12 changes: 12 additions & 0 deletions migrations-db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
require 'credentials.php';

$credentials = getCredentials();

return [
'dbname' => $credentials['dbname'],
'user' => $credentials['user'],
'password' => $credentials['password'],
'host' => $credentials['host'],
'driver' => 'pdo_mysql',
];
21 changes: 21 additions & 0 deletions migrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [
'table_storage' => [
'table_name' => 'doctrine_migration_versions',
'version_column_name' => 'version',
'version_column_length' => 1024,
'executed_at_column_name' => 'executed_at',
'execution_time_column_name' => 'execution_time',
],

'migrations_paths' => [
'Zenstruck\Foundry\Tests\Fixtures\Migrations' => 'tests/Fixtures/Migrations',
],

'all_or_nothing' => false,
'check_database_platform' => true,
'organize_migrations' => 'none',
'connection' => null,
'em' => null,
];
62 changes: 62 additions & 0 deletions src/Bundle/Maker/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Finder\Finder;
use Zenstruck\Foundry\ModelFactory;

/**
Expand Down Expand Up @@ -202,6 +203,40 @@ private function defaultPropertiesFor(string $class, bool $allFields): iterable
$metadata = $em->getClassMetadata($class);
$ids = $metadata->getIdentifierFieldNames();

// TODO cleanup the code
// TODO class exist dont relay on fix namespaces
// TODO write some tests
// TODO test with kind of possible relations
// If Factory exist for related entities populate too with auto defaults
$relatedEntities = $metadata->associationMappings;
foreach ($relatedEntities as $item) {
// if joinColumns is not written entity is default nullable ($nullable = true;)
// @see vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/JoinColumn.php LINE 28
if (!\array_key_exists('joinColumns', $item)) {
continue;
}

// if this key is available its a ManyToMany relation but its optional, Key must not be present to be ManyToMany
if (\array_key_exists('joinTable', $item)) {
continue;
}

if (true === $item['joinColumns'][0]['nullable']) {
continue;
}

$joinedColumns = $item['joinColumns'];
$fieldName = $item['fieldName'];

$targetEntityArray = \explode('\\', $item['targetEntity']);
$targetEntity = \end($targetEntityArray);
$factory = \ucfirst($targetEntity).'Factory';

if ($this->isFactory($factory)) {
yield \lcfirst($fieldName) => \ucfirst($targetEntity).'Factory::new(),';
}
}

foreach ($metadata->fieldMappings as $property) {
// ignore identifiers and nullable fields
if ((!$allFields && ($property['nullable'] ?? false)) || \in_array($property['fieldName'], $ids, true)) {
Expand All @@ -218,4 +253,31 @@ private function defaultPropertiesFor(string $class, bool $allFields): iterable
yield $property['fieldName'] => $value;
}
}

private function isFactory($factory)
{
// Quickfix on Github CI - TODO
if (\class_exists('Zenstruck\Foundry\Tests\Fixtures\Factories\\'.$factory)) {
return true;
}

$dirs = [];

if (\is_dir('src')) {
$dirs[] = 'src/';
}

if (\is_dir('Tests')) {
$dirs[] = 'Tests/';
}

$finder = new Finder();
$finder->in($dirs)->files()->name($factory.'.php');

if (\count(\iterator_to_array($finder)) > 0) {
return true;
}

return false;
}
}
36 changes: 36 additions & 0 deletions tests/Fixtures/Entity/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Zenstruck\Foundry\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="bars")
*/
class Bar
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity=Foo::class, inversedBy="oneToMany")
*/
private $foo;

public function getFoo(): ?Foo
{
return $this->foo;
}

public function setFoo(?Foo $foo): self
{
$this->foo = $foo;

return $this;
}
}
132 changes: 132 additions & 0 deletions tests/Fixtures/Entity/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Zenstruck\Foundry\Tests\Fixtures\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="foos")
*/
class Foo
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\OneToOne(targetEntity=Bar::class, cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $oneToOne;

/**
* @ORM\OneToMany(targetEntity=Bar::class, mappedBy="foo")
*/
private $oneToMany;

/**
* @ORM\ManyToOne(targetEntity=Bar::class)
* @ORM\JoinColumn(nullable=false)
*/
private $manyToOne;

/**
* @ORM\ManyToMany(targetEntity=Bar::class)
*/
private $manyToMany;

public function __construct()
{
$this->oneToMany = new ArrayCollection();
$this->manyToMany = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

public function getOneToOne(): ?Bar
{
return $this->oneToOne;
}

public function setOneToOne(Bar $oneToOne): self
{
$this->oneToOne = $oneToOne;

return $this;
}

/**
* @return Collection|Bar[]
*/
public function getOneToMany(): Collection
{
return $this->oneToMany;
}

public function addOneToMany(Bar $oneToMany): self
{
if (!$this->oneToMany->contains($oneToMany)) {
$this->oneToMany[] = $oneToMany;
$oneToMany->setFoo($this);
}

return $this;
}

public function removeOneToMany(Bar $oneToMany): self
{
if ($this->oneToMany->removeElement($oneToMany)) {
// set the owning side to null (unless already changed)
if ($oneToMany->getFoo() === $this) {
$oneToMany->setFoo(null);
}
}

return $this;
}

public function getManyToOne(): ?Bar
{
return $this->manyToOne;
}

public function setManyToOne(?Bar $manyToOne): self
{
$this->manyToOne = $manyToOne;

return $this;
}

/**
* @return Collection|Bar[]
*/
public function getManyToMany(): Collection
{
return $this->manyToMany;
}

public function addManyToMany(Bar $manyToMany): self
{
if (!$this->manyToMany->contains($manyToMany)) {
$this->manyToMany[] = $manyToMany;
}

return $this;
}

public function removeManyToMany(Bar $manyToMany): self
{
$this->manyToMany->removeElement($manyToMany);

return $this;
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/Factories/BarFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Zenstruck\Foundry\Tests\Fixtures\Factories;

use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Tests\Fixtures\Entity\Bar;

/**
* @author Benjamin Knecht <benjaminknecht@wearewondrous.com>
*/
final class BarFactory extends ModelFactory
{
protected static function getClass(): string
{
return Bar::class;
}

protected function getDefaults(): array
{
return ['value' => 'Some Bar'];
}
}
Loading