Skip to content

Commit 8220148

Browse files
committed
Throw exceptions instead of using assertions
1 parent 2ad65db commit 8220148

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
### Fixed
6+
7+
- #36: Failure evaluating code: is_resource($handle) (string assertions are deprecated in PHP 7.2)
58

69
## 1.7 - 2017-02-09
710

src/PromiseCore.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,34 @@ class PromiseCore
6767
/**
6868
* Create shared core.
6969
*
70-
* @param RequestInterface $request HTTP request
71-
* @param resource $handle cURL handle
72-
* @param ResponseBuilder $responseBuilder
70+
* @param RequestInterface $request HTTP request.
71+
* @param resource $handle cURL handle.
72+
* @param ResponseBuilder $responseBuilder Response builder.
73+
*
74+
* @throws \InvalidArgumentException If $handle is not a cURL resource.
7375
*/
7476
public function __construct(
7577
RequestInterface $request,
7678
$handle,
7779
ResponseBuilder $responseBuilder
7880
) {
79-
assert(is_resource($handle));
80-
assert(get_resource_type($handle) === "curl");
81+
if (!is_resource($handle)) {
82+
throw new \InvalidArgumentException(
83+
sprintf(
84+
'Parameter $handle expected to be a cURL resource, %s given',
85+
gettype($handle)
86+
)
87+
);
88+
}
89+
90+
if (get_resource_type($handle) !== 'curl') {
91+
throw new \InvalidArgumentException(
92+
sprintf(
93+
'Parameter $handle expected to be a cURL resource, %s resource given',
94+
get_resource_type($handle)
95+
)
96+
);
97+
}
8198

8299
$this->request = $request;
83100
$this->handle = $handle;

tests/PromiseCoreTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@
1616
*/
1717
class PromiseCoreTest extends BaseUnitTestCase
1818
{
19+
/**
20+
* Testing if handle is not a resource.
21+
*/
22+
public function testHandleIsNotAResource()
23+
{
24+
$this->setExpectedException(
25+
\InvalidArgumentException::class,
26+
'Parameter $handle expected to be a cURL resource, NULL given'
27+
);
28+
29+
new PromiseCore(
30+
$this->createRequest('GET', '/'),
31+
null,
32+
new ResponseBuilder($this->createResponse())
33+
);
34+
}
35+
36+
/**
37+
* Testing if handle is not a cURL resource.
38+
*/
39+
public function testHandleIsNotACurlResource()
40+
{
41+
$this->setExpectedException(
42+
\InvalidArgumentException::class,
43+
'Parameter $handle expected to be a cURL resource, stream resource given'
44+
);
45+
46+
new PromiseCore(
47+
$this->createRequest('GET', '/'),
48+
fopen('php://memory', 'r+b'),
49+
new ResponseBuilder($this->createResponse())
50+
);
51+
}
52+
1953
/**
2054
* Test on fulfill actions.
2155
*/

0 commit comments

Comments
 (0)