Commit 892eb74c authored by Bas de Nooijer's avatar Bas de Nooijer

Implemented basic upload support in all client adapters and improved unittests

parent 4dcfb999
......@@ -5,7 +5,6 @@ htmlHeader();
// create a client instance
$client = new Solarium\Client($config);
$client->setAdapter('Solarium\Core\Client\Adapter\ZendHttp');
// get an extract query instance and add settings
$query = $client->createExtract();
......@@ -17,7 +16,7 @@ $query->setOmitHeader(false);
// add document
$doc = $query->createDocument();
$doc->id = time();
$doc->id = 'extract-test';
$doc->some = 'more fields';
$query->setDocument($doc);
......
......@@ -168,7 +168,11 @@ class Curl extends Configurable implements AdapterInterface
if ($method == Request::METHOD_POST) {
curl_setopt($handler, CURLOPT_POST, true);
curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData());
if ($request->getFileUpload()) {
curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => '@'.$request->getFileUpload()));
} else {
curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData());
}
$httpResponse = curl_exec($handler);
} elseif ($method == Request::METHOD_GET) {
curl_setopt($handler, CURLOPT_HTTPGET, true);
......
......@@ -105,16 +105,26 @@ class Http extends Configurable implements AdapterInterface
);
if ($method == Request::METHOD_POST) {
$data = $request->getRawData();
if (null !== $data) {
if ($request->getFileUpload()) {
stream_context_set_option(
$context,
'http',
'content',
$data
file_get_contents($request->getFileUpload())
);
$request->addHeader('Content-Type: text/xml; charset=UTF-8');
$request->addHeader('Content-Type: multipart/form-data');
} else {
$data = $request->getRawData();
if (null !== $data) {
stream_context_set_option(
$context,
'http',
'content',
$data
);
$request->addHeader('Content-Type: text/xml; charset=UTF-8');
}
}
}
......
......@@ -161,9 +161,17 @@ class PeclHttp extends Configurable implements AdapterInterface
break;
case Request::METHOD_POST:
$method = HTTP_METH_POST;
$httpRequest->setBody($request->getRawData());
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'text/xml; charset=utf-8';
if ($request->getFileUpload()) {
$httpRequest->addPostFile(
'content',
$request->getFileUpload(),
'application/octet-stream; charset=binary'
);
} else {
$httpRequest->setBody($request->getRawData());
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'text/xml; charset=utf-8';
}
}
break;
case Request::METHOD_HEAD:
......
......@@ -260,12 +260,12 @@ class ZendHttp extends Configurable implements AdapterInterface
protected function prepareFileUpload($client, $request)
{
$filename = $request->getFileUpload();
if (($content = @file_get_contents($filename)) === false) {
throw new RuntimeException("Unable to read file '{$filename}' for upload");
}
$client->setFileUpload('content', 'content', $content, 'application/octet-stream; charset=binary');
$client->setFileUpload(
'content',
'content',
file_get_contents($filename),
'application/octet-stream; charset=binary'
);
// set query params as "multipart/form-data" fields
foreach ($request->getParams() as $name => $value) {
......
......@@ -41,6 +41,7 @@ use Solarium\QueryType\Update\Query\Query as UpdateQuery;
use Solarium\QueryType\Analysis\Query\Field as AnalysisQueryField;
use Solarium\QueryType\Terms\Query as TermsQuery;
use Solarium\QueryType\Suggester\Query as SuggesterQuery;
use Solarium\QueryType\Extract\Query as ExtractQuery;
use Solarium\Core\Client\Adapter\Http as ClientAdapterHttp;
use Solarium\Core\Plugin;
......@@ -895,6 +896,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$observer->suggester($query);
}
public function testExtract()
{
$query = new ExtractQuery();
$observer = $this->getMock('Solarium\Core\Client\Client', array('execute'));
$observer->expects($this->once())
->method('execute')
->with($this->equalTo($query));
$observer->extract($query);
}
public function testCreateQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
......@@ -1070,6 +1083,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$observer->createSuggester($options);
}
public function testCreateExtract()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium\Core\Client\Client', array('createQuery'));
$observer->expects($this->once())
->method('createQuery')
->with($this->equalTo(Client::QUERY_EXTRACT), $this->equalTo($options));
$observer->createExtract($options);
}
public function testTriggerEvent()
{
$eventName = 'Test';
......
......@@ -63,6 +63,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
'username' => 'testuser',
'password' => 'testpass',
),
'file' => __FILE__,
);
$this->request->setOptions($options);
......@@ -101,6 +102,11 @@ class RequestTest extends \PHPUnit_Framework_TestCase
),
$this->request->getAuthentication()
);
$this->assertEquals(
$options['file'],
$this->request->getFileUpload()
);
}
public function testGetDefaultMethod()
......@@ -510,4 +516,19 @@ file upload:
);
}
public function testSetAndGetFileUpload()
{
$this->request->setFileUpload(__FILE__);
$this->assertEquals(
__FILE__,
$this->request->getFileUpload()
);
}
public function testSetAndGetFileUploadWithInvalidFile()
{
$this->setExpectedException('Solarium\Exception\RuntimeException');
$this->request->setFileUpload('invalid-filename.dummy');
}
}
......@@ -201,6 +201,19 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
);
}
public function testSetAndGetFieldBoosts()
{
$this->doc->setFieldBoost('name',2.5);
$this->doc->setFieldBoost('category',1.5);
$this->assertEquals(
array(
'name' => 2.5,
'category' => 1.5,
),
$this->doc->getFieldBoosts()
);
}
public function testGetInvalidFieldBoost()
{
$this->assertEquals(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment