Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Aug 6, 2016
1 parent 4209c00 commit d75807b
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 41 deletions.
27 changes: 25 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ protected function doRequestAsync($request)
$ch = $this->createCurl($request);
$content = (yield $ch);
yield CoInterface::RETURN_WITH => $this->processResult($content, $ch);
// @codeCoverageIgnoreStart
}
// @codeCoverageIgnoreEnd

protected function createCurl($request)
{
Expand All @@ -98,7 +100,7 @@ protected function createCurl($request)
}
$params = $request->getParameters();
$files = $request->getFiles();
if (!$files) {
if (!$files && !self::containsCURLFile($params)) {
$content = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
return $ch;
Expand Down Expand Up @@ -143,6 +145,9 @@ protected function createHeaders($request)
continue;
}
}
if ($request->getContent() !== null && !preg_grep('/^content-type:/', $headers)) {
$headers[] = 'content-type: application/octet-stream';
}
return $headers;
}

Expand Down Expand Up @@ -170,12 +175,14 @@ protected function addMultipartFiles(array $files, array &$multipart, $arrayName
$multipart[$name] = new \CURLFile($info);
continue;
}
if (!isset($info['tmp_name'])) {
if (!isset($info['tmp_name']) || is_array($info['tmp_name'])) {
$this->addMultipartFiles($info, $multipart, $name);
continue;
}
if ('' === $info['tmp_name']) {
// @codeCoverageIgnoreStart
continue;
// @codeCoverageIgnoreEnd
}
$multipart[$name] = new \CURLFile(
$info['tmp_name'],
Expand All @@ -198,4 +205,20 @@ protected function addMultipartFields(array $params, array &$multipart, $arrayNa
$this->addMultipartFields($value, $multipart, $name);
}
}

protected static function containsCURLFile(array $array)
{
foreach ($array as $item) {
if (is_array($item)) {
if (self::containsCURLFile($item)) {
return true;
}
continue;
}
if ($item instanceof \CURLFile) {
return true;
}
}
return false;
}
}
9 changes: 9 additions & 0 deletions tests/assets/www/json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="json"><?php

$rawpost = file_get_contents('php://input');
$rawquery = filter_input(INPUT_SERVER, 'QUERY_STRING');
$rawcookie = filter_input(INPUT_SERVER, 'HTTP_COOKIE');

echo json_encode(compact('_GET', '_POST', '_COOKIE', 'rawpost', 'rawquery', 'rawcookie'));

?></div>
11 changes: 11 additions & 0 deletions tests/assets/www/upload_form01.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file[x][y]">
<input type="submit" value="submit">
</form>
<?php if (isset($_FILES['file']['error']['x']['y']) && is_int($_FILES['file']['error']['x']['y'])): ?>
<?php if ($_FILES['file']['error']['x']['y'] === UPLOAD_ERR_OK): ?>
<div id="success">SUCCESS</div>
<?php else: ?>
<div id="error">ERROR</div>
<?php endif; ?>
<?php endif; ?>
11 changes: 11 additions & 0 deletions tests/assets/www/upload_form02.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="file[tmp_name][y]">
<input type="submit" value="submit">
</form>
<?php if (isset($_FILES['file']['error']['tmp_name']['y']) && is_int($_FILES['file']['error']['tmp_name']['y'])): ?>
<?php if ($_FILES['file']['error']['tmp_name']['y'] === UPLOAD_ERR_OK): ?>
<div id="success">SUCCESS</div>
<?php else: ?>
<div id="error">ERROR</div>
<?php endif; ?>
<?php endif; ?>
25 changes: 25 additions & 0 deletions tests/unit/ClientConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use mpyw\Coutte\Client;
use mpyw\Co\Co;
use mpyw\Co\CoInterface;
use mpyw\Privator\Proxy;
use mpyw\Privator\ProxyException;

/**
* @requires PHP 7.0
*/
class ClientConfigTest extends \Codeception\TestCase\Test {

use \Codeception\Specify;

public function testCurlOptionsConfig()
{
$client = new Client($expected = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
]);
$actual = array_intersect_key($client->getCurlOptions(), $expected);
$this->assertEquals($expected, $actual);
}
}
51 changes: 51 additions & 0 deletions tests/unit/ClientGetAndPostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use mpyw\Coutte\Client;
use mpyw\Co\Co;
use mpyw\Co\CoInterface;
use mpyw\Co\CURLException;
use mpyw\Privator\Proxy;
use mpyw\Privator\ProxyException;

/**
* @requires PHP 7.0
*/
class ClientGetAndPostTest extends \Codeception\TestCase\Test {

use \Codeception\Specify;

public function getClient()
{
return new Client([
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
]);
}

public function testInvalid()
{
$this->setExpectedException(CURLException::class);
$client = $this->getClient();
$client->request('GET', 'invalid');
}

public function testNormalPost()
{
$client = $this->getClient();
$expected = ['a' => 'b'];
$crawler = $client->request('POST', 'http://localhost:8080/json.php', $expected);
$json = json_decode($crawler->filter('.json')->text(), true);
$this->assertEquals($expected, $json['_POST']);
}

public function testRawPost()
{
$client = $this->getClient();
$expected = 'abcde';
$crawler = $client->request('POST', 'http://localhost:8080/json.php', [], [], [], $expected);
$json = json_decode($crawler->filter('.json')->text(), true);
$this->assertEquals([], $json['_POST']);
$this->assertEquals($expected, $json['rawpost']);
}

}
56 changes: 56 additions & 0 deletions tests/unit/ClientPayloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use mpyw\Coutte\Client;
use mpyw\Co\Co;
use mpyw\Co\CoInterface;
use mpyw\Privator\Proxy;
use mpyw\Privator\ProxyException;

/**
* @requires PHP 7.0
*/
class ClientPayloadTest extends \Codeception\TestCase\Test {

use \Codeception\Specify;

public function getClient()
{
return new Client([
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
]);
}

public function testPayloadHttp()
{
$client = $this->getClient();
$client->request('GET', 'http://localhost:8080/hello.php');
$this->assertEquals('Hello', $client->getResponse()->getContent());
}

public function testPayloadHttps()
{
$client = $this->getClient();
$client->request('GET', 'https://localhost:8081/hello.php');
$this->assertEquals('Hello', $client->getResponse()->getContent());
}

public function testAsyncPayloadHttp()
{
Co::wait(function () {
$client = $this->getClient();
yield $client->requestAsync('GET', 'http://localhost:8080/hello.php');
$this->assertEquals('Hello', $client->getResponse()->getContent());
});
}

public function testAsyncPayloadHttps()
{
Co::wait(function () {
$client = $this->getClient();
yield $client->requestAsync('GET', 'https://localhost:8081/hello.php');
$this->assertEquals('Hello', $client->getResponse()->getContent());
});
}

}
39 changes: 0 additions & 39 deletions tests/unit/ClientTest.php

This file was deleted.

106 changes: 106 additions & 0 deletions tests/unit/ClientUploadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

use mpyw\Coutte\Client;
use mpyw\Co\Co;
use mpyw\Co\CoInterface;
use mpyw\Privator\Proxy;
use mpyw\Privator\ProxyException;

/**
* @requires PHP 7.0
*/
class ClientUploadTest extends \Codeception\TestCase\Test {

use \Codeception\Specify;

public function getClient()
{
return new Client([
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
]);
}

public function testUpload01()
{
$client = $this->getClient();
$crawler = $client->request('GET', 'http://localhost:8080/upload_form01.php');
$form = $crawler->filter('form')->form([
'file[x][y]' => __FILE__,
]);
$crawler = $client->submit($form);
$this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
}

public function testUpload02()
{
$client = $this->getClient();
$crawler = $client->request('GET', 'http://localhost:8080/upload_form02.php');
$form = $crawler->filter('form')->form([
'file[tmp_name][y]' => __FILE__,
]);
$crawler = $client->submit($form);
$this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
}

public function testUpload03()
{
$this->setExpectedException(
PHPUnit_Framework_Exception::class,
'is_readable() expects parameter 1 to be a valid path, object given'
);
$client = $this->getClient();
$crawler = $client->request('GET', 'http://localhost:8080/upload_form01.php');
$form = $crawler->filter('form')->form([
'file[x][y]' => new CURLFile(__FILE__),
]);
}

public function testUpload04()
{
$client = $this->getClient();
$crawler = $client->request(
'POST',
'http://localhost:8080/upload_form01.php',
[],
[
'file[x]' => [
'y' => new CURLFile(__FILE__)
]
]
);
$this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
}

public function testUpload05()
{
$client = $this->getClient();
$crawler = $client->request(
'POST',
'http://localhost:8080/upload_form01.php',
[],
[
'file[x]' => [
'y' => __FILE__,
]
]
);
$this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
}

public function testUpload06()
{
$client = $this->getClient();
$crawler = $client->request(
'POST',
'http://localhost:8080/upload_form01.php',
[
[],
'file[x]' => [
'y' => new CURLFile(__FILE__)
]
]
);
$this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
}
}

0 comments on commit d75807b

Please sign in to comment.