Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Unit to be able to use namespaces within tests; Changed Client/…

…BuzzTest.php to use mocked Client and mocked Adapter; Fixed Bug #18;
  • Loading branch information...
commit 18de9d5066c523594004aa4c9d2122fa0fef9f80 1 parent a16a752
@mikelohmann mikelohmann authored
View
15 lib/Doctrine/Search/Http/Client/BuzzClient.php
@@ -9,20 +9,11 @@ class BuzzClient extends AbstractClient
{
protected $browser;
- public function __construct($host = 'localhost', $port = 80, $username = '', $password = '')
- {
- $this->setBrowser(new Browser());
- parent::__construct($host, $port, $username, $password);
- }
-
- /**
- * You can overwrite the default browser if needed
- *
- * @param Buzz\Browser $browser
- */
- public function setBrowser(Browser $browser)
+ public function __construct(Browser $browser, $host = 'localhost', $port = 80, $username = '', $password = '')
{
$this->browser = $browser;
+
+ parent::__construct($host, $port, $username, $password);
}
/**
View
2  phpunit.xml.dist
@@ -13,7 +13,7 @@
>
<testsuites>
<testsuite name="Doctrine Search Test Suite">
- <directory>./tests/Doctrine/Search</directory>
+ <directory>./tests/Unit/Doctrine/Search</directory>
</testsuite>
</testsuites>
View
75 tests/Unit/Doctrine/Search/ElasticSearch/Tests/ClientTest.php
@@ -0,0 +1,75 @@
+<?php
+namespace Unit\Doctrine\Search\ElasticSearch\Tests;
+use Doctrine\Search\ElasticSearch\Client;
+use Doctrine\Search\ElasticSearch\Connection;
+use Doctrine\Search\Http\Response\BuzzResponse;
+
+class ClientTest extends \PHPUnit_Framework_TestCase
+{
+ private $client;
+
+ private $httpClient;
+
+ private $json;
+
+ private $connection;
+
+ protected function setUp()
+ {
+ $this->httpClient = $this->getMock('Doctrine\\Search\\Http\\Client\\BuzzClient', array(), array(), '', false);
+ $this->json = '{
+ "user": "kimchy",
+ "postDate": "2009-11-15T14:12:12",
+ "message": "Another tweet, will it be indexed?"
+ }';
+
+ $this->client = new Client($this->httpClient);
+ }
+
+ /**
+ *
+ * @expectedException PHPUnit_Framework_Error
+ */
+ public function testFindWrongParameter()
+ {
+ $buzzResponse = $this->getMock('Buzz\Message\Response');
+ $buzzResponse->expects($this->never())
+ ->method('getContent')
+ ->will($this->returnValue($this->json));
+
+ $mockedResponse = new BuzzResponse($buzzResponse);
+
+ $this->httpClient->expects($this->never())
+ ->method('sendRequest')
+ ->will($this->returnValue($mockedResponse));
+
+ $this->client->find(array('query'));
+ }
+
+ /**
+ * @dataProvider getQuery
+ */
+ public function testFind($query)
+ {
+ $buzzResponse = $this->getMock('Buzz\Message\Response');
+ $buzzResponse->expects($this->once())
+ ->method('getContent')
+ ->will($this->returnValue($this->json));
+
+ $mockedResponse = new BuzzResponse($buzzResponse);
+
+ $this->httpClient->expects($this->once())
+ ->method('sendRequest')
+ ->will($this->returnValue($mockedResponse));
+
+ $result = $this->client->find($query);
+ $this->assertEquals(json_decode($this->json), $result);
+ }
+
+ static public function getQuery()
+ {
+ return array(
+ array('http://localhost:9200')
+ );
+ }
+}
View
41 tests/Unit/Doctrine/Search/Http/Adapter/CurlTest.php
@@ -0,0 +1,41 @@
+<?php
+namespace Unit\Doctrine\Search\Http\Adapter;
+use Doctrine\Search\Http\Adapter\Curl;
+
+
+class CurlTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @var Curl
+ */
+ protected $adapter;
+
+ protected function setUp() {
+ if ( ! extension_loaded('curl') ) {
+ $this->markTestSkipped('Extension cURL is not loaded');
+ }
+
+ $this->adapter = new Curl();
+ }
+
+ /**
+ * @dataProvider getTestData
+ */
+ public function testSendData($host, $port, $method, $url, $body, $contains)
+ {
+ $this->adapter->connect($host, $port);
+ $this->adapter->request($method, $url, array(), $body);
+
+ $this->assertContains($contains, $this->adapter->readData(), true);
+ }
+
+ static public function getTestData()
+ {
+ return array(
+ array('http://www.google.de', 80, 'GET', '/', '', '<html'),
+ array('http://google.de', 80, 'GET', '/', '', '301'),
+ array('http://www.php.net', 80, 'GET', '/', '', 'php'),
+ );
+ }
+
+}
View
53 tests/Unit/Doctrine/Search/Http/Client/BuzzTest.php
@@ -0,0 +1,53 @@
+<?php
+namespace Unit\Doctrine\Search\Http\Adapter\Client;
+
+use Unit\Doctrine\Search\Http\Client\Mocks\FileGetContentsMock;
+
+use Doctrine\Search\Http\Client\BuzzClient;
+use Buzz\Browser;
+
+/**
+ * @author Bachi
+ */
+class BuzzTest extends \PHPUnit_Framework_TestCase
+{
+ private $client;
+
+ private $browser;
+
+ protected function setUp()
+ {
+ $this->browser = $this->getMock('Buzz\\Browser', array(), array(), '', false);
+ $this->client = new BuzzClient($this->browser, 'www.google.de', 80);
+ }
+
+ public function testCallExistingHost()
+ {
+ $buzzResponse = $this->getMock('Buzz\Message\Response', array(), array(), '', false);
+
+ $this->browser->expects($this->once())
+ ->method('call')
+ ->will($this->returnValue($buzzResponse));
+
+ $response = $this->client->sendRequest();
+ $this->assertInstanceOf('Doctrine\\Search\\Http\\ResponseInterface', $response);
+
+ //@todo this should be tested in the Response-Test
+ /*$this->assertContains('<html>', $response->getContent());
+ $this->assertTrue($response->isSuccessfull());*/
+ }
+
+
+ public function testCallNotExistingHost()
+ {
+ $this->setExpectedException('\RuntimeException');
+
+ $clientAdapter = new FileGetContentsMock();
+
+ $browser = new Browser($clientAdapter);
+
+ $client = new BuzzClient($browser, 'www.not-existing-host.de', 80);
+ $client->sendRequest('get');
+ }
+
+}
View
13 tests/Unit/Doctrine/Search/Http/Client/Mocks/FileGetContentsMock.php
@@ -0,0 +1,13 @@
+<?php
+namespace Unit\Doctrine\Search\Http\Client\Mocks;
+
+use Buzz\Client\ClientInterface;
+use Buzz\Message;
+
+class FileGetContentsMock implements ClientInterface
+{
+ public function send(Message\Request $request, Message\Response $response)
+ {
+ throw new \RuntimeException('Just a fake');
+ }
+}
View
7 tests/Unit/Doctrine/Search/Solr/Tests/ClientTest.php
@@ -0,0 +1,7 @@
+<?php
+namespace Unit\Doctrine\Search\Solr\Tests;
+
+class ClientTest /* extends \PHPUnit_Framework_TestCase */
+{
+
+}
View
28 tests/Unit/Doctrine/Search/VersionTest.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Unit\Doctrine\Search;
+use Doctrine\Search\Version;
+
+class VersionTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider getVersions
+ */
+ public function testCompareVersion($version, $expected)
+ {
+ $this->assertEquals($expected, Version::compare($version));
+ }
+
+ static public function getVersions()
+ {
+ return array(
+ array('1.0', 1),
+ array('0', -1),
+ array('-1', -1),
+ array('0.1-alpha', 0),
+ array('0.1', 1),
+ array('0.1-beta', 1),
+ );
+ }
+
+}
View
2  tests/bootstrap.php
@@ -12,6 +12,8 @@
$loader->register();
$loader = new ClassLoader('Doctrine\\Search', __DIR__ . '/../lib');
$loader->register();
+$loader = new ClassLoader('Unit\\Doctrine\\Search', __DIR__ . '/');
+$loader->register();
AnnotationRegistry::registerFile(__DIR__ . '/../lib/Doctrine/Search/Mapping/Annotations/DoctrineAnnotations.php');
BuzzAutoloader::register();
Please sign in to comment.
Something went wrong with that request. Please try again.