Skip to content

Commit

Permalink
Turn socket tests into integration tests.
Browse files Browse the repository at this point in the history
Loose dependency on Mocker, functions weren't reliably overloaded
when running whole test suite. Partially reverts
77af1d5.

Fixes #1243
Fixes #1244
  • Loading branch information
mariuswilms committed Dec 29, 2015
1 parent bd25335 commit 581bde2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\cases\net\socket;
namespace lithium\tests\integration\net\socket;

use lithium\net\http\Request;
use lithium\net\socket\Context;
use lithium\test\Mocker;

class ContextTest extends \lithium\test\Unit {
class ContextTest extends \lithium\test\Integration {

protected $_testConfig = array(
'persistent' => false,
'scheme' => 'http',
'host' => 'google.com',
'host' => 'example.org',
'port' => 80,
'timeout' => 4,
'classes' => array(
Expand All @@ -26,55 +25,8 @@ class ContextTest extends \lithium\test\Unit {
)
);

public function setUp() {
$base = 'lithium\net\socket';
$namespace = __NAMESPACE__;
Mocker::overwriteFunction("{$namespace}\\stream_context_get_options", function($resource) {
rewind($resource);
return unserialize(stream_get_contents($resource));
});
Mocker::overwriteFunction("{$base}\\stream_context_create", function($options) {
return $options;
});
Mocker::overwriteFunction("{$base}\\fopen", function($file, $mode, $includePath, $context) {
$handle = fopen("php://memory", "rw");
fputs($handle, serialize($context));
return $handle;
});
Mocker::overwriteFunction("{$base}\\stream_get_meta_data", function($resource) {
return array(
'wrapper_data' => array(
'HTTP/1.1 301 Moved Permanently',
'Location: http://www.google.com/',
'Content-Type: text/html; charset=UTF-8',
'Date: Thu, 28 Feb 2013 07:05:10 GMT',
'Expires: Sat, 30 Mar 2013 07:05:10 GMT',
'Cache-Control: public, max-age=2592000',
'Server: gws',
'Content-Length: 219',
'X-XSS-Protection: 1; mode=block',
'X-Frame-Options: SAMEORIGIN',
'Connection: close',
),
);
});
Mocker::overwriteFunction("{$base}\\stream_get_contents", function($resource) {
return <<<EOD
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
EOD;
});
Mocker::overwriteFunction("{$base}\\feof", function($resource) {
return true;
});
}

public function tearDown() {
Mocker::overwriteFunction(false);
public function skip() {
$this->skipIf(!$this->_hasNetwork(), 'No network connection.');
}

public function testConstruct() {
Expand Down Expand Up @@ -170,12 +122,12 @@ public function testContextAdapter() {
$response = $socket->send();
$this->assertInstanceOf('lithium\net\http\Response', $response);

$expected = 'google.com';
$expected = 'example.org';
$result = $response->host;
$this->assertEqual($expected, $result);

$result = $response->body();
$this->assertPattern("/<title[^>]*>301 Moved<\/title>/im", (string) $result);
$this->assertPattern("/<title[^>]*>Example Domain<\/title>/im", (string) $result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\cases\net\socket;
namespace lithium\tests\integration\net\socket;

use lithium\net\http\Request;
use lithium\net\socket\Curl;
use lithium\test\Mocker;

class CurlTest extends \lithium\test\Unit {
class CurlTest extends \lithium\test\Integration {

protected $_testConfig = array(
'persistent' => false,
'scheme' => 'http',
'host' => 'google.com',
'host' => 'example.org',
'port' => 80,
'timeout' => 2,
'classes' => array(
Expand All @@ -27,53 +26,12 @@ class CurlTest extends \lithium\test\Unit {
);

public function skip() {
$this->skipIf(!$this->_hasNetwork(), 'No network connection.');

$message = 'Your PHP installation was not compiled with curl support.';
$this->skipIf(!function_exists('curl_init'), $message);
}

public function setUp() {
$base = 'lithium\net\socket';
Mocker::overwriteFunction("{$base}\curl_init", function($url) {
return fopen("php://memory", "rw");
});
Mocker::overwriteFunction("{$base}\curl_setopt_array", function($resource, $options) {
return count($options);
});
Mocker::overwriteFunction("{$base}\curl_setopt", function($resource, $key, $value) {
return;
});
Mocker::overwriteFunction("{$base}\curl_close", function(&$resource) {
$resource = null;
return;
});
Mocker::overwriteFunction("{$base}\curl_exec", function($resource) {
return <<<EOD
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 28 Feb 2013 07:05:10 GMT
Expires: Sat, 30 Mar 2013 07:05:10 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Connection: close
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
EOD;
});
}

public function tearDown() {
Mocker::overwriteFunction(false);
}

public function testAllMethodsNoConnection() {
$stream = new Curl(array('scheme' => null));
$this->assertFalse($stream->open());
Expand Down Expand Up @@ -257,12 +215,12 @@ public function testCurlAdapter() {
$response = $socket->send();
$this->assertInstanceOf('lithium\net\http\Response', $response);

$expected = 'google.com';
$expected = 'example.org';
$result = $response->host;
$this->assertEqual($expected, $result);

$result = $response->body();
$this->assertPattern("/<title[^>]*>301 Moved<\/title>/im", (string) $result);
$this->assertPattern("/<title[^>]*>Example Domain<\/title>/im", (string) $result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\cases\net\socket;
namespace lithium\tests\integration\net\socket;

use lithium\net\http\Request;
use lithium\net\socket\Stream;
use lithium\test\Mocker;

class StreamTest extends \lithium\test\Unit {
class StreamTest extends \lithium\test\Integration {

protected $_testConfig = array(
'persistent' => false,
'scheme' => 'http',
'host' => 'google.com',
'host' => 'example.org',
'port' => 80,
'timeout' => 2,
'classes' => array(
Expand All @@ -26,40 +25,8 @@ class StreamTest extends \lithium\test\Unit {
)
);

public function setUp() {
$base = 'lithium\net\socket';
Mocker::overwriteFunction("{$base}\\stream_socket_client", function() {
return fopen("php://memory", "rw");
});
Mocker::overwriteFunction("{$base}\\feof", function($resource) {
return true;
});
Mocker::overwriteFunction("{$base}\stream_get_contents", function($resource) {
return <<<EOD
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Thu, 28 Feb 2013 07:05:10 GMT
Expires: Sat, 30 Mar 2013 07:05:10 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Connection: close
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
EOD;
});
}

public function tearDown() {
Mocker::overwriteFunction(false);
public function skip() {
$this->skipIf(!$this->_hasNetwork(), 'No network connection.');
}

public function testAllMethodsNoConnection() {
Expand Down Expand Up @@ -122,7 +89,7 @@ public function testWriteAndRead() {
$this->assertInternalType('resource', $stream->resource());

$result = $stream->write();
$this->assertEqual(83, $result);
$this->assertEqual(84, $result);
$this->assertPattern("/^HTTP/", (string) $stream->read());
}

Expand Down Expand Up @@ -167,12 +134,12 @@ public function testStreamAdapter() {
$response = $socket->send();
$this->assertInstanceOf('lithium\net\http\Response', $response);

$expected = 'google.com';
$expected = 'example.org';
$result = $response->host;
$this->assertEqual($expected, $result);

$result = $response->body();
$this->assertPattern("/<title[^>]*>301 Moved<\/title>/im", (string) $result);
$this->assertPattern("/<title[^>]*>Example Domain<\/title>/im", (string) $result);
}
}

Expand Down

0 comments on commit 581bde2

Please sign in to comment.