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

Controller/Test/CreateResourceControllerTest.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\CreateResourceController;
6
use Resourceful\FileCache\FileCache;
7
use JDesrosiers\Silex\Provider\JsonSchemaServiceProvider;
8
use PHPUnit_Framework_TestCase;
9
use Silex\Application;
10
use Silex\Provider\RoutingServiceProvider;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\Client;
13
14
class CreateResourceControllerTest extends PHPUnit_Framework_TestCase
15
{
16
    private $app;
17
    private $service;
18
    private $client;
19
20
    public function setUp()
21
    {
22
        $this->app = new Application();
23
        $this->app["debug"] = true;
24
25
        $this->app->register(new RoutingServiceProvider());
26
        $this->app->register(new JsonSchemaServiceProvider());
27
        $this->app["uniqid"] = function () {
28
            return uniqid();
29
        };
30
31
        $this->app["schemaService"] = new FileCache(__DIR__);
32
33
        $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...
34
        $this->app->get("/foo/{id}")->bind("/schema/foo");
35
        $this->app->post("/foo/", new CreateResourceController($this->service, "/schema/foo"));
36
        $this->app["json-schema.schema-store"]->add("/schema/foo", $this->app["schemaService"]->fetch("/schema/foo"));
37
38
        $this->client = new Client($this->app);
39
    }
40
41
    public function testCreate()
42
    {
43
        $foo = new \stdClass();
44
        $foo->id = "4ee8e29d45851";
45
46
        $this->app["uniqid"] = $foo->id;
47
48
        $headers = array(
49
            "HTTP_ACCEPT" => "application/json",
50
            "CONTENT_TYPE" => "application/json"
51
        );
52
        $this->client->request("POST", "/foo/", array(), array(), $headers, "{}");
53
        $response = $this->client->getResponse();
54
55
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
56
        $this->assertEquals("application/json", $response->headers->get("Content-Type"));
57
        $this->assertEquals("/foo/$foo->id", $response->headers->get("Location"));
58
        $this->assertJsonStringEqualsJsonString("{\"id\":\"$foo->id\"}", $response->getContent());
59
    }
60
61
    public function testBadRequest()
62
    {
63
        $this->app->error(function (\Exception $e, $code) {
64
            $errorMessage = '[{"code":303,"dataPath":"\/illegalField","schemaPath":"\/additionalProperties","message":"Additional properties not allowed"}]';
65
            $this->assertEquals($errorMessage, $e->getMessage());
66
        });
67
68
        $headers = array(
69
            "HTTP_ACCEPT" => "application/json",
70
            "CONTENT_TYPE" => "application/json"
71
        );
72
        $this->client->request("POST", "/foo/", array(), array(), $headers, '{"illegalField":"illegal"}');
73
        $response = $this->client->getResponse();
74
75
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
76
    }
77
78
    public function testSaveError()
79
    {
80
        $foo = new \stdClass();
81
        $foo->id = "4ee8e29d45851";
82
83
        $this->service->method("save")
84
            ->willReturn(false);
85
86
        $this->app->error(function (\Exception $e, $code) {
87
            $this->assertEquals("Failed to save resource", $e->getMessage());
88
        });
89
90
        $headers = array(
91
            "HTTP_ACCEPT" => "application/json",
92
            "CONTENT_TYPE" => "application/json"
93
        );
94
        $this->client->request("POST", "/foo/", array(), array(), $headers, "{}");
95
        $response = $this->client->getResponse();
96
97
        $this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode());
98
    }
99
}
100