From 559cd65ab7073419c03b8bb139fc87fb4a451f78 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 31 Oct 2014 15:13:46 -0400
Subject: [PATCH] Added tests.

---
 .../src/VuFindTest/Solr/WriterTest.php        | 162 ++++++++++++++++++
 1 file changed, 162 insertions(+)
 create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/Solr/WriterTest.php

diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Solr/WriterTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Solr/WriterTest.php
new file mode 100644
index 00000000000..ace04286d2b
--- /dev/null
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Solr/WriterTest.php
@@ -0,0 +1,162 @@
+<?php
+/**
+ * Solr Writer Test Class
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind2
+ * @package  Tests
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
+ */
+namespace VuFindTest\Solr;
+use VuFind\Db\Table\ChangeTracker, VuFind\Search\BackendManager;
+use VuFind\Solr\Writer;
+
+/**
+ * Solr Utils Test Class
+ *
+ * @category VuFind2
+ * @package  Tests
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
+ */
+class WriterTest extends \VuFindTest\Unit\TestCase
+{
+    /**
+     * Test commit
+     *
+     * @return void
+     */
+    public function testCommit()
+    {
+        $bm = $this->getBackendManagerWithMockSolr();
+        $connector = $bm->get('Solr')->getConnector();
+        $connector->expects($this->at(0))->method('setTimeout')->with($this->equalTo(60 * 60));
+        $connector->expects($this->once())->method('write')->with($this->isInstanceOf('VuFindSearch\Backend\Solr\Document\CommitDocument'));
+        $connector->expects($this->at(2))->method('setTimeout')->with($this->equalTo(30));
+        $writer = new Writer($bm, $this->getMockChangeTracker());
+        $writer->commit('Solr');
+    }
+
+    /**
+     * Test save
+     *
+     * @return void
+     */
+    public function testSave()
+    {
+        $bm = $this->getBackendManagerWithMockSolr();
+        $commit = new \VuFindSearch\Backend\Solr\Document\CommitDocument();
+        $connector = $bm->get('Solr')->getConnector();
+        $connector->expects($this->once())->method('write')->with($this->equalTo($commit));
+        $writer = new Writer($bm, $this->getMockChangeTracker());
+        $writer->save('Solr', $commit);
+    }
+
+    /**
+     * Test optimize
+     *
+     * @return void
+     */
+    public function testOptimize()
+    {
+        $bm = $this->getBackendManagerWithMockSolr();
+        $connector = $bm->get('Solr')->getConnector();
+        $connector->expects($this->at(0))->method('setTimeout')->with($this->equalTo(60 * 60 * 24));
+        $connector->expects($this->once())->method('write')->with($this->isInstanceOf('VuFindSearch\Backend\Solr\Document\OptimizeDocument'));
+        $connector->expects($this->at(2))->method('setTimeout')->with($this->equalTo(30));
+        $writer = new Writer($bm, $this->getMockChangeTracker());
+        $writer->optimize('Solr');
+    }
+
+    /**
+     * Test delete all
+     *
+     * @return void
+     */
+    public function testDeleteAll()
+    {
+        $bm = $this->getBackendManagerWithMockSolr();
+        $connector = $bm->get('Solr')->getConnector();
+        $callback = function ($i) {
+            return trim($i->asXML()) == "<?xml version=\"1.0\"?>\n<delete><query>*:*</query></delete>";
+        };
+        $connector->expects($this->once())->method('write')->with($this->callback($callback));
+        $writer = new Writer($bm, $this->getMockChangeTracker());
+        $writer->deleteAll('Solr');
+    }
+
+    /**
+     * Test delete records
+     *
+     * @return void
+     */
+    public function testDeleteRecords()
+    {
+        $bm = $this->getBackendManagerWithMockSolr();
+        $connector = $bm->get('Solr')->getConnector();
+        $callback = function ($i) {
+            return trim($i->asXML()) == "<?xml version=\"1.0\"?>\n<delete><id>foo</id><id>bar</id></delete>";
+        };
+        $connector->expects($this->once())->method('write')->with($this->callback($callback));
+        $ct = $this->getMockChangeTracker();
+        $ct->expects($this->at(0))->method('markDeleted')->with($this->equalTo('biblio'), $this->equalTo('foo'));
+        $ct->expects($this->at(1))->method('markDeleted')->with($this->equalTo('biblio'), $this->equalTo('bar'));
+        $writer = new Writer($bm, $ct);
+        $writer->deleteRecords('Solr', array('foo', 'bar'));
+    }
+
+    /**
+     * Get mock backend manager
+     *
+     * @return BackendManager
+     */
+    protected function getBackendManagerWithMockSolr()
+    {
+        $sm = new \Zend\ServiceManager\ServiceManager();
+        $pm = new BackendManager($sm);
+        $mockBackend = $this->getMockBuilder('VuFindSearch\Backend\Solr\Backend')
+            ->disableOriginalConstructor()
+            ->getMock();
+        $mockConnector = $this->getMockBuilder('VuFindSearch\Backend\Solr\Connector')
+            ->disableOriginalConstructor()
+            ->setMethods(array('getUrl', 'setTimeout', 'write'))
+            ->getMock();
+        $mockBackend->expects($this->any())->method('getConnector')->will($this->returnValue($mockConnector));
+        $mockConnector->expects($this->any())->method('getTimeout')->will($this->returnValue(30));
+        $mockConnector->expects($this->any())->method('getUrl')->will($this->returnValue('http://localhost:8080/solr/biblio'));
+        $sm->setService('Solr', $mockBackend);
+        return $pm;
+    }
+
+    /**
+     * Get mock change tracker
+     *
+     * @return ChangeTracker
+     */
+    protected function getMockChangeTracker()
+    {
+        return $this->getMockBuilder('VuFind\Db\Table\ChangeTracker')
+            ->disableOriginalConstructor()
+            ->setMethods(array('markDeleted'))
+            ->getMock();
+    }
+}
\ No newline at end of file
-- 
GitLab