Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
1of0
php
curly
Commits
2136d8e4
Commit
2136d8e4
authored
Aug 22, 2016
by
TheBigB
Browse files
Increasing test coverage
parent
62851167
Pipeline
#20752967
failed with stage
in 1 minute and 39 seconds
Changes
11
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
127 additions
and
141 deletions
+127
-141
README.md
README.md
+3
-0
phpunit.xml
phpunit.xml
+8
-0
src/Curly.php
src/Curly.php
+7
-2
src/ExtendedServerRequest.php
src/ExtendedServerRequest.php
+1
-133
src/Handlers/StreamHandler.php
src/Handlers/StreamHandler.php
+7
-1
tests/AbstractTestCase.php
tests/AbstractTestCase.php
+0
-1
tests/GenericTest.php
tests/GenericTest.php
+0
-1
tests/HandlerTest.php
tests/HandlerTest.php
+1
-1
tests/HeaderForwardTest.php
tests/HeaderForwardTest.php
+38
-0
tests/InputStreamTest.php
tests/InputStreamTest.php
+60
-0
tests/OutputStreamTest.php
tests/OutputStreamTest.php
+2
-2
No files found.
README.md
View file @
2136d8e4
[

](https://gitlab.com/1of0/php-curly/commits/develop)
[

](https://gitlab.com/1of0/php-curly/commits/develop)
# Curly
Curly is an object oriented wrapper around PHP's cURL extension.
...
...
phpunit.xml
View file @
2136d8e4
...
...
@@ -4,4 +4,12 @@
<directory>
tests
</directory>
</testsuite>
</testsuites>
<filter>
<whitelist
processUncoveredFilesFromWhitelist=
"true"
>
<directory
suffix=
".php"
>
src
</directory>
<exclude>
<directory>
src/Exceptions
</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
src/Curly.php
View file @
2136d8e4
...
...
@@ -95,6 +95,11 @@ class Curly implements HttpClientInterface
curl_exec
(
$channel
);
if
(
$responseStream
!==
null
&&
$responseStream
->
isSeekable
())
{
$responseStream
->
rewind
();
}
$status
=
curl_getinfo
(
$channel
,
CURLINFO_HTTP_CODE
);
return
new
Response
(
$responseStream
?:
'php://temp'
,
$status
,
$this
->
parseHeaderStream
(
$headerStream
));
}
...
...
@@ -115,7 +120,7 @@ class Curly implements HttpClientInterface
if
(
$options
->
outputHeaderStream
===
null
)
{
$stream
=
fopen
(
'php://temp'
,
'r
w
b'
);
$stream
=
fopen
(
'php://temp'
,
'r
+
b'
);
$options
->
outputHeaderStream
=
$stream
;
return
new
Stream
(
$stream
);
}
...
...
@@ -141,7 +146,7 @@ class Curly implements HttpClientInterface
if
(
$options
->
outputStream
===
null
)
{
$stream
=
fopen
(
'php://temp'
,
'r
w
b'
);
$stream
=
fopen
(
'php://temp'
,
'r
+
b'
);
$options
->
outputStream
=
$stream
;
return
new
Stream
(
$stream
);
}
...
...
src/ExtendedServerRequest.php
View file @
2136d8e4
...
...
@@ -2,8 +2,6 @@
namespace
OneOfZero\Curly
;
use
Psr\Http\Message\StreamInterface
;
use
Psr\Http\Message\UriInterface
;
use
Zend\Diactoros\ServerRequest
;
use
Zend\Diactoros\Uri
;
...
...
@@ -33,7 +31,7 @@ class ExtendedServerRequest extends ServerRequest
*/
public
function
withStringBody
(
$content
)
{
$stream
=
new
SharedStream
(
'php://temp'
,
'r
w
b'
);
$stream
=
new
SharedStream
(
'php://temp'
,
'r
+
b'
);
BinarySafe
::
write
(
$stream
,
$content
);
$stream
->
rewind
();
...
...
@@ -112,134 +110,4 @@ class ExtendedServerRequest extends ServerRequest
->
withStringBody
(
$buffer
)
;
}
#region // What follows is irrelevant proxying to fix the return-type hints
/**
* {@inheritdoc}
* @return static
*/
public
function
withProtocolVersion
(
$version
)
{
return
parent
::
withProtocolVersion
(
$version
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withHeader
(
$header
,
$value
)
{
return
parent
::
withHeader
(
$header
,
$value
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withAddedHeader
(
$header
,
$value
)
{
return
parent
::
withAddedHeader
(
$header
,
$value
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withoutHeader
(
$header
)
{
return
parent
::
withoutHeader
(
$header
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withBody
(
StreamInterface
$body
)
{
return
parent
::
withBody
(
$body
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withRequestTarget
(
$requestTarget
)
{
return
parent
::
withRequestTarget
(
$requestTarget
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withUri
(
UriInterface
$uri
,
$preserveHost
=
false
)
{
return
parent
::
withUri
(
$uri
,
$preserveHost
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withUploadedFiles
(
array
$uploadedFiles
)
{
return
parent
::
withUploadedFiles
(
$uploadedFiles
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withCookieParams
(
array
$cookies
)
{
return
parent
::
withCookieParams
(
$cookies
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withQueryParams
(
array
$query
)
{
return
parent
::
withQueryParams
(
$query
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withParsedBody
(
$data
)
{
return
parent
::
withParsedBody
(
$data
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withAttribute
(
$attribute
,
$value
)
{
return
parent
::
withAttribute
(
$attribute
,
$value
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withoutAttribute
(
$attribute
)
{
return
parent
::
withoutAttribute
(
$attribute
);
}
/**
* {@inheritdoc}
* @return static
*/
public
function
withMethod
(
$method
)
{
return
parent
::
withMethod
(
$method
);
}
#endregion
}
src/Handlers/StreamHandler.php
View file @
2136d8e4
...
...
@@ -7,6 +7,7 @@ use OneOfZero\Curly\BinarySafe;
use
OneOfZero\Curly\CancellationCallbackInterface
;
use
OneOfZero\Curly\CurlyOptions
;
use
OneOfZero\Streams\SharedStreamInterface
;
use
RuntimeException
;
/**
* Class StreamHandler
...
...
@@ -161,8 +162,13 @@ class StreamHandler extends CancellableHandler
// Trim off line break
list
(
$header
)
=
explode
(
"
\r\n
"
,
$headerData
);
header
(
$header
,
false
);
if
(
headers_sent
())
{
throw
new
RuntimeException
(
'Cannot forward headers because headers were already sent'
);
}
header
(
$header
,
false
);
return
parent
::
onHeader
(
$channel
,
$headerData
);
}
...
...
tests/AbstractTestCase.php
View file @
2136d8e4
...
...
@@ -11,7 +11,6 @@ namespace OneOfZero\Curly\Tests;
use
OneOfZero\Curly\ExtendedServerRequest
;
use
PHPUnit_Framework_TestCase
;
use
Zend\Diactoros\Request
;
use
Zend\Diactoros\Uri
;
abstract
class
AbstractTestCase
extends
PHPUnit_Framework_TestCase
...
...
tests/GenericTest.php
View file @
2136d8e4
...
...
@@ -11,7 +11,6 @@ namespace OneOfZero\Curly\Tests;
use
OneOfZero\Curly\Curly
;
use
OneOfZero\Curly\ExtendedServerRequest
;
use
Zend\Diactoros\Request
;
class
GenericTest
extends
AbstractTestCase
{
...
...
tests/HandlerTest.php
View file @
2136d8e4
...
...
@@ -93,7 +93,7 @@ class HandlerTest extends AbstractTestCase
*/
private
function
createRandomByteStream
()
{
$stream
=
new
SharedStream
(
'php://temp'
,
'r
w
b'
);
$stream
=
new
SharedStream
(
'php://temp'
,
'r
+
b'
);
for
(
$i
=
0
;
$i
<
self
::
BYTES_TO_READ
;
$i
++
)
{
...
...
tests/HeaderForwardTest.php
0 → 100644
View file @
2136d8e4
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace
OneOfZero\Curly\Tests
;
use
OneOfZero\Curly\Curly
;
use
OneOfZero\Curly\Handlers\StreamHandler
;
use
RuntimeException
;
class
HeaderForwardTest
extends
AbstractTestCase
{
/**
* Checks whether output streams work properly when configured through a StreamHandler.
*/
public
function
testHeaderForwarding
()
{
$curly
=
new
Curly
();
$handler
=
new
StreamHandler
();
$handler
->
enableOutputHeaderForwarding
();
$curly
->
setCustomHandler
(
$handler
);
$request
=
$this
->
buildRequest
(
'GET'
,
'get'
);
$this
->
expectException
(
RuntimeException
::
class
);
$response
=
$curly
->
request
(
$request
);
$this
->
assertEquals
(
200
,
$response
->
getStatusCode
());
// TODO: Abstract header() call in StreamHandler class and test the result
}
}
tests/InputStreamTest.php
0 → 100644
View file @
2136d8e4
<?php
/**
* Copyright (c) 2015 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace
OneOfZero\Curly\Tests
;
use
OneOfZero\Curly\Curly
;
use
OneOfZero\Curly\Handlers\StreamHandler
;
use
OneOfZero\Curly\SharedStream
;
class
InputStreamTest
extends
AbstractTestCase
{
const
BYTES_TO_READ
=
1024
;
/**
* Checks whether output streams work properly when configured through a StreamHandler.
*/
public
function
testStreamHandler
()
{
$curly
=
new
Curly
();
$inputStream
=
$this
->
createRandomByteStream
();
$curly
->
setCustomHandler
(
new
StreamHandler
(
$inputStream
));
$request
=
$this
->
buildRequest
(
'POST'
,
'post'
)
->
withAddedHeader
(
'Content-Type'
,
'text/plain'
)
->
withAddedHeader
(
'Content-Length'
,
strval
(
self
::
BYTES_TO_READ
))
;
$response
=
$curly
->
request
(
$request
);
$this
->
assertEquals
(
200
,
$response
->
getStatusCode
());
$responseObject
=
json_decode
(
$response
->
getBody
()
->
getContents
());
$this
->
assertEquals
(
strval
(
$inputStream
),
$responseObject
->
data
);
}
/**
* Creates a stream in memory of BYTES_TO_READ random bytes.
*
* @return SharedStream
*/
private
function
createRandomByteStream
()
{
$stream
=
new
SharedStream
(
'php://temp'
,
'r+b'
);
for
(
$i
=
0
;
$i
<
self
::
BYTES_TO_READ
;
$i
++
)
{
$stream
->
write
(
chr
((
$i
%
26
)
+
97
));
}
$stream
->
rewind
();
return
$stream
;
}
}
tests/OutputStreamTest.php
View file @
2136d8e4
...
...
@@ -40,7 +40,7 @@ class OutputStreamTest extends AbstractTestCase
{
$curly
=
new
Curly
();
$stream
=
new
SharedStream
(
'php://temp'
,
'r
w
b'
);
$stream
=
new
SharedStream
(
'php://temp'
,
'r
+
b'
);
$curly
->
setCustomHandler
(
new
StreamHandler
(
null
,
$stream
));
$request
=
$this
->
buildRequest
(
'GET'
,
'bytes'
,
self
::
BYTES_TO_READ
);
...
...
@@ -57,7 +57,7 @@ class OutputStreamTest extends AbstractTestCase
{
$curly
=
new
Curly
();
$stream
=
new
SharedStream
(
'php://temp'
,
'r
w
b'
);
$stream
=
new
SharedStream
(
'php://temp'
,
'r
+
b'
);
$curly
->
getOptions
()
->
outputStream
=
$stream
->
getResource
();
$request
=
$this
->
buildRequest
(
'GET'
,
'bytes'
,
self
::
BYTES_TO_READ
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment