2012-03-05 10:26:07 +00:00
< ? php
/*
* This file is part of Composer .
*
* ( c ) Nils Adermann < naderman @ naderman . de >
* Jordi Boggiano < j . boggiano @ seld . be >
*
* 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\RemoteFilesystem ;
class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase
{
public function testGetOptionsForUrl ()
{
$io = $this -> getMock ( 'Composer\IO\IOInterface' );
$io
-> expects ( $this -> once ())
-> method ( 'hasAuthorization' )
-> will ( $this -> returnValue ( false ))
;
2012-10-18 16:43:31 +00:00
$res = $this -> callGetOptionsForUrl ( $io , array ( 'http://example.org' , array ()));
2012-03-18 20:05:10 +00:00
$this -> assertTrue ( isset ( $res [ 'http' ][ 'header' ]) && false !== strpos ( $res [ 'http' ][ 'header' ], 'User-Agent' ), 'getOptions must return an array with a header containing a User-Agent' );
2012-03-05 10:26:07 +00:00
}
public function testGetOptionsForUrlWithAuthorization ()
{
$io = $this -> getMock ( 'Composer\IO\IOInterface' );
$io
-> expects ( $this -> once ())
-> method ( 'hasAuthorization' )
-> will ( $this -> returnValue ( true ))
;
$io
-> expects ( $this -> once ())
-> method ( 'getAuthorization' )
-> will ( $this -> returnValue ( array ( 'username' => 'login' , 'password' => 'password' )))
;
2012-10-18 16:43:31 +00:00
$options = $this -> callGetOptionsForUrl ( $io , array ( 'http://example.org' , array ()));
2012-03-05 10:26:07 +00:00
$this -> assertContains ( 'Authorization: Basic' , $options [ 'http' ][ 'header' ]);
}
2012-10-03 13:09:47 +00:00
public function testGetOptionsForUrlWithStreamOptions ()
{
$io = $this -> getMock ( 'Composer\IO\IOInterface' );
$io
-> expects ( $this -> once ())
-> method ( 'hasAuthorization' )
-> will ( $this -> returnValue ( true ))
;
$streamOptions = array ( 'ssl' => array (
'allow_self_signed' => true ,
));
2012-10-18 16:43:31 +00:00
$res = $this -> callGetOptionsForUrl ( $io , array ( 'https://example.org' , array ()), $streamOptions );
2012-10-03 13:09:47 +00:00
$this -> assertTrue ( isset ( $res [ 'ssl' ]) && isset ( $res [ 'ssl' ][ 'allow_self_signed' ]) && true === $res [ 'ssl' ][ 'allow_self_signed' ], 'getOptions must return an array with a allow_self_signed set to true' );
}
2012-10-18 16:43:31 +00:00
public function testGetOptionsForUrlWithCallOptionsKeepsHeader ()
{
$io = $this -> getMock ( 'Composer\IO\IOInterface' );
$io
-> expects ( $this -> once ())
-> method ( 'hasAuthorization' )
-> will ( $this -> returnValue ( true ))
;
$streamOptions = array ( 'http' => array (
'header' => 'Foo: bar' ,
));
$res = $this -> callGetOptionsForUrl ( $io , array ( 'https://example.org' , $streamOptions ));
$this -> assertTrue ( isset ( $res [ 'http' ][ 'header' ]), 'getOptions must return an array with a http.header key' );
$this -> assertGreaterThan ( strlen ( 'Foo: bar' ), strlen ( $res [ 'http' ][ 'header' ]));
}
2012-03-05 10:26:07 +00:00
public function testCallbackGetFileSize ()
{
$fs = new RemoteFilesystem ( $this -> getMock ( 'Composer\IO\IOInterface' ));
$this -> callCallbackGet ( $fs , STREAM_NOTIFY_FILE_SIZE_IS , 0 , '' , 0 , 0 , 20 );
$this -> assertAttributeEquals ( 20 , 'bytesMax' , $fs );
}
public function testCallbackGetNotifyProgress ()
{
$io = $this -> getMock ( 'Composer\IO\IOInterface' );
$io
-> expects ( $this -> once ())
-> method ( 'overwrite' )
;
$fs = new RemoteFilesystem ( $io );
$this -> setAttribute ( $fs , 'bytesMax' , 20 );
$this -> setAttribute ( $fs , 'progress' , true );
$this -> callCallbackGet ( $fs , STREAM_NOTIFY_PROGRESS , 0 , '' , 0 , 10 , 20 );
$this -> assertAttributeEquals ( 50 , 'lastProgress' , $fs );
}
public function testCallbackGetNotifyFailure404 ()
{
$fs = new RemoteFilesystem ( $this -> getMock ( 'Composer\IO\IOInterface' ));
try {
2012-03-10 07:49:21 +00:00
$this -> callCallbackGet ( $fs , STREAM_NOTIFY_FAILURE , 0 , 'HTTP/1.1 404 Not Found' , 404 , 0 , 0 );
2012-03-05 10:26:07 +00:00
$this -> fail ();
} catch ( \Exception $e ) {
2012-03-08 20:59:02 +00:00
$this -> assertInstanceOf ( 'Composer\Downloader\TransportException' , $e );
2012-03-10 07:49:21 +00:00
$this -> assertEquals ( 404 , $e -> getCode ());
$this -> assertContains ( 'HTTP/1.1 404 Not Found' , $e -> getMessage ());
2012-03-05 10:26:07 +00:00
}
}
public function testGetContents ()
{
$fs = new RemoteFilesystem ( $this -> getMock ( 'Composer\IO\IOInterface' ));
2012-03-29 13:09:01 +00:00
$this -> assertContains ( 'testGetContents' , $fs -> getContents ( 'http://example.org' , 'file://' . __FILE__ ));
2012-03-05 10:26:07 +00:00
}
public function testCopy ()
{
$fs = new RemoteFilesystem ( $this -> getMock ( 'Composer\IO\IOInterface' ));
$file = tempnam ( sys_get_temp_dir (), 'c' );
2012-03-29 13:09:01 +00:00
$this -> assertTrue ( $fs -> copy ( 'http://example.org' , 'file://' . __FILE__ , $file ));
2012-03-05 10:26:07 +00:00
$this -> assertFileExists ( $file );
2012-03-29 13:09:01 +00:00
$this -> assertContains ( 'testCopy' , file_get_contents ( $file ));
2012-03-05 10:26:07 +00:00
unlink ( $file );
}
2012-10-03 16:51:42 +00:00
protected function callGetOptionsForUrl ( $io , array $args = array (), array $options = array ())
2012-03-05 10:26:07 +00:00
{
2012-10-03 13:09:47 +00:00
$fs = new RemoteFilesystem ( $io , $options );
2012-03-05 10:26:07 +00:00
$ref = new \ReflectionMethod ( $fs , 'getOptionsForUrl' );
$ref -> setAccessible ( true );
return $ref -> invokeArgs ( $fs , $args );
}
protected function callCallbackGet ( RemoteFilesystem $fs , $notificationCode , $severity , $message , $messageCode , $bytesTransferred , $bytesMax )
{
$ref = new \ReflectionMethod ( $fs , 'callbackGet' );
$ref -> setAccessible ( true );
$ref -> invoke ( $fs , $notificationCode , $severity , $message , $messageCode , $bytesTransferred , $bytesMax );
}
protected function setAttribute ( $object , $attribute , $value )
{
$attr = new \ReflectionProperty ( $object , $attribute );
$attr -> setAccessible ( true );
$attr -> setValue ( $object , $value );
}
}