Skip to content

Commit bd67005

Browse files
committed
Rewrite tests :
Try to remove most mocks Normalize code
1 parent 0d91b6a commit bd67005

13 files changed

+97
-154
lines changed

tests/ConstantExtractorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Yokai\EnumBundle\Tests;
44

55
use Generator;
6+
use PHPUnit\Framework\TestCase;
67
use Yokai\EnumBundle\ConstantExtractor;
78
use Yokai\EnumBundle\Exception\LogicException;
89

tests/ConstantListEnumTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Yokai\EnumBundle\Tests;
44

5+
use PHPUnit\Framework\TestCase;
56
use Yokai\EnumBundle\ConstantListEnum;
67
use Yokai\EnumBundle\Exception\InvalidArgumentException;
78
use Yokai\EnumBundle\Tests\Fixtures\Vehicle;

tests/ConstantListTranslatedEnumTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Yokai\EnumBundle\Tests;
44

5+
use PHPUnit\Framework\TestCase;
56
use Symfony\Contracts\Translation\TranslatorInterface;
67
use Yokai\EnumBundle\ConstantListTranslatedEnum;
78
use Yokai\EnumBundle\Exception\InvalidArgumentException;

tests/DependencyInjection/CompilerPass/TaggedEnumCollectorCompilerPassTest.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,43 @@
22

33
namespace Yokai\EnumBundle\Tests\DependencyInjection\CompilerPass;
44

5+
use PHPUnit\Framework\TestCase;
56
use Symfony\Component\DependencyInjection\ContainerBuilder;
6-
use Symfony\Component\DependencyInjection\Definition;
77
use Symfony\Component\DependencyInjection\Reference;
88
use Yokai\EnumBundle\DependencyInjection\CompilerPass\TaggedEnumCollectorCompilerPass;
9-
use Yokai\EnumBundle\Tests\TestCase;
9+
use Yokai\EnumBundle\EnumRegistry;
10+
use Yokai\EnumBundle\Tests\Fixtures\GenderEnum;
11+
use Yokai\EnumBundle\Tests\Fixtures\TypeEnum;
1012

1113
/**
1214
* @author Yann Eugoné <[email protected]>
1315
*/
1416
class TaggedEnumCollectorCompilerPassTest extends TestCase
1517
{
16-
/**
17-
* @var TaggedEnumCollectorCompilerPass
18-
*/
19-
private $compiler;
20-
21-
protected function setUp(): void
22-
{
23-
$this->compiler = new TaggedEnumCollectorCompilerPass;
24-
}
25-
2618
public function testCollectWhenServiceNotAvailable(): void
2719
{
28-
$compiler = $this->prophesize(ContainerBuilder::class);
29-
$compiler->hasDefinition('yokai_enum.enum_registry')->shouldBeCalled()->willReturn(false);
20+
$container = new ContainerBuilder();
21+
22+
(new TaggedEnumCollectorCompilerPass())->process($container);
3023

31-
$this->compiler->process($compiler->reveal());
24+
self::assertTrue(true); // no exception thrown
3225
}
3326

3427
public function testCollectEnums(): void
3528
{
36-
$registry = $this->prophesize(Definition::class);
37-
$registry->addMethodCall('add', [new Reference('enum.gender')])->shouldBeCalled();
38-
$registry->addMethodCall('add', [new Reference('enum.type')])->shouldBeCalled();
39-
40-
$compiler = $this->prophesize(ContainerBuilder::class);
41-
$compiler->hasDefinition('yokai_enum.enum_registry')->shouldBeCalled()->willReturn(true);
42-
$compiler->getDefinition('yokai_enum.enum_registry')->shouldBeCalled()->willReturn($registry);
43-
$compiler->findTaggedServiceIds('yokai_enum.enum')->shouldBeCalled()->willReturn([
44-
'enum.gender' => $this->prophesize(Definition::class)->reveal(),
45-
'enum.type' => $this->prophesize(Definition::class)->reveal(),
46-
]);
47-
48-
$this->compiler->process($compiler->reveal());
29+
$container = new ContainerBuilder();
30+
$container->register('yokai_enum.enum_registry', EnumRegistry::class);
31+
$container->register('enum.gender', GenderEnum::class)
32+
->addTag('yokai_enum.enum');
33+
$container->register('enum.type', TypeEnum::class)
34+
->addTag('yokai_enum.enum');
35+
36+
(new TaggedEnumCollectorCompilerPass())->process($container);
37+
38+
$registry = $container->getDefinition('yokai_enum.enum_registry');
39+
self::assertEquals([
40+
['add', [new Reference('enum.gender')]],
41+
['add', [new Reference('enum.type')]],
42+
], $registry->getMethodCalls());
4943
}
5044
}

tests/DependencyInjection/EnumExtensionTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,21 @@
22

33
namespace Yokai\EnumBundle\Tests\DependencyInjection;
44

5+
use PHPUnit\Framework\TestCase;
56
use Symfony\Component\DependencyInjection\ContainerBuilder;
67
use Yokai\EnumBundle\DependencyInjection\EnumExtension;
78
use Yokai\EnumBundle\EnumInterface;
89
use Yokai\EnumBundle\EnumRegistry;
9-
use Yokai\EnumBundle\Tests\TestCase;
1010

1111
/**
1212
* @author Yann Eugoné <[email protected]>
1313
*/
1414
class EnumExtensionTest extends TestCase
1515
{
16-
public function extension(): EnumExtension
17-
{
18-
return new EnumExtension();
19-
}
20-
21-
/**
22-
* @test
23-
*/
24-
public function it_register_services(): void
16+
public function testLoad(): void
2517
{
2618
$container = new ContainerBuilder();
27-
$this->extension()->load([[]], $container);
19+
(new EnumExtension())->load([[]], $container);
2820

2921
$services = [
3022
'yokai_enum.form_type.enum_type',

tests/EnumRegistryTest.php

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Yokai\EnumBundle\Tests;
44

5-
use Prophecy\Prophecy\ObjectProphecy;
6-
use Symfony\Contracts\Translation\TranslatorInterface;
5+
use PHPUnit\Framework\TestCase;
76
use Yokai\EnumBundle\EnumRegistry;
87
use Yokai\EnumBundle\Exception\InvalidArgumentException;
98
use Yokai\EnumBundle\Exception\LogicException;
@@ -17,56 +16,48 @@
1716
*/
1817
class EnumRegistryTest extends TestCase
1918
{
20-
/**
21-
* @var EnumRegistry
22-
*/
23-
private $registry;
24-
25-
protected function setUp(): void
26-
{
27-
$this->registry = new EnumRegistry;
28-
}
29-
3019
public function testAddDuplicatedException(): void
3120
{
3221
$this->expectException(LogicException::class);
33-
$this->registry->add(new GenderEnum);
34-
$this->registry->add(new GenderEnum);
22+
$registry = new EnumRegistry();
23+
$registry->add(new GenderEnum());
24+
$registry->add(new GenderEnum());
3525
}
3626

3727
public function testGetInvalidException(): void
3828
{
3929
$this->expectException(InvalidArgumentException::class);
40-
$this->registry->add(new GenderEnum);
41-
$this->registry->get('type');
30+
$registry = new EnumRegistry();
31+
$registry->add(new GenderEnum());
32+
$registry->get('type');
4233
}
4334

4435
public function testAddNominal(): void
4536
{
46-
/** @var TranslatorInterface|ObjectProphecy $translator */
47-
$translator = $this->prophesize(TranslatorInterface::class)->reveal();
48-
$gender = new GenderEnum;
37+
$translator = new Translator([]);
38+
$gender = new GenderEnum();
4939
$state = new StateEnum($translator);
5040
$subscription = new SubscriptionEnum($translator);
51-
$type = new TypeEnum;
52-
53-
$this->registry->add($gender);
54-
$this->registry->add($state);
55-
$this->registry->add($subscription);
56-
$this->registry->add($type);
57-
58-
self::assertTrue($this->registry->has(GenderEnum::class));
59-
self::assertTrue($this->registry->has('state'));
60-
self::assertTrue($this->registry->has('subscription'));
61-
self::assertTrue($this->registry->has('type'));
62-
63-
self::assertSame($gender, $this->registry->get(GenderEnum::class));
64-
self::assertSame($state, $this->registry->get('state'));
65-
self::assertSame($subscription, $this->registry->get('subscription'));
66-
self::assertSame($type, $this->registry->get('type'));
41+
$type = new TypeEnum();
42+
43+
$registry = new EnumRegistry();
44+
$registry->add($gender);
45+
$registry->add($state);
46+
$registry->add($subscription);
47+
$registry->add($type);
48+
49+
self::assertTrue($registry->has(GenderEnum::class));
50+
self::assertTrue($registry->has('state'));
51+
self::assertTrue($registry->has('subscription'));
52+
self::assertTrue($registry->has('type'));
53+
54+
self::assertSame($gender, $registry->get(GenderEnum::class));
55+
self::assertSame($state, $registry->get('state'));
56+
self::assertSame($subscription, $registry->get('subscription'));
57+
self::assertSame($type, $registry->get('type'));
6758
self::assertSame(
6859
[GenderEnum::class => $gender, 'state' => $state, 'subscription' => $subscription, 'type' => $type],
69-
$this->registry->all()
60+
$registry->all()
7061
);
7162
}
7263
}

tests/EnumTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTimeImmutable;
66
use LogicException;
7+
use PHPUnit\Framework\TestCase;
78
use Yokai\EnumBundle\Enum;
89
use Yokai\EnumBundle\Exception\InvalidArgumentException;
910

tests/Form/Extension/EnumTypeGuesserTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ class EnumTypeGuesserTest extends TypeTestCase
5252

5353
protected function setUp(): void
5454
{
55-
$this->enumRegistry = $this->prophesize(EnumRegistry::class);
56-
$this->enumRegistry->has('state')->willReturn(false);
57-
$this->enumRegistry->has(GenderEnum::class)->willReturn(true);
58-
$this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum);
55+
$this->enumRegistry = new EnumRegistry();
56+
$this->enumRegistry->add(new GenderEnum());
5957

6058
$this->metadata = new ClassMetadata(self::TEST_CLASS);
6159
$this->metadata->addPropertyConstraint(self::TEST_PROPERTY_DIRECT, new Enum(['enum' => GenderEnum::class]));
@@ -71,7 +69,7 @@ protected function getConstraints(array $options): array
7169
$this->metadataFactory->getMetadataFor(self::TEST_CLASS)
7270
->willReturn($this->metadata);
7371

74-
$this->guesser = new EnumTypeGuesser($this->metadataFactory->reveal(), $this->enumRegistry->reveal());
72+
$this->guesser = new EnumTypeGuesser($this->metadataFactory->reveal(), $this->enumRegistry);
7573

7674
parent::setUp();
7775
}
@@ -138,7 +136,7 @@ public function testCreateForm(): void
138136
protected function getExtensions(): array
139137
{
140138
return [
141-
new TestExtension($this->enumRegistry->reveal(), $this->metadataFactory->reveal()),
139+
new TestExtension($this->enumRegistry, $this->metadataFactory->reveal()),
142140
];
143141
}
144142
}

tests/Form/Type/EnumTypeTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Yokai\EnumBundle\Tests\Form\Type;
44

5-
use Prophecy\PhpUnit\ProphecyTrait;
65
use Symfony\Component\Form\FormInterface;
76
use Symfony\Component\Form\Test\TypeTestCase;
87
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
@@ -17,20 +16,6 @@
1716
*/
1817
class EnumTypeTest extends TypeTestCase
1918
{
20-
use ProphecyTrait;
21-
22-
private $enumRegistry;
23-
24-
protected function setUp(): void
25-
{
26-
$this->enumRegistry = $this->prophesize(EnumRegistry::class);
27-
$this->enumRegistry->has('state')->willReturn(false);
28-
$this->enumRegistry->has(GenderEnum::class)->willReturn(true);
29-
$this->enumRegistry->get(GenderEnum::class)->willReturn(new GenderEnum);
30-
31-
parent::setUp();
32-
}
33-
3419
public function testEnumOptionIsRequired(): void
3520
{
3621
$this->expectException(MissingOptionsException::class);
@@ -52,8 +37,11 @@ public function testEnumOptionValid(): void
5237

5338
protected function getExtensions(): array
5439
{
40+
$enumRegistry = new EnumRegistry();
41+
$enumRegistry->add(new GenderEnum());
42+
5543
return [
56-
new TestExtension($this->enumRegistry->reveal())
44+
new TestExtension($enumRegistry)
5745
];
5846
}
5947

tests/TestCase.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)