Skip to content

Commit

Permalink
feature: restore service in LoadUserWithServiceData
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Oct 4, 2022
1 parent 189df6e commit 3459fd5
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 13 deletions.
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

docker build ./ --tag ltfb

#docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'rm -rf tests/App*/var/cache/* vendor/ composer.lock ; composer update'

#exit 0

#docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'rm -rf tests/App*/var/cache/* ; composer update ; php vendor/phpunit/phpunit/phpunit --filter testLoadNonexistentFixturesFilesPaths --exclude-group "" --testdox'
docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'php vendor/phpunit/phpunit/phpunit --exclude-group "" --testdox'
docker run -i -t --rm --volume "$PWD/.:/app" --workdir /app ltfb sh -c 'rm -rf tests/App*/var/cache/*'
10 changes: 10 additions & 0 deletions tests/App/DataFixtures/ORM/LoadDependentUserWithServiceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Liip\Acme\Tests\App\Entity\User;
use Liip\Acme\Tests\App\Service\DummyService;

class LoadDependentUserWithServiceData extends AbstractFixture implements DependentFixtureInterface
{
/** @var DummyService */
private $dummyService;

public function __construct(DummyService $dummyService)
{
$this->dummyService = $dummyService;
}

/**
* {@inheritdoc}
*/
Expand All @@ -29,6 +38,7 @@ public function load(ObjectManager $manager): void
$user = clone $this->getReference('serviceUser');

$user->setId(3);
$user->setDummyText($this->dummyService->getText());

$manager->persist($user);
$manager->flush();
Expand Down
3 changes: 3 additions & 0 deletions tests/App/DataFixtures/ORM/LoadUserDataInGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Doctrine\Persistence\ObjectManager;
use Liip\Acme\Tests\App\Entity\User;

/**
* @see \Liip\Acme\Tests\Test\ConfigSqliteTest::loadAllFixtures()
*/
class LoadUserDataInGroup extends AbstractFixture implements FixtureInterface, FixtureGroupInterface
{
/**
Expand Down
13 changes: 13 additions & 0 deletions tests/App/DataFixtures/ORM/LoadUserWithServiceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Liip\Acme\Tests\App\Entity\User;
use Liip\Acme\Tests\App\Service\DummyService;

/**
* @see LoadDependentUserWithServiceData::getDependencies()
*/
class LoadUserWithServiceData extends AbstractFixture implements FixtureInterface
{
/** @var DummyService */
private $dummyService;

public function __construct(DummyService $dummyService)
{
$this->dummyService = $dummyService;
}

/**
* {@inheritdoc}
*/
Expand All @@ -30,6 +42,7 @@ public function load(ObjectManager $manager): void
$user->setId(1);
$user->setName('foo bar');
$user->setEmail('[email protected]');
$user->setDummyText($this->dummyService->getText());

$manager->persist($user);
$manager->flush();
Expand Down
31 changes: 19 additions & 12 deletions tests/App/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@
*/
class User
{
/**
* @var int
*/
/** @var int */
private $id;

/**
* @var string
*/
/** @var string */
private $name;

/**
* @var string
*/
/** @var string */
private $salt;

/**
* @var string
*/
/** @var string */
private $email;

/** @var string */
private $dummyText;

public function __construct()
{
$this->salt = sha1(
Expand Down Expand Up @@ -93,4 +88,16 @@ public function getSalt(): string
{
return $this->salt;
}

public function setDummyText(?string $dummyText): self
{
$this->dummyText = $dummyText;

return $this;
}

public function getDummyText(): ?string
{
return $this->dummyText;
}
}
4 changes: 4 additions & 0 deletions tests/App/Resources/config/doctrine/User.orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ Liip\Acme\Tests\App\Entity\User:
salt:
type: string
length: '255'
dummyText:
type: string
length: '255'
nullable: true
22 changes: 22 additions & 0 deletions tests/App/Service/DummyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\Acme\Tests\App\Service;

class DummyService
{
public function getText(): string
{
return 'text from DummyService';
}
}
3 changes: 3 additions & 0 deletions tests/App/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ services:
_defaults:
autowire: true
autoconfigure: true

Liip\Acme\Tests\App\DataFixtures\ORM\:
resource: 'DataFixtures/ORM/*'
tags: ['doctrine.fixture.orm']

Liip\Acme\Tests\App\Service\:
resource: './Service/'
10 changes: 9 additions & 1 deletion tests/Test/ConfigSqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testLoadFixtures(): void
);
}

public function testLoadAll(): void
public function loadAllFixtures(): void
{
// Load the fixtures with an invalid group. The database should be empty.
$fixtures = $this->databaseTool->loadAllFixtures(['wrongGroup']);
Expand Down Expand Up @@ -289,6 +289,14 @@ public function testLoadDependentFixturesWithDependencyInjected(): void
4,
$users
);

$user1 = $this->userRepository->find(1);
$this->assertSame(1, $user1->getId());
$this->assertSame('text from DummyService', $user1->getDummyText());

$user3 = $this->userRepository->find(3);
$this->assertSame(3, $user3->getId());
$this->assertSame('text from DummyService', $user3->getDummyText());
}

/**
Expand Down

0 comments on commit 3459fd5

Please sign in to comment.