Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/49928
Change subject: Added toArray to sort expressions and some restricturing of
tests
......................................................................
Added toArray to sort expressions and some restricturing of tests
Change-Id: I81f07cdefe433890d41f5d7c39085fbce05bc9fc
---
M Ask.classes.php
M Ask.mw.php
A includes/Ask/ArrayValueProvider.php
M includes/Ask/Arrayable.php
M includes/Ask/Comparable.php
M includes/Ask/Hashable.php
M includes/Ask/Immutable.php
M includes/Ask/Language/Description/Description.php
M includes/Ask/Language/Option/PropertyValueSortExpression.php
M includes/Ask/Language/Option/SortExpression.php
M includes/Ask/Language/Option/SortOptions.php
M includes/Ask/Language/Selection/SelectionRequest.php
A includes/Ask/Typeable.php
M tests/phpunit/AskTestCase.php
M tests/phpunit/Language/Description/DescriptionTest.php
A tests/phpunit/Language/Option/PropertyValueSortExpressionTest.php
A tests/phpunit/Language/Option/SortExpressionTest.php
M tests/phpunit/Language/Selection/SelectionRequestTest.php
18 files changed, 269 insertions(+), 100 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Ask
refs/changes/28/49928/1
diff --git a/Ask.classes.php b/Ask.classes.php
index d7abad6..f3925a8 100644
--- a/Ask.classes.php
+++ b/Ask.classes.php
@@ -17,9 +17,11 @@
$classes = array(
'Ask\Arrayable',
+ 'Ask\ArrayValueProvider',
'Ask\Comparable',
'Ask\Hashable',
'Ask\Immutable',
+ 'Ask\Typeable',
'Ask\Language\Description\AnyValue',
'Ask\Language\Description\Conjunction',
diff --git a/Ask.mw.php b/Ask.mw.php
index 1179d2b..e51946a 100644
--- a/Ask.mw.php
+++ b/Ask.mw.php
@@ -47,6 +47,9 @@
$wgAutoloadClasses['Ask\Tests\AskTestCase']
= __DIR__ . '/tests/phpunit/AskTestCase.php';
+ $wgAutoloadClasses['Ask\Tests\Option\SortExpressionTest']
+ = __DIR__ . '/tests/phpunit/Language/Option/SortExpression.php';
+
$wgAutoloadClasses['Ask\Tests\Language\Description\DescriptionCollectionTest']
= __DIR__ .
'/tests/phpunit/Language/Description/DescriptionCollectionTest.php';
diff --git a/includes/Ask/ArrayValueProvider.php
b/includes/Ask/ArrayValueProvider.php
new file mode 100644
index 0000000..138ea80
--- /dev/null
+++ b/includes/Ask/ArrayValueProvider.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Ask;
+
+/**
+ * Interface for objects that have a getArrayValue method.
+ *
+ * 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 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+interface ArrayValueProvider {
+
+ /**
+ * Returns a representation of the objects value in primitive form,
+ * using only primitive values and arrays. The return value
+ * is thus does not contain any objects and can be fed to
+ * json_encode and similar. The result should typically have
+ * an understandable and stable format.
+ *
+ * @since 0.1
+ *
+ * @return array|null|bool|int|float|string
+ */
+ public function getArrayValue();
+
+}
\ No newline at end of file
diff --git a/includes/Ask/Arrayable.php b/includes/Ask/Arrayable.php
index e3ba24c..81a367d 100644
--- a/includes/Ask/Arrayable.php
+++ b/includes/Ask/Arrayable.php
@@ -31,20 +31,19 @@
interface Arrayable {
/**
- * Returns a representation of the object in primitive form,
- * using only primitive values and arrays. The return value
- * is thus does not contain any objects and can be fed to
- * json_encode and similar. The result should typically have
- * an understandable and stable format.
+ * Returns a representation of the objects value and type in
+ * primitive form. The returned array contains two elements:
*
- * The returned value might contain an absolute or relative type
- * identifier that can be used to construct an object from the
- * return value.
+ * - type: A string that is an identifier for the type of object
+ *
+ * - value: array|null|bool|int|float|string
+ * Contains no non-array, non-primitive values and
+ * can thus be fed to functions such as json_encode.
*
* @since 0.1
*
- * @return array|null|bool|int|float|string
+ * @return array
*/
public function toArray();
-};
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/includes/Ask/Comparable.php b/includes/Ask/Comparable.php
index e3be6c6..2e8ecd3 100644
--- a/includes/Ask/Comparable.php
+++ b/includes/Ask/Comparable.php
@@ -28,4 +28,4 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-interface Comparable extends \Comparable {};
\ No newline at end of file
+interface Comparable extends \Comparable {}
\ No newline at end of file
diff --git a/includes/Ask/Hashable.php b/includes/Ask/Hashable.php
index c47322b..a26af83 100644
--- a/includes/Ask/Hashable.php
+++ b/includes/Ask/Hashable.php
@@ -28,4 +28,4 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-interface Hashable extends \Hashable {};
\ No newline at end of file
+interface Hashable extends \Hashable {}
\ No newline at end of file
diff --git a/includes/Ask/Immutable.php b/includes/Ask/Immutable.php
index a3f9f00..c738b24 100644
--- a/includes/Ask/Immutable.php
+++ b/includes/Ask/Immutable.php
@@ -29,4 +29,4 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-interface Immutable extends \Immutable {};
\ No newline at end of file
+interface Immutable extends \Immutable {}
\ No newline at end of file
diff --git a/includes/Ask/Language/Description/Description.php
b/includes/Ask/Language/Description/Description.php
index f7b5e66..d357fb5 100644
--- a/includes/Ask/Language/Description/Description.php
+++ b/includes/Ask/Language/Description/Description.php
@@ -30,7 +30,7 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-abstract class Description implements \Ask\Arrayable, \Ask\Comparable,
\Ask\Hashable {
+abstract class Description implements \Ask\Arrayable, \Ask\Comparable,
\Ask\Hashable, \Ask\Typeable, \Ask\ArrayValueProvider {
/**
* Returns the size of the description.
@@ -49,26 +49,6 @@
* @return integer
*/
public abstract function getDepth();
-
- /**
- * Returns a string identifier for the description's type.
- *
- * @since 0.1
- *
- * @return string
- */
- public abstract function getType();
-
- /**
- * Returns the value in a format that contains only primitive values
- * and arrays. This format is typically stable and easy to understand,
- * and thus ideal for serialization such as json_encode.
- *
- * @since 0.1
- *
- * @return string
- */
- public abstract function getArrayValue();
/**
* @see \Ask\Arrayable::toArray
diff --git a/includes/Ask/Language/Option/PropertyValueSortExpression.php
b/includes/Ask/Language/Option/PropertyValueSortExpression.php
index c3012f6..1250655 100644
--- a/includes/Ask/Language/Option/PropertyValueSortExpression.php
+++ b/includes/Ask/Language/Option/PropertyValueSortExpression.php
@@ -84,4 +84,17 @@
return SortExpression::PROPERTY_VALUE;
}
+ /**
+ * @see SortExpression::getArrayValue
+ *
+ * @since 0.1
+ *
+ * @return string
+ */
+ public function getArrayValue() {
+ return array(
+ 'property' => $this->property->toArray(),
+ );
+ }
+
}
diff --git a/includes/Ask/Language/Option/SortExpression.php
b/includes/Ask/Language/Option/SortExpression.php
index 0fcce8d..a682e46 100644
--- a/includes/Ask/Language/Option/SortExpression.php
+++ b/includes/Ask/Language/Option/SortExpression.php
@@ -28,7 +28,7 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-abstract class SortExpression {
+abstract class SortExpression implements \Ask\Immutable, \Ask\Arrayable,
\Ask\Typeable, \Ask\ArrayValueProvider {
const PROPERTY_VALUE = 'PropertyValue';
@@ -44,15 +44,6 @@
protected $direction = null;
/**
- * Returns the type of the sort expression.
- *
- * @since 0.1
- *
- * @return string
- */
- public abstract function getType();
-
- /**
* Returns the sort direction.
* Either SortExpression::ASCENDING or SortExpression::DESCENDING
*
@@ -65,4 +56,23 @@
return $this->direction;
}
+ /**
+ * @see \Ask\Arrayable::toArray
+ *
+ * This method has a more specific return format then
Arrayable::toArray.
+ * The return value is always an array that holds a type key pointing
+ * to string type identifier (the same one as obtained via ->getType())
+ * and a value key pointing to a mixed (though simple) value.
+ *
+ * @since 0.1
+ *
+ * @return array
+ */
+ public final function toArray() {
+ return array(
+ 'type' => $this->getType(),
+ 'value' => $this->getArrayValue(),
+ );
+ }
+
}
\ No newline at end of file
diff --git a/includes/Ask/Language/Option/SortOptions.php
b/includes/Ask/Language/Option/SortOptions.php
index 81167fb..edcad1c 100644
--- a/includes/Ask/Language/Option/SortOptions.php
+++ b/includes/Ask/Language/Option/SortOptions.php
@@ -28,7 +28,7 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-class SortOptions {
+class SortOptions implements \Ask\Immutable {
/**
* The sort expressions that make up these sort options.
diff --git a/includes/Ask/Language/Selection/SelectionRequest.php
b/includes/Ask/Language/Selection/SelectionRequest.php
index 933540d..d8dc54b 100644
--- a/includes/Ask/Language/Selection/SelectionRequest.php
+++ b/includes/Ask/Language/Selection/SelectionRequest.php
@@ -31,30 +31,10 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-abstract class SelectionRequest implements \Ask\Arrayable, \Ask\Comparable,
\Ask\Hashable {
+abstract class SelectionRequest implements \Ask\Arrayable, \Ask\Comparable,
\Ask\Hashable, \Ask\Typeable, \Ask\ArrayValueProvider {
const TYPE_PROP = 1;
const TYPE_SUBJECT = 2;
-
- /**
- * Returns the type of the selection request.
- *
- * @since 0.1
- *
- * @return string
- */
- public abstract function getType();
-
- /**
- * Returns the value in a format that contains only primitive values
- * and arrays. This format is typically stable and easy to understand,
- * and thus ideal for serialization such as json_encode.
- *
- * @since 0.1
- *
- * @return string
- */
- public abstract function getArrayValue();
/**
* @see \Ask\Arrayable::toArray
diff --git a/includes/Ask/Typeable.php b/includes/Ask/Typeable.php
new file mode 100644
index 0000000..18b6b69
--- /dev/null
+++ b/includes/Ask/Typeable.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Ask;
+
+/**
+ * Interface for objects that have a getType method.
+ *
+ * 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 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+interface Typeable {
+
+ /**
+ * Returns a type identifier for the object.
+ * This identifier does not have to be globally unique,
+ * though is expected to be unique for objects of the same type.
+ *
+ * @since 0.1
+ *
+ * @return string
+ */
+ public function getType();
+
+}
\ No newline at end of file
diff --git a/tests/phpunit/AskTestCase.php b/tests/phpunit/AskTestCase.php
index 6e071c5..e67e38c 100644
--- a/tests/phpunit/AskTestCase.php
+++ b/tests/phpunit/AskTestCase.php
@@ -63,8 +63,26 @@
else {
$this->assertFalse( is_object( $value ), 'Value should
not be an object' );
$this->assertFalse( is_resource( $value ), 'Value
should not be a resource' );
- $this->assertFalse( is_callable( $value ), 'Value
should not be a callable' );
}
}
+ /**
+ * @param array $array
+ * @param object $object Needs to implement both Typeable and
ArrayValueProvider
+ */
+ protected function assertToArrayStructure( array $array, $object ) {
+ $this->assertInternalType( 'array', $array );
+ $this->assertArrayHasKey( 'type', $array );
+ $this->assertArrayHasKey( 'value', $array );
+ $this->assertCount( 2, $array );
+
+ $this->assertEquals(
+ array(
+ 'type' => $object->getType(),
+ 'value' => $object->getArrayValue(),
+ ),
+ $array
+ );
+ }
+
}
diff --git a/tests/phpunit/Language/Description/DescriptionTest.php
b/tests/phpunit/Language/Description/DescriptionTest.php
index 1c6e107..5848180 100644
--- a/tests/phpunit/Language/Description/DescriptionTest.php
+++ b/tests/phpunit/Language/Description/DescriptionTest.php
@@ -86,19 +86,7 @@
*/
public function testReturnValueOfToArray( Description $description ) {
$array = $description->toArray();
-
- $this->assertInternalType( 'array', $array );
- $this->assertArrayHasKey( 'type', $array );
- $this->assertArrayHasKey( 'value', $array );
- $this->assertCount( 2, $array );
-
- $this->assertEquals(
- array(
- 'type' => $description->getType(),
- 'value' => $description->getArrayValue(),
- ),
- $array
- );
+ $this->assertToArrayStructure( $array, $description );
}
/**
diff --git a/tests/phpunit/Language/Option/PropertyValueSortExpressionTest.php
b/tests/phpunit/Language/Option/PropertyValueSortExpressionTest.php
new file mode 100644
index 0000000..f0a7225
--- /dev/null
+++ b/tests/phpunit/Language/Option/PropertyValueSortExpressionTest.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Ask\Tests\Language\Option;
+
+use Ask\Language\Option\SortExpression;
+
+/**
+ * Tests for the Ask\Language\Option\PropertyValueSortExpression 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 0.1
+ *
+ * @file
+ * @ingroup AskTests
+ *
+ * @group Ask
+ * @group AskOption
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class PropertyValueSortExpressionTest extends SortExpressionTest {
+
+
+
+}
diff --git a/tests/phpunit/Language/Option/SortExpressionTest.php
b/tests/phpunit/Language/Option/SortExpressionTest.php
new file mode 100644
index 0000000..49616d8
--- /dev/null
+++ b/tests/phpunit/Language/Option/SortExpressionTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Ask\Tests\Language\Option;
+
+use Ask\Language\Option\SortExpression;
+
+/**
+ * Base class for unit tests for the Ask\Language\Option\SortExpression
deriving classes.
+ *
+ * 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 0.1
+ *
+ * @file
+ * @ingroup AskTests
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+abstract class SortExpressionTest extends \Ask\Tests\AskTestCase {
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @since 0.1
+ *
+ * @param SortExpression $object
+ */
+ public function testReturnValueOfToArray( SortExpression $object ) {
+ $array = $object->toArray();
+ $this->assertToArrayStructure( $array, $object );
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @since 0.1
+ *
+ * @param SortExpression $object
+ */
+ public function testReturnTypeOfGetArrayValue( SortExpression $object )
{
+ $array = $object->getArrayValue();
+ $this->assertPrimitiveStructure( $array );
+ }
+
+}
diff --git a/tests/phpunit/Language/Selection/SelectionRequestTest.php
b/tests/phpunit/Language/Selection/SelectionRequestTest.php
index 754e76b..a66fa26 100644
--- a/tests/phpunit/Language/Selection/SelectionRequestTest.php
+++ b/tests/phpunit/Language/Selection/SelectionRequestTest.php
@@ -63,23 +63,11 @@
*
* @since 0.1
*
- * @param SelectionRequest $description
+ * @param SelectionRequest $object
*/
- public function testReturnValueOfToArray( SelectionRequest $description
) {
- $array = $description->toArray();
-
- $this->assertInternalType( 'array', $array );
- $this->assertArrayHasKey( 'type', $array );
- $this->assertArrayHasKey( 'value', $array );
- $this->assertCount( 2, $array );
-
- $this->assertEquals(
- array(
- 'type' => $description->getType(),
- 'value' => $description->getArrayValue(),
- ),
- $array
- );
+ public function testReturnValueOfToArray( SelectionRequest $object ) {
+ $array = $object->toArray();
+ $this->assertToArrayStructure( $array, $object );
}
/**
@@ -87,11 +75,10 @@
*
* @since 0.1
*
- * @param SelectionRequest $description
+ * @param SelectionRequest $object
*/
- public function testReturnTypeOfGetArrayValue( SelectionRequest
$description ) {
- $array = $description->getArrayValue();
-
+ public function testReturnTypeOfGetArrayValue( SelectionRequest $object
) {
+ $array = $object->getArrayValue();
$this->assertPrimitiveStructure( $array );
}
--
To view, visit https://gerrit.wikimedia.org/r/49928
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I81f07cdefe433890d41f5d7c39085fbce05bc9fc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Ask
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits