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(); ...@@ -5,7 +5,6 @@ htmlHeader();
// create a client instance // create a client instance
$client = new Solarium\Client($config); $client = new Solarium\Client($config);
$client->setAdapter('Solarium\Core\Client\Adapter\ZendHttp');
// get an extract query instance and add settings // get an extract query instance and add settings
$query = $client->createExtract(); $query = $client->createExtract();
...@@ -17,7 +16,7 @@ $query->setOmitHeader(false); ...@@ -17,7 +16,7 @@ $query->setOmitHeader(false);
// add document // add document
$doc = $query->createDocument(); $doc = $query->createDocument();
$doc->id = time(); $doc->id = 'extract-test';
$doc->some = 'more fields'; $doc->some = 'more fields';
$query->setDocument($doc); $query->setDocument($doc);
......
...@@ -168,7 +168,11 @@ class Curl extends Configurable implements AdapterInterface ...@@ -168,7 +168,11 @@ class Curl extends Configurable implements AdapterInterface
if ($method == Request::METHOD_POST) { if ($method == Request::METHOD_POST) {
curl_setopt($handler, CURLOPT_POST, true); 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); $httpResponse = curl_exec($handler);
} elseif ($method == Request::METHOD_GET) { } elseif ($method == Request::METHOD_GET) {
curl_setopt($handler, CURLOPT_HTTPGET, true); curl_setopt($handler, CURLOPT_HTTPGET, true);
......
...@@ -105,16 +105,26 @@ class Http extends Configurable implements AdapterInterface ...@@ -105,16 +105,26 @@ class Http extends Configurable implements AdapterInterface
); );
if ($method == Request::METHOD_POST) { if ($method == Request::METHOD_POST) {
$data = $request->getRawData(); if ($request->getFileUpload()) {
if (null !== $data) {
stream_context_set_option( stream_context_set_option(
$context, $context,
'http', 'http',
'content', 'content',
$data file_get_contents($request->getFileUpload())
); );
$request->addHeader('Content-Type: multipart/form-data');
$request->addHeader('Content-Type: text/xml; charset=UTF-8'); } 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 ...@@ -161,9 +161,17 @@ class PeclHttp extends Configurable implements AdapterInterface
break; break;
case Request::METHOD_POST: case Request::METHOD_POST:
$method = HTTP_METH_POST; $method = HTTP_METH_POST;
$httpRequest->setBody($request->getRawData()); if ($request->getFileUpload()) {
if (!isset($headers['Content-Type'])) { $httpRequest->addPostFile(
$headers['Content-Type'] = 'text/xml; charset=utf-8'; '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; break;
case Request::METHOD_HEAD: case Request::METHOD_HEAD:
......
...@@ -260,12 +260,12 @@ class ZendHttp extends Configurable implements AdapterInterface ...@@ -260,12 +260,12 @@ class ZendHttp extends Configurable implements AdapterInterface
protected function prepareFileUpload($client, $request) protected function prepareFileUpload($client, $request)
{ {
$filename = $request->getFileUpload(); $filename = $request->getFileUpload();
$client->setFileUpload(
if (($content = @file_get_contents($filename)) === false) { 'content',
throw new RuntimeException("Unable to read file '{$filename}' for upload"); 'content',
} file_get_contents($filename),
'application/octet-stream; charset=binary'
$client->setFileUpload('content', 'content', $content, 'application/octet-stream; charset=binary'); );
// set query params as "multipart/form-data" fields // set query params as "multipart/form-data" fields
foreach ($request->getParams() as $name => $value) { foreach ($request->getParams() as $name => $value) {
......
...@@ -41,6 +41,7 @@ use Solarium\QueryType\Update\Query\Query as UpdateQuery; ...@@ -41,6 +41,7 @@ use Solarium\QueryType\Update\Query\Query as UpdateQuery;
use Solarium\QueryType\Analysis\Query\Field as AnalysisQueryField; use Solarium\QueryType\Analysis\Query\Field as AnalysisQueryField;
use Solarium\QueryType\Terms\Query as TermsQuery; use Solarium\QueryType\Terms\Query as TermsQuery;
use Solarium\QueryType\Suggester\Query as SuggesterQuery; 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\Client\Adapter\Http as ClientAdapterHttp;
use Solarium\Core\Plugin; use Solarium\Core\Plugin;
...@@ -895,6 +896,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase ...@@ -895,6 +896,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$observer->suggester($query); $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() public function testCreateQuery()
{ {
$options = array('optionA' => 1, 'optionB' => 2); $options = array('optionA' => 1, 'optionB' => 2);
...@@ -1070,6 +1083,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase ...@@ -1070,6 +1083,18 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$observer->createSuggester($options); $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() public function testTriggerEvent()
{ {
$eventName = 'Test'; $eventName = 'Test';
......
...@@ -63,6 +63,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase ...@@ -63,6 +63,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
'username' => 'testuser', 'username' => 'testuser',
'password' => 'testpass', 'password' => 'testpass',
), ),
'file' => __FILE__,
); );
$this->request->setOptions($options); $this->request->setOptions($options);
...@@ -101,6 +102,11 @@ class RequestTest extends \PHPUnit_Framework_TestCase ...@@ -101,6 +102,11 @@ class RequestTest extends \PHPUnit_Framework_TestCase
), ),
$this->request->getAuthentication() $this->request->getAuthentication()
); );
$this->assertEquals(
$options['file'],
$this->request->getFileUpload()
);
} }
public function testGetDefaultMethod() public function testGetDefaultMethod()
...@@ -510,4 +516,19 @@ file upload: ...@@ -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 ...@@ -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() public function testGetInvalidFieldBoost()
{ {
$this->assertEquals( $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