summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/ClientFactoryTest.php
blob: d84d0333a7e7e052566dd375ad1f2962c4bd35ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace Mediawiki\Api\Test\Unit\Guzzle;

use GuzzleHttp\HandlerStack;
use Mediawiki\Api\Guzzle\ClientFactory;
use Psr\Http\Message\RequestInterface;

/**
 * @author Christian Schmidt
 *
 * @covers Mediawiki\Api\Guzzle\ClientFactory
 */
class ClientFactoryTest extends \PHPUnit_Framework_TestCase {

	public function testNoConfig() {
		$clientFactory = new ClientFactory();

		$client = $clientFactory->getClient();

		$this->assertSame( $client, $clientFactory->getClient() );

		$config = $client->getConfig();
		$this->assertEquals( $config['headers']['User-Agent'], 'Addwiki - mediawiki-api-base' );

		$this->assertFalse( empty( $config['cookies'] ) );
	}

	public function testUserAgent() {
		$clientFactory = new ClientFactory( [ 'user-agent' => 'Foobar' ] );

		$client = $clientFactory->getClient();

		$this->assertNull( $client->getConfig( 'user-agent' ) );

		$config = $client->getConfig();
		$this->assertEquals( $config['headers']['User-Agent'], 'Foobar' );
	}

	public function testHeaders() {
		$clientFactory = new ClientFactory( [
			'headers' => [
				'User-Agent' => 'Foobar',
				'X-Foo' => 'Bar',
			]
		] );

		$client = $clientFactory->getClient();

		$headers = $client->getConfig( 'headers' );
		$this->assertCount( 2, $headers );
		$this->assertEquals( $headers['User-Agent'], 'Foobar' );
		$this->assertEquals( $headers['X-Foo'], 'Bar' );
	}

	public function testHandler() {
		$handler = HandlerStack::create();

		$clientFactory = new ClientFactory( [ 'handler' => $handler ] );

		$client = $clientFactory->getClient();

		$this->assertSame( $handler, $client->getConfig( 'handler' ) );
	}

	public function testMiddleware() {
		$invoked = false;
		$middleware = function () use ( &$invoked ) {
			return function () use ( &$invoked ) {
				$invoked = true;
			};
		};

		$clientFactory = new ClientFactory( [ 'middleware' => [ $middleware ] ] );

		$client = $clientFactory->getClient();

		$this->assertNull( $client->getConfig( 'middleware' ) );

		$request = $this->getMockBuilder( RequestInterface::class )->getMock();

		$handler = $client->getConfig( 'handler' );
		$handler->remove( 'http_errors' );
		$handler->remove( 'allow_redirects' );
		$handler->remove( 'cookies' );
		$handler->remove( 'prepare_body' );
		$handler( $request, [] );

		$this->assertTrue( $invoked );
	}
}