jenkins-bot has submitted this change and it was merged.
Change subject: Add Reader\Hash, a faux CDB reader wrapping an array.
......................................................................
Add Reader\Hash, a faux CDB reader wrapping an array.
This is needed for refactoring the Interwiki class,
see I7d7424345d0ce3ce90ba284006ee9615e3d99baa
Change-Id: If8a14ae5c1d9d325a3f25a70c71edf19522a3dd6
---
M composer.json
A src/Reader/Hash.php
M tests/CdbTest.php
A tests/Reader/HashTest.php
4 files changed, 205 insertions(+), 1 deletion(-)
Approvals:
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git a/composer.json b/composer.json
index d3ef7d7..f6c7fb7 100644
--- a/composer.json
+++ b/composer.json
@@ -15,6 +15,9 @@
{
"name": "Ori Livneh",
"email": "[email protected]"
+ },
+ {
+ "name": "Daniel Kinzler"
}
],
"autoload": {
@@ -23,7 +26,8 @@
]
},
"require": {
- "php": ">=5.3.2"
+ "php": ">=5.3.2",
+ "wikimedia/assert": "~0.2.2"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "0.9",
diff --git a/src/Reader/Hash.php b/src/Reader/Hash.php
new file mode 100644
index 0000000..3010305
--- /dev/null
+++ b/src/Reader/Hash.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace Cdb\Reader;
+
+use Cdb\Reader;
+use Wikimedia\Assert\Assert;
+
+/**
+ * Hash implements the CdbReader interface based on an associative
+ * PHP array (a.k.a "hash").
+ *
+ * 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
+ *
+ * @file
+ */
+
+/**
+ * Hash implements the CdbReader interface based on an associative
+ * PHP array (a.k.a "hash").
+ */
+class Hash extends Reader {
+
+ /**
+ * @var string
+ */
+ private $data;
+
+ /**
+ * A queue of keys to return from nextkey(),
+ * initialized by firstkey();
+ *
+ * @var string[]|null
+ */
+ private $keys = null;
+
+ /**
+ * Create the object and open the file
+ *
+ * @param string[] $data An associative PHP array.
+ */
+ public function __construct( $data ) {
+ Assert::parameterType( 'array', $data, '$data' );
+ Assert::parameterElementType( 'string', $data, '$data' );
+
+ $this->data = $data;
+ }
+
+ /**
+ * Close the file. Optional, you can just let the variable go out of
scope.
+ */
+ public function close() {
+ $this->data = array();
+ $this->keys = null;
+ }
+
+ /**
+ * Get a value with a given key. Only string values are supported.
+ *
+ * @param string $key
+ *
+ * @return bool|string The value associated with $key, or false if $key
is not known.
+ */
+ public function get( $key ) {
+ return isset( $this->data[ $key ] ) ? $this->data[ $key ] :
false;
+ }
+
+ /**
+ * Check whether key exists
+ *
+ * @param string $key
+ *
+ * @return bool
+ */
+ public function exists( $key ) {
+ return isset( $this->data[ $key ] );
+ }
+
+ /**
+ * Fetch first key
+ *
+ * @return string
+ */
+ public function firstkey() {
+ $this->keys = array_keys( $this->data );
+ return $this->nextkey();
+ }
+
+ /**
+ * Fetch next key
+ *
+ * @return string
+ */
+ public function nextkey() {
+ if ( $this->keys === null ) {
+ return $this->firstkey();
+ }
+
+ return empty( $this->keys ) ? false : array_shift( $this->keys
);
+ }
+
+}
diff --git a/tests/CdbTest.php b/tests/CdbTest.php
index 9795b0f..6ef4655 100644
--- a/tests/CdbTest.php
+++ b/tests/CdbTest.php
@@ -7,6 +7,7 @@
/**
* Test the CDB reader/writer
+ * @group Cdb
* @covers Cdb\Writer\PHP
* @covers Cdb\Writer\DBA
*/
diff --git a/tests/Reader/HashTest.php b/tests/Reader/HashTest.php
new file mode 100644
index 0000000..bab34e7
--- /dev/null
+++ b/tests/Reader/HashTest.php
@@ -0,0 +1,85 @@
+<?php
+namespace Cdb\Test\Reader;
+
+use Cdb\Reader\Hash;
+
+/**
+ * @group Cdb
+ * @covers Cdb\Reader\Hash
+ */
+class HashTest extends \PHPUnit_Framework_TestCase {
+
+ public function provideConstructor_fail() {
+ return array(
+ array( 'Foo' ),
+ array( array( "x" => 1, "y" => 2 ) ),
+ );
+ }
+
+ /**
+ * @dataProvider provideConstructor_fail
+ */
+ public function testConstructor_fail( $data ) {
+ $this->setExpectedException( 'InvalidArgumentException' );
+
+ new Hash( $data );
+ }
+
+ public function testClose() {
+ $reader = new Hash( array( 'foo' => 'FOO' ) );
+ $reader->close();
+
+ $this->assertFalse( $reader->get( 'foo' ) );
+ }
+
+ public function testGet() {
+ $reader = new Hash( array( 'foo' => 'FOO' ) );
+
+ $this->assertSame( 'FOO', $reader->get( 'foo' ) );
+ $this->assertFalse( $reader->get( 'xyz' ) );
+ }
+
+ public function testExists() {
+ $reader = new Hash( array( 'foo' => 'FOO' ) );
+
+ $this->assertTrue( $reader->exists( 'foo' ) );
+ $this->assertFalse( $reader->exists( 'xyz' ) );
+ }
+
+ public function testFirstKey() {
+ $reader = new Hash( array(
+ 'one' => 'ONE',
+ 'two' => 'TWO',
+ ) );
+
+ $this->assertSame( 'one', $reader->firstkey() );
+ $this->assertSame( 'one', $reader->firstkey() );
+
+ $reader->nextkey();
+ $this->assertSame( 'one', $reader->firstkey() );
+ }
+
+ public function testFirstKey_empty() {
+ $reader = new Hash( array() );
+
+ $this->assertFalse( $reader->firstkey() );
+ }
+
+ public function testNextKey() {
+ $reader = new Hash( array(
+ 'one' => 'ONE',
+ 'two' => 'TWO',
+ ) );
+
+ $this->assertSame( 'one', $reader->nextkey() );
+ $this->assertSame( 'two', $reader->nextkey() );
+
+ $reader->firstkey();
+ $this->assertSame( 'two', $reader->nextkey() );
+ $this->assertFalse( $reader->nextkey() );
+
+ $reader->firstkey();
+ $this->assertSame( 'two', $reader->nextkey() );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/286194
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If8a14ae5c1d9d325a3f25a70c71edf19522a3dd6
Gerrit-PatchSet: 4
Gerrit-Project: cdb
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Chad <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits