ComradeReaderTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 8
dl 0
loc 55
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getReader() 0 8 1
A testGet() 0 8 1
A testPassingPostAndGetParameters() 0 13 1
A testNotFound() 0 5 1
1
<?php
2
3
namespace ComradeReader\Test\Service;
4
use ComradeReader\Service\ComradeReader;
5
use ComradeReader\Test\Helpers\SimpleTestEntity;
6
use Doctrine\Common\Cache\VoidCache;
7
use Symfony\Component\Serializer\Encoder\JsonDecode;
8
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
9
use Symfony\Component\Serializer\Serializer;
10
11
/**
12
 * @package ComradeReader\Service
13
 */
14
class ComradeReaderTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @return ComradeReader
18
     */
19
    private function getReader()
20
    {
21
        $serializer = new Serializer(
22
            [new PropertyNormalizer()], [new JsonDecode()]
23
        );
24
25
        return new ComradeReader('http://localhost:8005', 'test', $serializer, new VoidCache());
26
    }
27
28
    /**
29
     * Basic test for valid request
30
     *
31
     * @see examples/test-api-responses/colors.php
32
     */
33
    public function testGet()
34
    {
35
        /** @var SimpleTestEntity $color */
36
        $color = $this->getReader()->get('/colors.php')->decodeIntoObject(SimpleTestEntity::class);
37
38
        $this->assertSame('red', $color->getColorName());
39
        $this->assertSame(1, $color->getId());
40
    }
41
42
    /**
43
     * Test passing POST parameters
44
     */
45
    public function testPassingPostAndGetParameters()
46
    {
47
        $reader = $this->getReader();
48
        $reader->setTokenFieldName('custom_token_field_name');
49
50
        $response = $reader->post(
51
            '/post-parameters.php?this_is_a_get_parameter=true', ['integer' => 1], 500
0 ignored issues
show
Documentation introduced by
array('integer' => 1) is of type array<string,integer,{"integer":"integer"}>, but the function expects a null|object<ComradeReade...ParametersBagInterface>.

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...
52
        )->getData();
53
54
        $this->assertSame('true', $response['GET']['this_is_a_get_parameter']);
55
        $this->assertSame('test', $response['GET']['custom_token_field_name']);
56
        $this->assertSame('1', $response['POST']['integer']);
57
    }
58
59
    /**
60
     * @expectedException \ComradeReader\Exception\ResourceNotFoundException
61
     * @expectedExceptionMessage Resource "/this-should-not-be-found" was not found on remote server, got a HTTP 404 error while trying to get the resource. Please verify if the resource exists, and if the HTTP method is correct
62
     */
63
    public function testNotFound()
64
    {
65
        $this->getReader()->request('GET', '/this-should-not-be-found')
66
            ->decodeIntoObject(SimpleTestEntity::class);
67
    }
68
}