1<?php
2/**
3 * This file is part of the Tmdb PHP API created by Michael Roterman.
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @package Tmdb
9 * @author Michael Roterman <michael@wtfz.net>
10 * @copyright (c) 2013, Michael Roterman
11 * @version 0.0.1
12 */
13namespace Tmdb\Tests\Api;
14
15use Tmdb\HttpClient\Response;
16use Tmdb\RequestToken;
17
18class AuthenticationTest extends TestCase
19{
20    /**
21     * @test
22     */
23    public function shouldGetNewToken()
24    {
25        $api = $this->getApiWithMockedHttpAdapter();
26
27        $this->getAdapter()
28            ->expects($this->once())
29            ->method('get')
30            ->with($this->getRequest('https://api.themoviedb.org/3/authentication/token/new'))
31        ;
32
33        $api->getNewToken();
34    }
35
36    /**
37     * @test
38     */
39    public function shouldGetNewSession()
40    {
41        $api = $this->getApiWithMockedHttpAdapter();
42
43        $this->getAdapter()
44            ->expects($this->once())
45            ->method('get')
46            ->with($this->getRequest('https://api.themoviedb.org/3/authentication/session/new', ['request_token' => 'request_token']))
47        ;
48
49        $api->getNewSession(new RequestToken('request_token'));
50    }
51
52    /**
53     * @test
54     */
55    public function shouldValidateRequestTokenWithLogin()
56    {
57        $api = $this->getApiWithMockedHttpAdapter();
58
59        $response = new Response(200);
60        $response->setBody(json_encode([
61            'success' => true,
62            'request_token' => 'abcdefghijklmnopqrstuvwxyz'
63        ]));
64
65        $this->getAdapter()
66            ->expects($this->any())
67            ->method('get')
68            ->with($this->getRequest('https://api.themoviedb.org/3/authentication/token/validate_with_login', [
69                'request_token' => 'request_token',
70                'username' => 'piet',
71                'password' => 'henk'
72            ]))
73            ->willReturn($response)
74        ;
75
76        $api->validateRequestTokenWithLogin(new RequestToken('request_token'), 'piet', 'henk');
77    }
78
79    /**
80     * @test
81     */
82    public function shouldGetSessionTokenWithLogin()
83    {
84        $api = $this->getApiWithMockedHttpAdapter();
85
86        $response = new Response(200);
87        $response->setBody(json_encode([
88            'success' => true,
89            'request_token' => 'abcdefghijklmnopqrstuvwxyz'
90        ]));
91
92        $api->getClient()->getAdapter()
93            ->expects($this->any())
94            ->method('get')
95            ->with($this->getRequest('https://api.themoviedb.org/3/authentication/token/validate_with_login', [
96                'request_token' => 'request_token',
97                'username' => 'piet',
98                'password' => 'henk'
99            ]))
100            ->willReturn($response)
101        ;
102
103        $api->getSessionTokenWithLogin(new RequestToken('request_token'), 'piet', 'henk');
104    }
105
106    /**
107     * @test
108     * @expectedException \InvalidArgumentException
109     */
110    public function shouldThrowExceptionWhenNotValidated()
111    {
112        $api = $this->getApiWithMockedHttpAdapter();
113
114        $response = new Response(200);
115        $response->setBody(json_encode([
116            'success' => false
117        ]));
118
119        $api->getClient()->getAdapter()
120            ->expects($this->any())
121            ->method('get')
122            ->with($this->getRequest('https://api.themoviedb.org/3/authentication/token/validate_with_login', [
123                'request_token' => 'request_token',
124                'username' => 'piet',
125                'password' => 'henk'
126            ]))
127            ->will($this->returnValue($response))
128        ;
129
130        $api->getSessionTokenWithLogin(new RequestToken('request_token'), 'piet', 'henk');
131    }
132
133    /**
134     * @test
135     */
136    public function shouldGetNewGuestSession()
137    {
138        $api = $this->getApiWithMockedHttpAdapter();
139
140        $this->getAdapter()
141            ->expects($this->once())
142            ->method('get')
143            ->with($this->getRequest('https://api.themoviedb.org/3/authentication/guest_session/new'))
144        ;
145
146        $api->getNewGuestSession();
147    }
148
149    protected function getApiClass()
150    {
151        return 'Tmdb\Api\Authentication';
152    }
153}
154