Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a78c02

Browse files
committed
fix(in-memory): trigger in memory on PreparationStarted
1 parent 792077a commit 1a78c02

4 files changed

Lines changed: 82 additions & 3 deletions

File tree

src/PHPUnit/EnableInMemoryOnTestPrepared.php renamed to src/PHPUnit/EnableInMemoryOnPreparationStarted.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
* @internal
2424
* @author Nicolas PHILIPPE <[email protected]>
2525
*/
26-
final class EnableInMemoryOnTestPrepared implements Event\Test\PreparedSubscriber
26+
final class EnableInMemoryOnPreparationStarted implements Event\Test\PreparationStartedSubscriber
2727
{
28-
public function notify(Event\Test\Prepared $event): void
28+
public function notify(Event\Test\PreparationStarted $event): void
2929
{
3030
$test = $event->test();
3131

src/PHPUnit/FoundryExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ public function bootstrap(
5757
Event\Test\PreparationStarted::class => [
5858
new BootFoundryOnPreparationStarted(),
5959
new ResetDatabaseOnPreparationStarted($autoResetEnabled),
60+
new EnableInMemoryOnPreparationStarted(),
6061
],
6162
Event\Test\Prepared::class => [
62-
new EnableInMemoryOnTestPrepared(),
63+
// todo: mock time ?!
64+
// todo: in memory bug avec assert()->exists()
6365
new BuildStoryOnTestPrepared(),
6466
new TriggerDataProviderPersistenceOnTestPrepared(),
6567
],

src/Persistence/Exception/ObjectHasUnsavedChanges.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class ObjectHasUnsavedChanges extends RefreshObjectFailed
1818
public function __construct(string $objectClass)
1919
{
2020
parent::__construct(
21+
// todo mauvaise erreur!
2122
"Cannot auto refresh \"{$objectClass}\" as there are unsaved changes. Be sure to call ->_save() or disable auto refreshing (see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh for details)."
2223
);
2324
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Integration\InMemory;
15+
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
18+
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
19+
use PHPUnit\Framework\Attributes\Test;
20+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
21+
use Zenstruck\Foundry\Attribute\ResetDatabase;
22+
use Zenstruck\Foundry\Attribute\WithStory;
23+
use Zenstruck\Foundry\InMemory\AsInMemoryTest;
24+
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
25+
use Zenstruck\Foundry\Test\Factories;
26+
use Zenstruck\Foundry\Tests\Fixture\Entity\Address;
27+
use Zenstruck\Foundry\Tests\Fixture\Entity\Category;
28+
use Zenstruck\Foundry\Tests\Fixture\Entity\Contact;
29+
use Zenstruck\Foundry\Tests\Fixture\Entity\GenericEntity;
30+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Address\AddressFactory;
31+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Category\CategoryFactory;
32+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory;
33+
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
34+
use Zenstruck\Foundry\Tests\Fixture\InMemory\InMemoryAddressRepository;
35+
use Zenstruck\Foundry\Tests\Fixture\InMemory\InMemoryContactRepository;
36+
use Zenstruck\Foundry\Tests\Fixture\Stories\EntityStory;
37+
use Zenstruck\Foundry\Tests\Integration\RequiresORM;
38+
39+
/**
40+
* @author Nicolas PHILIPPE <[email protected]>
41+
* @requires PHPUnit >=11.4
42+
*/
43+
#[RequiresPhpunit('>=11.4')]
44+
#[RequiresPhpunitExtension(FoundryExtension::class)]
45+
#[AsInMemoryTest]
46+
#[ResetDatabase]
47+
final class InMemoryUsingSetUpTest extends KernelTestCase
48+
{
49+
use Factories;
50+
use RequiresORM;
51+
52+
private InMemoryContactRepository $contactRepository;
53+
54+
private EntityManagerInterface $entityManager;
55+
private Contact $contact;
56+
57+
protected function setUp(): void
58+
{
59+
$this->contactRepository = self::getContainer()->get(InMemoryContactRepository::class); // @phpstan-ignore assign.propertyType
60+
61+
$this->contact = ContactFactory::createOne();
62+
63+
self::assertCount(1, ContactFactory::repository());
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
#[Test]
70+
public function can_access_objects_created_in_set_up_method(): void
71+
{
72+
self::assertCount(1, ContactFactory::repository());
73+
74+
self::assertSame($this->contact, $this->contactRepository->_all()[0]);
75+
}
76+
}

0 commit comments

Comments
 (0)