From b982c551105bcfc61ac8aaba41ff9a1faa1043de Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 25 Oct 2013 10:33:57 -0400
Subject: [PATCH] Simplified code; added unit test.

---
 module/VuFind/src/VuFind/Autocomplete/Tag.php |  2 +-
 .../src/VuFindTest/Autocomplete/TagTest.php   | 85 +++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/Autocomplete/TagTest.php

diff --git a/module/VuFind/src/VuFind/Autocomplete/Tag.php b/module/VuFind/src/VuFind/Autocomplete/Tag.php
index db7366e46d6..689d1b33a7a 100644
--- a/module/VuFind/src/VuFind/Autocomplete/Tag.php
+++ b/module/VuFind/src/VuFind/Autocomplete/Tag.php
@@ -63,7 +63,7 @@ class Tag implements AutocompleteInterface, \VuFind\Db\Table\DbTableAwareInterfa
         $tagTable = $this->getTagsTable();
         $tags = $tagTable->matchText($query);
         if ($tags) {
-            foreach ($tags as $i=>$tag) {
+            foreach ($tags as $tag) {
                 $tagList[] = $tag['tag'];
             }
         }
diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Autocomplete/TagTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Autocomplete/TagTest.php
new file mode 100644
index 00000000000..174ef5e68df
--- /dev/null
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Autocomplete/TagTest.php
@@ -0,0 +1,85 @@
+<?php
+/**
+ * Tag autocomplete test class.
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2011.
+ *
+ * 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://www.vufind.org  Main Page
+ */
+namespace VuFindTest\Autocomplete;
+use VuFind\Autocomplete\Tag;
+
+/**
+ * Tag autocomplete 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://www.vufind.org  Main Page
+ */
+class TagTest extends \VuFindTest\Unit\DbTestCase
+{
+    /**
+     * Test that missing plugin manager causes exception.
+     *
+     * @return void
+     * @expectedException Exception
+     * @expectedExceptionMessage DB table manager missing.
+     */
+    public function testMissingDependency()
+    {
+        $tag = new Tag();
+        $tag->getSuggestions('foo');
+    }
+
+    /**
+     * Test real suggestions.
+     *
+     * @return void
+     */
+    public function testSuggestions()
+    {
+        // Fake DB response:
+        $tags = array(
+            array('tag' => 'bar1'),
+            array('tag' => 'bar2'),
+        );
+
+        // Fake services:
+        $tagTable = $this->getMock('VuFind\Db\Table\Tags', array('matchText'));
+        $tagTable->expects($this->once())->method('matchText')
+            ->with($this->equalTo('foo'))
+            ->will($this->returnValue($tags));
+        $tableManager
+            = $this->getMock('VuFind\Db\Table\PluginManager', array('get'));
+        $tableManager->expects($this->once())->method('get')
+            ->with($this->equalTo('Tags'))
+            ->will($this->returnValue($tagTable));
+
+        // Real object to test:
+        $tag = new Tag();
+        $tag->setDbTableManager($tableManager);
+
+        $this->assertEquals(array('bar1', 'bar2'), $tag->getSuggestions('foo'));
+    }
+}
\ No newline at end of file
-- 
GitLab