Completed
Push — master ( 45cba9...0c520e )
by Iakov
08:04 queued 04:40
created

testGetRequiredArtifacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kami\ApiCoreBundle\Tests\RequestProcessor\Step\Common;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
6
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\FetchEntityByIdStep;
7
use Kami\ApiCoreBundle\Tests\Entity\MyModel;
8
use Kami\ApiCoreBundle\Tests\Repository\TestRepository;
9
use Kami\Component\RequestProcessor\Artifact;
10
use Kami\Component\RequestProcessor\ArtifactCollection;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
15
class FetchEntityByIdStepTest extends TestCase
16
{
17
18
    public function testCanBeConstructed()
19
    {
20
        $registry = $this->createMock(Registry::class);
21
22
        $step = new FetchEntityByIdStep($registry);
23
        $this->assertInstanceOf(FetchEntityByIdStep::class, $step);
24
    }
25
26
    public function testGetRequiredArtifacts()
27
    {
28
        $registry = $this->createMock(Registry::class);
29
30
        $step = new FetchEntityByIdStep($registry);
31
        $this->assertEquals(['reflection', 'access_granted'], $step->getRequiredArtifacts());
32
    }
33
34
    public function testExecute()
35
    {
36
        $repositoryMock = $this->createMock(TestRepository::class);
37
        $repositoryMock->expects($this->at(0))->method('find')->willReturn(new MyModel());
38
        $registry = $this->createMock(Registry::class);
39
        $registry->expects($this->any())->method('getRepository')->willReturn($repositoryMock);
40
41
        $step = new FetchEntityByIdStep($registry);
42
43
        $step->setArtifacts(new ArtifactCollection([
44
            new Artifact('reflection', new \ReflectionClass(MyModel::class))
45
        ]));
46
47
        $response = $step->execute(new Request());
48
        $this->assertInstanceOf(ArtifactCollection::class, $response);
49
        $this->assertInstanceOf(MyModel::class, $response->get('entity')->getValue());
50
    }
51
52
    public function testFailureExecuteWithoutEntity ()
53
    {
54
        $repositoryMock = $this->createMock(TestRepository::class);
55
        $repositoryMock->expects($this->at(0))->method('find')->willReturn(null);
56
        $registry = $this->createMock(Registry::class);
57
        $registry->expects($this->any())->method('getRepository')->willReturn($repositoryMock);
58
        $step = new FetchEntityByIdStep($registry);
59
        $step->setArtifacts(new ArtifactCollection([
60
            new Artifact('reflection',  new \ReflectionClass(MyModel::class))
61
        ]));
62
63
        $this->expectException(NotFoundHttpException::class);
64
        $step->execute(new Request());
65
    }
66
}
67