Completed
Push — upgrade ( c4e463 )
by Kamil
22:47
created

NotificationControllerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 0
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A it_returns_an_empty_json_response_upon_client_exception() 0 13 1
A it_returns_json_response_from_client_on_success() 0 21 1
A setUp() 0 13 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\AdminBundle\Tests\Controller;
13
14
use GuzzleHttp\ClientInterface;
15
use GuzzleHttp\Exception\ConnectException;
16
use Http\Message\MessageFactory;
17
use Prophecy\Argument;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\StreamInterface;
21
use Sylius\Bundle\AdminBundle\Controller\NotificationController;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
25
/**
26
 * @author Jan Góralski <[email protected]>
27
 */
28
final class NotificationControllerTest extends \PHPUnit_Framework_TestCase
29
{
30
    /**
31
     * @var ClientInterface
32
     */
33
    private $client;
34
35
    /**
36
     * @var MessageFactory
37
     */
38
    private $messageFactory;
39
40
    /**
41
     * @var NotificationController
42
     */
43
    private $controller;
44
45
    /**
46
     * @var string
47
     */
48
    private static $hubUri = 'www.doesnotexist.test.com';
49
50
    /**
51
     * @test
52
     */
53
    public function it_returns_an_empty_json_response_upon_client_exception()
54
    {
55
        $this->messageFactory->createRequest(Argument::cetera())
0 ignored issues
show
Bug introduced by
The call to createRequest() misses a required argument $uri.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
The method willReturn() does not seem to exist on object<Psr\Http\Message\RequestInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            ->willReturn($this->prophesize(RequestInterface::class)->reveal())
57
        ;
58
59
        $this->client->send(Argument::cetera())->willThrow(ConnectException::class);
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::cetera() is of type object<Prophecy\Argument\Token\AnyValuesToken>, but the function expects a object<Psr\Http\Message\RequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method willThrow() does not seem to exist on object<Psr\Http\Message\ResponseInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
        $emptyResponse = $this->controller->getVersionAction(new Request());
62
63
        $this->assertEquals(JsonResponse::HTTP_NO_CONTENT, $emptyResponse->getStatusCode());
64
        $this->assertEquals('""', $emptyResponse->getContent());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function it_returns_json_response_from_client_on_success()
71
    {
72
        $content = json_encode(['version' => '9001']);
73
74
        $this->messageFactory->createRequest(Argument::cetera())
0 ignored issues
show
Bug introduced by
The call to createRequest() misses a required argument $uri.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
The method willReturn() does not seem to exist on object<Psr\Http\Message\RequestInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            ->willReturn($this->prophesize(RequestInterface::class)->reveal())
76
        ;
77
78
        $stream = $this->prophesize(StreamInterface::class);
79
        $stream->getContents()->willReturn($content);
80
81
        $externalResponse = $this->prophesize(ResponseInterface::class);
82
        $externalResponse->getBody()->willReturn($stream->reveal());
83
84
        $this->client->send(Argument::cetera())->willReturn($externalResponse->reveal());
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::cetera() is of type object<Prophecy\Argument\Token\AnyValuesToken>, but the function expects a object<Psr\Http\Message\RequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method willReturn() does not seem to exist on object<Psr\Http\Message\ResponseInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
86
        $response = $this->controller->getVersionAction(new Request());
87
88
        $this->assertEquals(JsonResponse::HTTP_OK, $response->getStatusCode());
89
        $this->assertEquals($content, $response->getContent());
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function setUp()
96
    {
97
        $this->client = $this->prophesize(ClientInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Guzzl...ClientInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<GuzzleHttp\ClientInterface> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
        $this->messageFactory = $this->prophesize(MessageFactory::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Http\...\MessageFactory::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Http\Message\MessageFactory> of property $messageFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
100
        $this->controller = new NotificationController(
101
            $this->client->reveal(),
102
            $this->messageFactory->reveal(),
103
            self::$hubUri
104
        );
105
106
        parent::setUp();
107
    }
108
}
109