Completed
Push — master ( 1958e9...0d5d7c )
by Enrico
04:50
created

src/Controller/Test/GetResourceControllerTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Resourceful\Controller\Test;
4
5
use Resourceful\Controller\GetResourceController;
6
use PHPUnit_Framework_TestCase;
7
use Silex\Application;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Client;
10
11
class GetResourceControllerTest extends PHPUnit_Framework_TestCase
12
{
13
    private $app;
14
    private $service;
15
    private $client;
16
17
    public function setUp()
18
    {
19
        $this->app = new Application();
20
        $this->app["debug"] = true;
21
22
        $this->service = $this->getMock("Doctrine\Common\Cache\Cache");
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
        $this->app->get("/foo/{id}", new GetResourceController($this->service));
24
25
        $this->client = new Client($this->app);
26
    }
27
28
    public function testGet()
29
    {
30
        $foo = new \stdClass();
31
        $foo->id = "4ee8e29d45851";
32
33
        $this->service->method("contains")
34
            ->with("/foo/4ee8e29d45851")
35
            ->willReturn(true);
36
37
        $this->service->method("fetch")
38
            ->with("/foo/4ee8e29d45851")
39
            ->willReturn($foo);
40
41
        $headers = array(
42
            "HTTP_ACCEPT" => "application/json",
43
        );
44
        $this->client->request("GET", "/foo/4ee8e29d45851", array(), array(), $headers);
45
        $response = $this->client->getResponse();
46
47
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
48
        $this->assertEquals("application/json", $response->headers->get("Content-Type"));
49
        $this->assertJsonStringEqualsJsonString('{"id":"4ee8e29d45851"}', $response->getContent());
50
    }
51
52
    public function testGetNotFound()
53
    {
54
        $this->service->method("contains")
55
            ->with("/foo/4ee8e29d45851")
56
            ->willReturn(false);
57
58
        $this->app->error(function (\Exception $e, $code) {
59
            $this->assertEquals("Not Found", $e->getMessage());
60
        });
61
62
        $headers = array(
63
            "HTTP_ACCEPT" => "application/json",
64
        );
65
        $this->client->request("GET", "/foo/4ee8e29d45851", array(), array(), $headers);
66
        $response = $this->client->getResponse();
67
68
        $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
69
    }
70
71
    public function testGetError()
72
    {
73
        $this->service->method("contains")
74
            ->with("/foo/4ee8e29d45851")
75
            ->willReturn(true);
76
77
        $this->service->method("fetch")
78
            ->with("/foo/4ee8e29d45851")
79
            ->willReturn(false);
80
81
        $this->app->error(function (\Exception $e, $code) {
82
            $this->assertEquals("Failed to retrieve resource", $e->getMessage());
83
        });
84
85
        $headers = array(
86
            "HTTP_ACCEPT" => "application/json",
87
        );
88
        $this->client->request("GET", "/foo/4ee8e29d45851", array(), array(), $headers);
89
        $response = $this->client->getResponse();
90
91
        $this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode());
92
    }
93
}
94