Completed
Push — master ( 4c45e0...1a7b7a )
by Дмитрий
02:22
created

AbstractProviderTestCase::mockClientResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 22
rs 9.7998
c 1
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace Test\OpenID\Provider;
8
9
use ReflectionClass;
10
use SocialConnect\Common\Http\Client\ClientInterface;
11
use SocialConnect\Provider\Consumer;
12
use SocialConnect\Provider\Session\SessionInterface;
13
use Test\TestCase;
14
15
abstract class AbstractProviderTestCase extends TestCase
16
{
17
    /**
18
     * @return string
19
     */
20
    abstract protected function getProviderClassName();
21
22
    /**
23
     * @param mixed $responseData
24
     * @param int $responseCode
25
     * @param bool $mockFromRequest
26
     * @return \PHPUnit_Framework_MockObject_MockObject
27
     */
28
    protected function mockClientResponse($responseData, $responseCode = 200, $mockFromRequest = false)
29
    {
30
        $mockedHttpClient = $this->getMockBuilder(\SocialConnect\Common\Http\Client\Curl::class)
31
            ->getMock();
32
33
        $response = new \SocialConnect\Common\Http\Response(
34
            $responseCode,
35
            $responseData,
36
            []
37
        );
38
39
        if ($mockFromRequest) {
40
            $mockedHttpClient->expects($this->once())
41
                ->method('fromRequest')
42
                ->willReturn($response);
43
        } else {
44
            $mockedHttpClient->expects($this->once())
45
                ->method('request')
46
                ->willReturn($response);
47
        }
48
49
        return $mockedHttpClient;
50
    }
51
52
    /**
53
     * @param object $object
54
     * @param string $name
55
     * @param array $params
56
     * @return mixed
57
     */
58
    protected static function callProtectedMethod($object, $name, array $params = [])
59
    {
60
        $class = new ReflectionClass($object);
61
62
        $method = $class->getMethod($name);
63
        $method->setAccessible(true);
64
65
        return $method->invokeArgs($object, $params);
66
    }
67
68
    /**
69
     * @param ClientInterface|null $httpClient
70
     * @return \SocialConnect\OpenID\AbstractProvider
71
     */
72
    protected function getProvider(ClientInterface $httpClient = null, SessionInterface $session = null)
73
    {
74
        if (!$httpClient) {
75
            $httpClient = $this->getMockBuilder(\SocialConnect\Common\Http\Client\Curl::class)
76
                ->disableOriginalConstructor()
77
                ->disableProxyingToOriginalMethods()
78
                ->getMock();
79
        }
80
81
        if (!$session) {
82
            $session = $this->getMockBuilder(\SocialConnect\Provider\Session\Session::class)
83
                ->disableOriginalConstructor()
84
                ->disableProxyingToOriginalMethods()
85
                ->getMock();
86
        }
87
88
        $className = $this->getProviderClassName();
89
90
        return new $className(
91
            $httpClient,
92
            $session,
93
            new Consumer(
94
                'unknown',
95
                'unkwown'
96
            ),
97
            [
98
                'redirectUri' => 'http://localhost:8000/'
99
            ]
100
        );
101
    }
102
103
    public function testGetOpenIDUrl()
104
    {
105
        parent::assertInternalType('string', $this->getProvider()->getOpenIdUrl());
106
    }
107
108
    public function testGetBaseUriReturnString()
109
    {
110
        parent::assertInternalType('string', $this->getProvider()->getBaseUri());
111
    }
112
113
    public function testGetNameReturnString()
114
    {
115
        parent::assertInternalType('string', $this->getProvider()->getName());
116
    }
117
}
118