From 492e672b234acc221df11df9f1429d3a1159debf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Sat, 3 Mar 2012 21:02:47 +0100 Subject: [PATCH] Added tests for StreamContextFactory --- .../Test/Util/StreamContextFactoryTest.php | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 tests/Composer/Test/Util/StreamContextFactoryTest.php diff --git a/tests/Composer/Test/Util/StreamContextFactoryTest.php b/tests/Composer/Test/Util/StreamContextFactoryTest.php new file mode 100644 index 000000000..eab6e7936 --- /dev/null +++ b/tests/Composer/Test/Util/StreamContextFactoryTest.php @@ -0,0 +1,96 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Util; + +use Composer\Util\StreamContextFactory; + +class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase +{ + protected function setUp() + { + unset($_SERVER['HTTP_PROXY']); + unset($_SERVER['http_proxy']); + } + + protected function tearDown() + { + unset($_SERVER['HTTP_PROXY']); + unset($_SERVER['http_proxy']); + } + + /** + * @dataProvider dataGetContext + */ + public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams) + { + $context = StreamContextFactory::getContext($defaultOptions, $defaultParams); + $options = stream_context_get_options($context); + $params = stream_context_get_params($context); + + $this->assertEquals($expectedOptions, $options); + $this->assertEquals($expectedParams, $params); + } + + public function dataGetContext() + { + return array( + array( + array(), array(), + array('options' => array()), array() + ), + array( + $a = array('http' => array('method' => 'GET')), $a, + array('options' => $a, 'notification' => $f = function() {}), array('notification' => $f) + ), + ); + } + + public function testHttpProxy() + { + $_SERVER['HTTP_PROXY'] = 'http://username:password@proxyserver.net:port/'; + $_SERVER['http_proxy'] = 'http://proxyserver/'; + + $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET'))); + $options = stream_context_get_options($context); + + $this->assertSame('http://proxyserver/', $_SERVER['http_proxy']); + + $this->assertEquals(array('http' => array( + 'proxy' => 'tcp://username:password@proxyserver.net:port/', + 'request_fulluri' => true, + 'method' => 'GET', + )), $options); + } + + public function testSSLProxy() + { + $_SERVER['http_proxy'] = 'https://proxyserver/'; + + if (extension_loaded('openssl')) { + $context = StreamContextFactory::getContext(); + $options = stream_context_get_options($context); + + $this->assertSame(array('http' => array( + 'proxy' => 'ssl://proxyserver/', + 'request_fulluri' => true, + )), $options); + } else { + try { + StreamContextFactory::getContext(); + $this->fail(); + } catch (\Exception $e) { + $this->assertInstanceOf('RuntimeException', $e); + } + } + } +}