Skip to content

Commit cf63259

Browse files
committed
PHPSpec to PHPUnit DependencyInjection
1 parent 307b7c0 commit cf63259

File tree

6 files changed

+201
-187
lines changed

6 files changed

+201
-187
lines changed

src/Bundle/spec/DependencyInjection/Compiler/Helper/TargetEntitiesResolverSpec.php

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

src/Bundle/spec/DependencyInjection/Driver/Exception/InvalidDriverExceptionSpec.php

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

src/Bundle/spec/DependencyInjection/Driver/Exception/UnknownDriverExceptionSpec.php

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Compiler\Helper;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\Helper\TargetEntitiesResolver;
18+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\Helper\TargetEntitiesResolverInterface;
19+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\AnimalInterface;
20+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\Bear;
21+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\BearInterface;
22+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\Fly;
23+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\FlyInterface;
24+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\MammalInterface;
25+
use Sylius\Bundle\ResourceBundle\Tests\Fixtures\Resource;
26+
27+
final class TargetEntitiesResolverTest extends TestCase
28+
{
29+
private TargetEntitiesResolverInterface $resolver;
30+
31+
protected function setUp(): void
32+
{
33+
$this->resolver = new TargetEntitiesResolver();
34+
}
35+
36+
public function testItIsATargetEntitiesResolver(): void
37+
{
38+
$this->assertInstanceOf(TargetEntitiesResolverInterface::class, $this->resolver);
39+
}
40+
41+
public function testItSkipsResourceInterface(): void
42+
{
43+
$emptyConfig = ['app.resource' => ['classes' => ['model' => Resource::class]]];
44+
45+
$this->assertSame([], $this->resolver->resolve($emptyConfig));
46+
}
47+
48+
public function testItAutodiscoversInterfacesBasedOnTheModelClass(): void
49+
{
50+
$flyConfig = ['app.fly' => ['classes' => ['model' => Fly::class]]];
51+
52+
$resolved = $this->resolver->resolve($flyConfig);
53+
54+
$this->assertCount(2, $resolved);
55+
$this->assertSame(Fly::class, $resolved[FlyInterface::class]);
56+
$this->assertSame(Fly::class, $resolved[AnimalInterface::class]);
57+
58+
$bearConfig = ['app.bear' => ['classes' => ['model' => Bear::class]]];
59+
60+
$resolved = $this->resolver->resolve($bearConfig);
61+
62+
$this->assertCount(3, $resolved);
63+
$this->assertSame(Bear::class, $resolved[BearInterface::class]);
64+
$this->assertSame(Bear::class, $resolved[MammalInterface::class]);
65+
$this->assertSame(Bear::class, $resolved[AnimalInterface::class]);
66+
}
67+
68+
public function testItAutodiscoversOnlyUniqueInterfacesBasedOnModelClasses(): void
69+
{
70+
$config = [
71+
'app.fly' => ['classes' => ['model' => Fly::class]],
72+
'app.bear' => ['classes' => ['model' => Bear::class]],
73+
];
74+
75+
$resolved = $this->resolver->resolve($config);
76+
77+
$this->assertCount(3, $resolved);
78+
$this->assertSame(Bear::class, $resolved[BearInterface::class]);
79+
$this->assertSame(Bear::class, $resolved[MammalInterface::class]);
80+
$this->assertSame(Fly::class, $resolved[FlyInterface::class]);
81+
82+
$this->assertArrayNotHasKey(AnimalInterface::class, $resolved);
83+
}
84+
85+
public function testItAutodiscoversInterfacesOnModelsWhenPassedMultipleTimes(): void
86+
{
87+
$config = [
88+
'app.fly' => ['classes' => ['model' => Fly::class]],
89+
'app.another_resource_with_fly_model' => ['classes' => ['model' => Fly::class]],
90+
];
91+
92+
$resolved = $this->resolver->resolve($config);
93+
94+
$this->assertCount(2, $resolved);
95+
$this->assertSame(Fly::class, $resolved[FlyInterface::class]);
96+
$this->assertSame(Fly::class, $resolved[AnimalInterface::class]);
97+
}
98+
99+
public function testItUsesTheInterfaceDefinedInTheConfig(): void
100+
{
101+
$config = [
102+
'app.deprecated' => ['classes' => ['model' => Resource::class, 'interface' => \Countable::class]],
103+
];
104+
105+
$resolved = @$this->resolver->resolve($config);
106+
107+
$this->assertCount(1, $resolved);
108+
$this->assertSame(Resource::class, $resolved[\Countable::class]);
109+
}
110+
111+
public function testItUsesTheInterfaceDefinedExplicitlyOverTheAutodiscoveredOne(): void
112+
{
113+
$config = [
114+
'app.deprecated' => ['classes' => ['model' => Resource::class, 'interface' => MammalInterface::class]],
115+
'app.bear' => ['classes' => ['model' => Bear::class]],
116+
];
117+
118+
$resolved = @$this->resolver->resolve($config);
119+
120+
$this->assertCount(3, $resolved);
121+
$this->assertSame(Resource::class, $resolved[MammalInterface::class]);
122+
$this->assertSame(Bear::class, $resolved[AnimalInterface::class]);
123+
$this->assertSame(Bear::class, $resolved[BearInterface::class]);
124+
}
125+
126+
public function testItThrowsAnExceptionIfModelClassCannotBeResolved(): void
127+
{
128+
$config = ['app.error' => ['classes' => ['interface' => \Countable::class]]];
129+
130+
$this->expectException(\InvalidArgumentException::class);
131+
132+
$this->resolver->resolve($config);
133+
}
134+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Driver\Exception;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Exception\InvalidDriverException;
18+
19+
final class InvalidDriverExceptionTest extends TestCase
20+
{
21+
private InvalidDriverException $invalidDriverException;
22+
23+
protected function setUp(): void
24+
{
25+
$this->invalidDriverException = new InvalidDriverException('driver', 'className');
26+
}
27+
28+
public function testHasAMessage(): void
29+
{
30+
$this->assertSame(
31+
'Driver "driver" is not supported by className.',
32+
$this->invalidDriverException->getMessage(),
33+
);
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Driver\Exception;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Exception\UnknownDriverException;
18+
19+
final class UnknownDriverExceptionTest extends TestCase
20+
{
21+
private UnknownDriverException $unknownDriverException;
22+
23+
protected function setUp(): void
24+
{
25+
$this->unknownDriverException = new UnknownDriverException('driver');
26+
}
27+
28+
public function testHasAMessage(): void
29+
{
30+
$this->assertSame('Unknown driver "driver".', $this->unknownDriverException->getMessage());
31+
}
32+
}

0 commit comments

Comments
 (0)