1 | <?php |
||
2 | |||
3 | use PHPUnit\Framework\TestCase; |
||
4 | use tinymeng\tools\HttpRequest; |
||
0 ignored issues
–
show
|
|||
5 | |||
6 | class HttpRequestTest extends TestCase |
||
7 | { |
||
8 | public function testHttpPost() |
||
9 | { |
||
10 | // 模拟一个简单的 POST 请求 |
||
11 | $url = 'http://example.com/api'; |
||
12 | $param = ['key' => 'value']; |
||
13 | $httpHeaders = ['Content-Type: application/json']; |
||
14 | $proxy = '127.0.0.1:8080'; |
||
15 | $http_code = 200; |
||
16 | |||
17 | // 使用 Mockery 或其他工具来模拟 HTTP 请求 |
||
18 | // 这里假设我们有一个模拟的响应内容 |
||
19 | $mockResponse = '{"status": "success", "data": {"key": "value"}}'; |
||
20 | |||
21 | // 使用 Mockery 来模拟 curl_exec 的返回值 |
||
22 | $curlMock = \Mockery::mock('alias:curl_init'); |
||
0 ignored issues
–
show
The type
Mockery was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
23 | $curlMock->shouldReceive('curl_setopt_array')->andReturn(true); |
||
24 | $curlMock->shouldReceive('curl_exec')->andReturn($mockResponse); |
||
25 | $curlMock->shouldReceive('curl_getinfo')->andReturn(['http_code' => $http_code]); |
||
26 | $curlMock->shouldReceive('curl_close')->andReturn(true); |
||
27 | |||
28 | $response = HttpRequest::httpPost($url, $param, $httpHeaders, $proxy, $http_code); |
||
29 | $this->assertEquals($mockResponse, $response); |
||
30 | } |
||
31 | |||
32 | public function testHttpGet() |
||
33 | { |
||
34 | // 模拟一个简单的 GET 请求 |
||
35 | $url = 'http://example.com/api'; |
||
36 | $param = ['key' => 'value']; |
||
37 | $httpHeaders = ['Content-Type: application/json']; |
||
38 | $proxy = '127.0.0.1:8080'; |
||
39 | $http_code = 200; |
||
40 | |||
41 | // 使用 Mockery 或其他工具来模拟 HTTP 请求 |
||
42 | // 这里假设我们有一个模拟的响应内容 |
||
43 | $mockResponse = '{"status": "success", "data": {"key": "value"}}'; |
||
44 | |||
45 | // 使用 Mockery 来模拟 curl_exec 的返回值 |
||
46 | $curlMock = \Mockery::mock('alias:curl_init'); |
||
47 | $curlMock->shouldReceive('curl_setopt_array')->andReturn(true); |
||
48 | $curlMock->shouldReceive('curl_exec')->andReturn($mockResponse); |
||
49 | $curlMock->shouldReceive('curl_getinfo')->andReturn(['http_code' => $http_code]); |
||
50 | $curlMock->shouldReceive('curl_close')->andReturn(true); |
||
51 | |||
52 | $response = HttpRequest::httpGet($url, $param, $httpHeaders, $proxy, $http_code); |
||
53 | $this->assertEquals($mockResponse, $response); |
||
54 | } |
||
55 | } |
||
56 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: