Passed
Pull Request — master (#10)
by Anatoly
03:03
created

EntryReadControllerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testRead() 0 22 1
A testReadNonExistentEntry() 0 20 1
1
<?php declare(strict_types=1);
2
3
namespace App\Bundle\Example\Tests\Service;
4
5
/**
6
 * Import classes
7
 */
8
use App\Exception\EntityNotFoundException;
9
use App\Tests\ContainerAwareTrait;
10
use App\Tests\DatabaseSchemaToolTrait;
11
use PHPUnit\Framework\TestCase;
12
use Sunrise\Http\Router\OpenApi\Test\OpenApiAssertKitTrait;
13
use Sunrise\Http\ServerRequest\ServerRequestFactory;
14
15
/**
16
 * EntryReadControllerTest
17
 */
18
class EntryReadControllerTest extends TestCase
19
{
20
    use ContainerAwareTrait;
21
    use DatabaseSchemaToolTrait;
22
    use OpenApiAssertKitTrait;
23
24
    /**
25
     * @var string
26
     */
27
    private const ROUTE_NAME = 'api_v1_entry_read';
28
29
    /**
30
     * @return void
31
     *
32
     * @runInSeparateProcess
33
     */
34
    public function testRead() : void
35
    {
36
        $container = $this->getContainer();
37
        $doctrine = $container->get('doctrine');
38
39
        $entityManager = $doctrine->getManager('master');
40
        $this->createDatabaseSchema($entityManager);
41
42
        $entryManager = $container->get('entryManager');
43
        $this->assertSame(0, $entryManager->countAll());
44
45
        $entry = $entryManager->create(['name' => 'foo', 'slug' => 'foo']);
46
47
        $request = (new ServerRequestFactory)
48
            ->createServerRequest('GET', '/api/v1/entry/' . $entry->getId()->toString())
49
            ->withAttribute('entryId', $entry->getId()->toString());
50
51
        $route = $container->get('router')->getRoute(self::ROUTE_NAME);
52
        $response = $route->handle($request);
53
54
        $this->assertSame(200, $response->getStatusCode());
55
        $this->assertResponseBodyMatchesDescription($route, $response);
56
    }
57
58
    /**
59
     * @return void
60
     *
61
     * @runInSeparateProcess
62
     */
63
    public function testReadNonExistentEntry() : void
64
    {
65
        $container = $this->getContainer();
66
        $doctrine = $container->get('doctrine');
67
68
        $entityManager = $doctrine->getManager('master');
69
        $this->createDatabaseSchema($entityManager);
70
71
        $entryManager = $container->get('entryManager');
72
        $this->assertSame(0, $entryManager->countAll());
73
74
        $request = (new ServerRequestFactory)
75
            ->createServerRequest('GET', '/api/v1/entry/6483341b-2ddb-43f0-8fc0-1c45ef9e31d8')
76
            ->withAttribute('entryId', '6483341b-2ddb-43f0-8fc0-1c45ef9e31d8');
77
78
        $this->expectException(EntityNotFoundException::class);
79
80
        $container->get('router')
81
            ->getRoute(self::ROUTE_NAME)
82
            ->handle($request);
83
    }
84
}
85