Passed
Push — master ( ed8525...da3e44 )
by Iakov
03:51
created

DeleteStepTest::testExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Kami\ApiCoreBundle\Tests\RequestProcessor\Step\Common;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
6
use Doctrine\ORM\EntityManager;
7
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\FetchEntityByIdStep;
8
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\ValidateResourceAccessStep;
9
use Kami\ApiCoreBundle\RequestProcessor\Step\Delete\DeleteStep;
10
use Kami\ApiCoreBundle\Tests\Entity\MyModel;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\Request;
13
use Kami\ApiCoreBundle\RequestProcessor\ProcessorResponse;
14
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15
16
class DeleteStepTest extends TestCase
17
{
18
19
    public function testCanBeConstructed()
20
    {
21
        $step = new DeleteStep($this->createMock(Registry::class));
22
        $this->assertInstanceOf(DeleteStep::class, $step);
23
    }
24
25
    public function testRequiresBefore()
26
    {
27
        $step = new DeleteStep($this->createMock(Registry::class));
28
        $this->assertEquals([ValidateResourceAccessStep::class, FetchEntityByIdStep::class], $step->requiresBefore());
29
    }
30
31
    public function testExecute()
32
    {
33
        $entityManagerMock = $this->createMock(EntityManager::class);
34
        $doctrineMock = $this->createMock(Registry::class);
35
        $doctrineMock->expects($this->any())->method('getManager')->willReturn($entityManagerMock);
36
37
        $step = new DeleteStep($doctrineMock);
38
        $request = new Request();
39
        $step->setRequest($request);
40
        $step->setPreviousResponse(new ProcessorResponse($request, [
41
            'entity' => new MyModel()
42
        ]));
43
44
        $response = $step->execute();
45
        $this->assertInstanceOf(ProcessorResponse::class, $response);
46
        $this->assertEquals(null, $response->getData()['response_data']);
47
    }
48
49
    public function testExecuteFailure()
50
    {
51
        $entityManagerMock = $this->createMock(EntityManager::class);
52
        $entityManagerMock->expects($this->any())->method('remove')->will($this->throwException(new BadRequestHttpException()));
53
        $doctrineMock = $this->createMock(Registry::class);
54
        $doctrineMock->expects($this->any())->method('getManager')->willReturn($entityManagerMock);
55
56
        $step = new DeleteStep($doctrineMock);
57
        $request = new Request();
58
        $step->setRequest($request);
59
        $step->setPreviousResponse(new ProcessorResponse($request, [
60
            'entity' => new MyModel()
61
        ]));
62
63
        $this->expectException(BadRequestHttpException::class);
64
        $step->execute();
65
    }
66
67
}
68