1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kami\ApiCoreBundle\Tests\RequestProcessor\Step\Common; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
6
|
|
|
use Doctrine\ORM\Repository\RepositoryFactory; |
7
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\ProcessorResponse; |
8
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\FetchEntityByIdStep; |
9
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\GetReflectionFromRequestStep; |
10
|
|
|
use Kami\ApiCoreBundle\Tests\Entity\MyModel; |
11
|
|
|
use Kami\ApiCoreBundle\Tests\fixtures\Entity; |
12
|
|
|
use Kami\ApiCoreBundle\Tests\Repository\TestRepository; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
|
17
|
|
|
class FetchEntityByIdStepTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public function testExecute() |
21
|
|
|
{ |
22
|
|
|
$repositoryMock = $this->createMock(TestRepository::class); |
23
|
|
|
$repositoryMock->expects($this->at(0))->method('find')->willReturn(new MyModel()); |
24
|
|
|
$doctrineMock = $this->createMock(Registry::class); |
25
|
|
|
$doctrineMock->expects($this->at(0))->method('getRepository')->willReturn($repositoryMock); |
26
|
|
|
$request = new Request(); |
27
|
|
|
$request->attributes->set('id', 1); |
28
|
|
|
$step = new FetchEntityByIdStep($doctrineMock); |
29
|
|
|
$step->setRequest($request); |
30
|
|
|
$step->setPreviousResponse(new ProcessorResponse($request, ['reflection' => new \ReflectionClass(MyModel::class)])); |
31
|
|
|
$response = $step->execute(); |
32
|
|
|
|
33
|
|
|
$this->assertInstanceOf(ProcessorResponse::class, $response); |
34
|
|
|
$this->assertInstanceOf(MyModel::class, $response->getData()['entity']); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testExecuteFailure() |
38
|
|
|
{ |
39
|
|
|
$repositoryMock = $this->createMock(TestRepository::class); |
40
|
|
|
$doctrineMock = $this->createMock(Registry::class); |
41
|
|
|
$doctrineMock->expects($this->at(0))->method('getRepository')->willReturn($repositoryMock); |
42
|
|
|
$request = new Request(); |
43
|
|
|
$step = new FetchEntityByIdStep($doctrineMock); |
44
|
|
|
$step->setRequest($request); |
45
|
|
|
$step->setPreviousResponse(new ProcessorResponse($request, ['reflection' => new \ReflectionClass(MyModel::class)])); |
46
|
|
|
|
47
|
|
|
$this->expectException(NotFoundHttpException::class); |
48
|
|
|
$step->execute(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testRequiresBefore() |
52
|
|
|
{ |
53
|
|
|
$step = new FetchEntityByIdStep($this->createMock(Registry::class)); |
54
|
|
|
$this->assertEquals([GetReflectionFromRequestStep::class], $step->requiresBefore()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|