Passed
Pull Request — master (#27)
by Iakov
04:00
created

PersistStepTest::testCanBeConstructed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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