ConnectorTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 108
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A constructor() 0 4 1
A constructorWithClient() 0 5 1
A getBinary() 0 5 1
A getByRefNeedsCorrectKeysInRef() 0 6 1
A isIdProvider() 0 12 1
A isIdValid() 0 4 1
A getByIdsWithEmptyArrayReturnsArray() 0 5 1
A generateId() 0 6 1
A getHttpClient() 0 8 1
1
<?php
2
3
namespace Communibase;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
9
/**
10
 * Class ConnectorTest
11
 *
12
 * @todo still alot more tests should be added, these are a first start. Feel free to submit new/better tests!
13
 * @package Communibase
14
 */
15
class ConnectorTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function constructor()
21
    {
22
        new Connector('', '');
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function constructorWithClient()
29
    {
30
        $connector = new Connector('test', '', $this->getHttpClient());
31
        $this->assertInstanceOf(Connector::class, $connector);
32
    }
33
34
    /**
35
     * @test
36
     * @expectedException Exception
37
     */
38
    public function getBinary()
39
    {
40
        $connector = new Connector('test', '');
41
        $connector->getBinary('');
42
    }
43
44
    /**
45
     * @test
46
     * @throws Exception
47
     */
48
    public function getByRefNeedsCorrectKeysInRef()
49
    {
50
        $connector = new Connector('test', '');
51
        $this->setExpectedException(\InvalidArgumentException::class);
52
        $connector->getByRef([]);
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function isIdProvider()
59
    {
60
        return [
61
            ['507f1f77bcf86cd799439011', true],
62
            ['507f191e810c19729de860ea', true],
63
            ['54b7ed2b49726734cab0570c', true],
64
            ['123c', false],
65
            ['t', false],
66
            ['t', false],
67
            ['58a2d90012f9ae00c647d0fc((\'.,.', false],
68
        ];
69
    }
70
71
    /**
72
     * @dataProvider isIdProvider
73
     * @test
74
     * @param $id
75
     * @param $isValid
76
     */
77
    public function isIdValid($id, $isValid)
78
    {
79
        $this->assertSame($isValid, Connector::isIdValid($id));
80
    }
81
82
    /**
83
     * @test
84
     * @throws Exception
85
     */
86
    public function getByIdsWithEmptyArrayReturnsArray() {
87
        $connector = new Connector('test', '');
88
        $this->assertSame([], $connector->getByIds('', []));
89
        $this->assertSame([], $connector->getByIds('Person', []));
90
    }
91
92
93
    /**
94
     * @test
95
     * @depends isIdValid
96
     */
97
    public function generateId()
98
    {
99
        $id = Connector::generateId();
100
101
        $this->assertTrue(Connector::isIdValid($id));
102
    }
103
104
    /**
105
     * Create a new HttpClient (as a mock)
106
     * The responses can be injected thus easily reusable
107
     *
108
     * @see http://docs.guzzlephp.org/en/latest/testing.html
109
     *
110
     * @param array $responses
111
     *
112
     * @return Client
113
     */
114
    private function getHttpClient(array $responses = [])
115
    {
116
117
        $mock = new MockHandler($responses);
118
        $handler = HandlerStack::create($mock);
119
120
        return new Client(['handler' => $handler]);
121
    }
122
}
123