|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mediawiki\Api\Test\Unit\Guzzle; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use GuzzleHttp\HandlerStack; |
| 7 | +use Mediawiki\Api\Guzzle\ClientFactory; |
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author Christian Schmidt |
| 12 | + * |
| 13 | + * @covers Mediawiki\Api\Guzzle\ClientFactory |
| 14 | + */ |
| 15 | +class ClientFactoryTest extends \PHPUnit_Framework_TestCase { |
| 16 | + |
| 17 | + public function testNoConfig() { |
| 18 | + $clientFactory = new ClientFactory(); |
| 19 | + |
| 20 | + $client = $clientFactory->getClient(); |
| 21 | + |
| 22 | + $this->assertSame( $client, $clientFactory->getClient() ); |
| 23 | + |
| 24 | + $config = $client->getConfig(); |
| 25 | + $this->assertEquals( $config['headers']['User-Agent'], 'Addwiki - mediawiki-api-base' ); |
| 26 | + |
| 27 | + $this->assertFalse( empty( $config['cookies'] ) ); |
| 28 | + } |
| 29 | + |
| 30 | + public function testUserAgent() { |
| 31 | + $clientFactory = new ClientFactory( array( 'user-agent' => 'Foobar' ) ); |
| 32 | + |
| 33 | + $client = $clientFactory->getClient(); |
| 34 | + |
| 35 | + $this->assertNull( $client->getConfig('user-agent') ); |
| 36 | + |
| 37 | + $config = $client->getConfig(); |
| 38 | + $this->assertEquals( $config['headers']['User-Agent'], 'Foobar' ); |
| 39 | + } |
| 40 | + |
| 41 | + public function testHeaders() { |
| 42 | + $clientFactory = new ClientFactory( array( |
| 43 | + 'headers' => array( |
| 44 | + 'User-Agent' => 'Foobar', |
| 45 | + 'X-Foo' => 'Bar', |
| 46 | + ) |
| 47 | + ) ); |
| 48 | + |
| 49 | + $client = $clientFactory->getClient(); |
| 50 | + |
| 51 | + $headers = $client->getConfig('headers'); |
| 52 | + $this->assertCount( 2, $headers ); |
| 53 | + $this->assertEquals( $headers['User-Agent'], 'Foobar' ); |
| 54 | + $this->assertEquals( $headers['X-Foo'], 'Bar' ); |
| 55 | + } |
| 56 | + |
| 57 | + public function testHandler() { |
| 58 | + $handler = HandlerStack::create(); |
| 59 | + |
| 60 | + $clientFactory = new ClientFactory( array( 'handler' => $handler ) ); |
| 61 | + |
| 62 | + $client = $clientFactory->getClient(); |
| 63 | + |
| 64 | + $this->assertSame( $handler, $client->getConfig('handler') ); |
| 65 | + } |
| 66 | + |
| 67 | + public function testMiddleware() { |
| 68 | + $invoked = false; |
| 69 | + $middleware = function() use (&$invoked) { |
| 70 | + return function() use (&$invoked) { |
| 71 | + $invoked = true; |
| 72 | + }; |
| 73 | + }; |
| 74 | + |
| 75 | + $clientFactory = new ClientFactory( array( 'middleware' => array( $middleware ) ) ); |
| 76 | + |
| 77 | + $client = $clientFactory->getClient(); |
| 78 | + |
| 79 | + $this->assertNull( $client->getConfig('middleware') ); |
| 80 | + |
| 81 | + $request = $this->getMockBuilder( RequestInterface::class )->getMock(); |
| 82 | + |
| 83 | + $handler = $client->getConfig( 'handler' ); |
| 84 | + $handler->remove( 'http_errors' ); |
| 85 | + $handler->remove( 'allow_redirects' ); |
| 86 | + $handler->remove( 'cookies' ); |
| 87 | + $handler->remove( 'prepare_body' ); |
| 88 | + $handler($request, array()); |
| 89 | + |
| 90 | + $this->assertTrue( $invoked ); |
| 91 | + } |
| 92 | +} |
0 commit comments