forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateProviderTest.php
72 lines (60 loc) · 3.08 KB
/
CreateProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\State;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\CreateProvider;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyResourceWithComplexConstructor;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Employee;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
class CreateProviderTest extends TestCase
{
use ProphecyTrait;
public function testProvide(): void
{
$link = new Link(identifiers: ['id'], fromClass: Company::class, parameterName: 'company');
$decorated = $this->prophesize(ProviderInterface::class);
$decorated->provide(
new Get(uriVariables: ['id' => $link], class: Company::class),
['company' => 1]
)->shouldBeCalled()->willReturn(new Company());
$operation = new Post(class: Employee::class, uriTemplate: '/company/{company}/employees', uriVariables: ['company' => $link]);
$createProvider = new CreateProvider($decorated->reveal());
$createProvider->provide($operation, ['company' => 1]);
}
public function testProvideFailsProperlyOnComplexConstructor(): void
{
$link = new Link(identifiers: ['id'], fromClass: Company::class, parameterName: 'company');
$decorated = $this->prophesize(ProviderInterface::class);
$decorated->provide(
new Get(uriVariables: ['id' => $link], class: Company::class),
['company' => 1]
)->shouldBeCalled()->willReturn(new Company());
$operation = new Post(class: DummyResourceWithComplexConstructor::class, uriTemplate: '/company/{company}/employees', uriVariables: ['company' => $link]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('An error occurred while trying to create an instance of the "ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyResourceWithComplexConstructor" resource. Consider writing your own "ApiPlatform\State\ProviderInterface" implementation and setting it as `provider` on your operation instead.');
$createProvider = new CreateProvider($decorated->reveal());
$createProvider->provide($operation, ['company' => 1]);
}
public function testSkipWhenController(): void
{
$decorated = $this->prophesize(ProviderInterface::class);
$operation = new Post(class: Employee::class, uriTemplate: '/company/{company}/employees', controller: 'test');
$decorated->provide($operation, ['company' => 1], [])->shouldBeCalled()->willReturn(new Employee());
$createProvider = new CreateProvider($decorated->reveal());
$createProvider->provide($operation, ['company' => 1]);
}
}