Mwjames has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/71574


Change subject: \SMW\Store\Collector add test
......................................................................

\SMW\Store\Collector add test

Change-Id: Ic73f05c5771e1eed19583f3eae8a440ffcaaefc6
---
M includes/ArrayAccessor.php
M includes/Setup.php
M includes/storage/Collector.php
A tests/phpunit/includes/storage/CollectorTest.php
M tests/phpunit/includes/storage/StoreTest.php
5 files changed, 152 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/74/71574/1

diff --git a/includes/ArrayAccessor.php b/includes/ArrayAccessor.php
index 69e5802..a354d62 100644
--- a/includes/ArrayAccessor.php
+++ b/includes/ArrayAccessor.php
@@ -68,12 +68,25 @@
 
 }
 
+interface Arrayable {
+
+       /**
+        * Returns an object as array
+        *
+        * @since 1.9
+        *
+        * @return array
+        */
+       public function toArray();
+
+}
+
 /**
  * This class enables access to an arbitrary array
  *
  * @ingroup Accessor
  */
-class ArrayAccessor extends ArrayObject implements Accessor {
+class ArrayAccessor extends ArrayObject implements Accessor, Arrayable {
 
        /**
         * Returns if a specified key is set or not
diff --git a/includes/Setup.php b/includes/Setup.php
index 66c873b..d562df5 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -152,6 +152,7 @@
        $wgAutoloadClasses['SMW\Profiler']              = $incDir . 
'Profiler.php';
 
        $wgAutoloadClasses['SMW\Accessor']              = $incDir . 
'ArrayAccessor.php';
+       $wgAutoloadClasses['SMW\Arrayable']             = $incDir . 
'ArrayAccessor.php';
        $wgAutoloadClasses['SMW\ArrayAccessor']         = $incDir . 
'ArrayAccessor.php';
 
        $wgAutoloadClasses['SMW\CacheHandler']          = $incDir . 
'/handlers/CacheHandler.php';
diff --git a/includes/storage/Collector.php b/includes/storage/Collector.php
index f8eb439..0401c36 100644
--- a/includes/storage/Collector.php
+++ b/includes/storage/Collector.php
@@ -6,6 +6,7 @@
 use SMW\DIProperty;
 use SMW\Settings;
 
+use SMWRequestOptions;
 use InvalidArgumentException;
 use MWTimestamp;
 
@@ -93,7 +94,7 @@
         *
         * @return Collector
         */
-       public function setRequestOptions( $requestOptions ) {
+       public function setRequestOptions( SMWRequestOptions $requestOptions ) {
                $this->requestOptions = $requestOptions;
                return $this;
        }
diff --git a/tests/phpunit/includes/storage/CollectorTest.php 
b/tests/phpunit/includes/storage/CollectorTest.php
new file mode 100644
index 0000000..9700d96
--- /dev/null
+++ b/tests/phpunit/includes/storage/CollectorTest.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\ArrayAccessor;
+
+/**
+ * Tests for the Collector class
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.9
+ *
+ * @file
+ *
+ * @license GNU GPL v2+
+ * @author mwjames
+ */
+
+/**
+ * @covers \SMW\Store\Collector
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ */
+class CollectorTest extends SemanticMediaWikiTestCase {
+
+       /**
+        * Returns the name of the class to be tested
+        *
+        * @return string|false
+        */
+       public function getClass() {
+               return '\SMW\Store\Collector';
+       }
+
+       /**
+        * Helper method that returns a Collector object
+        *
+        * @since 1.9
+        *
+        * @param $result
+        *
+        * @return Collector
+        */
+       private function getInstance( $doCollect = array(), $cacheAccessor = 
array() ) {
+
+               $collector = $this->getMockBuilder( $this->getClass() )
+                       ->setMethods( array( 'cacheAccessor', 'doCollect' ) )
+                       ->getMock();
+
+               $collector->expects( $this->any() )
+                       ->method( 'doCollect' )
+                       ->will( $this->returnValue( $doCollect ) );
+
+               $collector->expects( $this->any() )
+                       ->method( 'cacheAccessor' )
+                       ->will( $this->returnValue( new ArrayAccessor( 
$cacheAccessor ) ) );
+
+               return $collector;
+       }
+
+       /**
+        * @test Collector::__construct
+        *
+        * @since 1.9
+        */
+       public function testConstructor() {
+               $instance = $this->getInstance();
+               $this->assertInstanceOf( $this->getClass(), $instance );
+       }
+
+       /**
+        * @test Collector::getResults
+        *
+        * @since 1.9
+        */
+       public function testGetResults() {
+
+               $accessor = array(
+                       'id'      => rand(),
+                       'type'    => false,
+                       'enabled' => false,
+                       'expiry'  => 100
+               );
+
+               $expected = array( $this->getRandomString() );
+
+               $instance = $this->getInstance( $expected, $accessor );
+               $result   = $instance->getResults();
+
+               $this->assertInternalType( 'array', $result );
+               $this->assertEquals( $expected, $result );
+
+               $accessor = array(
+                       'id'      => rand(),
+                       'type'    => 'hash',
+                       'enabled' => true,
+                       'expiry'  => 100
+               );
+
+               $expected = array( $this->getRandomString(), 
$this->getRandomString() );
+
+               $instance = $this->getInstance( $expected, $accessor );
+               $result   = $instance->getResults();
+
+               $this->assertInternalType( 'array', $result );
+               $this->assertEquals( $expected, $result );
+
+               // Initialized with a different 'expected' set but due to that 
the id
+               // has not changed results are expected to be cached and be 
equal to
+               // the results of the previous initialization
+               $instance = $this->getInstance( array( 'Lula' ), $accessor );
+               $this->assertEquals( $result, $instance->getResults() );
+
+       }
+}
diff --git a/tests/phpunit/includes/storage/StoreTest.php 
b/tests/phpunit/includes/storage/StoreTest.php
index e0c8f38..1e5d3e4 100644
--- a/tests/phpunit/includes/storage/StoreTest.php
+++ b/tests/phpunit/includes/storage/StoreTest.php
@@ -8,6 +8,7 @@
 
 namespace SMW\Test;
 use Title, SMWDIProperty, SMWDIWikiPage, SMWQueryProcessor;
+use SMWRequestOptions;
 
 /**
  * Tests for the SMWStore class.
@@ -197,7 +198,7 @@
 
        public function testGetUnusedPropertiesSpecial() {
                $store = smwfGetStore();
-               $result = $store->getUnusedPropertiesSpecial( null );
+               $result = $store->getUnusedPropertiesSpecial( new 
SMWRequestOptions() );
 
                $this->assertInstanceOf( '\SMW\Store\Collector', $result );
                foreach( $result->getResults() as $row ) {
@@ -211,7 +212,7 @@
 
        public function testGetWantedPropertiesSpecial() {
                $store = smwfGetStore();
-               $result = $store->getWantedPropertiesSpecial( null );
+               $result = $store->getWantedPropertiesSpecial( new 
SMWRequestOptions() );
 
                $this->assertInstanceOf( '\SMW\Store\Collector', $result );
                foreach( $result->getResults() as $row ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/71574
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic73f05c5771e1eed19583f3eae8a440ffcaaefc6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to