Skip to content

Instantly share code, notes, and snippets.

@cgarvis
Created February 8, 2012 21:33
Show Gist options
  • Save cgarvis/1774053 to your computer and use it in GitHub Desktop.
Save cgarvis/1774053 to your computer and use it in GitHub Desktop.
diff --git a/tests/cases/net/socket/CurlTest.php b/tests/cases/net/socket/CurlTest.php
index 21a9dce..f874974 100644
--- a/tests/cases/net/socket/CurlTest.php
+++ b/tests/cases/net/socket/CurlTest.php
@@ -138,6 +138,20 @@ class CurlTest extends \lithium\test\Unit {
$stream->set('DummyFlag', 'Changed Dummy Value');
$this->assertEqual('Changed Dummy Value', $stream->options['DummyFlag']);
}
+
+ public function testSendPostThenGet() {
+ $postConfig = array('method' => 'POST', 'body' => '{"body"}');
+ $stream = new Curl($this->_testConfig);
+ $this->assertTrue(is_resource($stream->open()));
+ $this->assertTrue($stream->write(new Request($postConfig + $this->_testConfig)));
+ $this->assertTrue(isset($stream->options[CURLOPT_POST]));
+ $this->assertTrue($stream->close());
+
+ $this->assertTrue(is_resource($stream->open()));
+ $this->assertTrue($stream->write(new Request($this->_testConfig)));
+ $this->assertFalse(isset($stream->options[CURLOPT_POST]));
+ $this->assertTrue($stream->close());
+ }
}
-?>
\ No newline at end of file
+?>
<?php
namespace arwen\extensions\adapter\data\source\http;
use \lithium\util\String;
class Arwen extends \lithium\data\source\http {
/**
* The set of array keys which will be auto-populated in the object's protected properties from
* constructor parameters.
*
* @var array
*/
protected $_autoConfig = array('classes' => 'merge', 'methods' => 'merge');
/**
* Class dependencies.
*/
protected $_classes = array(
'service' => 'lithium\net\http\Service',
'entity' => 'lithium\data\entity\Document',
'set' => 'lithium\data\collection\DocumentSet',
'array' => 'lithium\data\collection\DocumentArray'
);
/**
* List of methods and their corresponding HTTP method and path.
*
* @var array
*/
protected $_methods = array(
'read' => array('method' => 'get', 'path' => "/api/{:source}"),
'create' => array('method' => 'post', 'path' => "/api/{:source}"),
'load' => array('method' => 'get', 'path' => "/api/{:source}/{:id}"),
'update' => array('method' => 'put', 'path' => "/api/{:source}/{:id}"),
'delete' => array('method' => 'delete', 'path' => "/api/{:source}/{:id}")
);
/**
* Constructor
* @param array $config
*/
public function __construct(array $config = array()) {
$defaults = array(
'adapter' => 'Arwen',
'auth' => 'Basic',
'headers' => array('Accept' => 'application/json'),
'type' => 'xml-json',
'scheme' => 'http',
'socket' => 'Curl',
'port' => 80
);
parent::__construct($config + $defaults);
}
/**
* Ensures that the server connection is closed and resources are freed when the adapter
* instance is destroyed.
*
* @return void
*/
public function __destruct() {
if(!$this->_isConnected) {
return;
}
$this->disconnect();
$this->_db = false;
unset($this->connection);
}
protected function _init() {
parent::_init();
// $this->connection->connection->set(CURLOPT_HTTPPROXYTUNNEL, true);
$this->connection->connection->set(CURLOPT_PROXYPORT, '8080');
$this->connection->connection->set(CURLOPT_PROXY, '127.0.0.1');
}
public function cast($entity, array $data, array $options = array()) {
foreach($data as $key => $val) {
if(!is_array($val)) {
continue;
}
$class = 'entity';
$model = $entity->model();
$data[$key] = $this->item($model, $val, compact('class'));
}
return parent::cast($entity, $data, $options);
}
public function create($query, array $options = array()) {
$this->connection->connection->set(CURLOPT_FOLLOWLOCATION, true);
$this->connection->connection->set(CURLOPT_HEADER, false);
$this->connection->connection->set(CURLOPT_UNRESTRICTED_AUTH, true);
$options += array('type' => 'xml-json');
$result = parent::create($query, $options);
$result = is_string($result) ? json_decode($result, true) : $result;
if (isset($result[0]['id'])) {
$query->entity()->sync($result[0]['id'], $result[0]);
return true;
}
return false;
}
/**
* Returns an array of object types accessible through this database.
*
* @param object $class
* @return void
*/
public function sources($class = null) {
return array(
'persons',
'children',
'addresses',
'person_history_entries',
'changedmail_reporting',
'roles',
'person_roles',
'extended_profile_attributes',
'alternate_user_names',
'income_levels',
'newsletters',
'newsletter_subscriptions'
);
}
/**
* Read from document.
*
* @param string $query
* @param array $options
* @return object
* @filter
*/
public function read($query, array $options = array()) {
$type = 'read';
$conditions = $query->conditions();
if(array_key_exists('id', $conditions)) {
$options['id'] = $conditions['id'];
$query->conditions(array('id' => null));
$type = 'load';
}
$params = compact('query', 'options');
$conn =& $this->connection;
if (!isset($this->_methods[$type])) {
return null;
}
$method = $this->_methods[$type];
$filter = function($self, $params) use (&$conn, $method) {
$query = $params['query'];
$options = $params['options'];
$data = array();
$defaults = array('conditions' => null, 'limit' => null);
if ($query) {
$options += array_filter($query->export($self), function($v) {
return $v !== null;
});
$options += $defaults;
$data = (array) $options['conditions'] + (array) $options['limit'];
}
$path = String::insert($method['path'], $options, array('clean' => true));
return $conn->{$method['method']}($path, $data, $options);
};
$result = $this->_filter(__METHOD__, $params, $filter);
$result = (array) json_decode($result, true);
$opts = array('class' => 'set', 'exists' => true);
return $this->item($query->model(), $result, $opts);
}
}
<?php
namespace arwen\tests\intergration\data;
use arwen\models\People;
use lithium\data\Connections;
class ArwenTest extends \lithium\test\Integration {
protected $_person;
public function skip() {
$this->skipIf(false, 'Run the damn test');
}
public function testCreate() {
$random = base_convert(rand(10e16, 10e20), 10, 36);
$username = 'test-'.$random;
$new = People::create(array(
'username' => $username,
'primary_email' => $username.'@ivillage.com'
));
$expected = array(
'username' => $username,
'primary_email' => $username.'@ivillage.com',
'context' => 0
);
$result = $new->data();
$this->assertEqual($expected, $result);
$this->assertEqual(
array(false, true, true),
array($new->exists(), $new->save(), $new->exists())
);
$this->assertTrue((int)$new->id == $new->id);
$this->_person = $new;
}
public function testRead() {
$username = $this->_person->username;
$conditions = array('username' => $username);
$existing = People::first(compact('conditions'));
$this->assertEqual($username, $existing->username);
$this->assertEqual($username.'@ivillage.com', $existing->primary_email);
$this->assertTrue($existing->exists());
}
public function testUpdate() {
$existing = $this->_person;
$random = base_convert(rand(10e16, 10e20), 10, 36);
$newusername = 'test-'.$random;
$existing->username = $newusername;
$result = $existing->save();
$this->assertTrue($result);
$updated = People::firstByUsername($newusername);
$this->assertEqual($existing->id, $updated->id);
$this->_person = $updated;
}
public function testDelete() {
$username = $this->_person->username;
$this->assertTrue($this->_person->delete());
$this->assertTrue(count(People::findByUsername($username)) === 0);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment