Foxtrott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/232642
Change subject: Minor refactoring & improvements to test coverage of
MenuFactory & MenuFromLines
......................................................................
Minor refactoring & improvements to test coverage of MenuFactory & MenuFromLines
Change-Id: Ia43fd38e938a17fad52750125253e247285554ea
---
M src/Menu/MenuFactory.php
M src/Menu/MenuFromLines.php
M tests/phpunit/Unit/Menu/MenuFromLinesTest.php
3 files changed, 63 insertions(+), 22 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/skins/chameleon
refs/changes/42/232642/1
diff --git a/src/Menu/MenuFactory.php b/src/Menu/MenuFactory.php
index 9fae9c3..a03c6f7 100644
--- a/src/Menu/MenuFactory.php
+++ b/src/Menu/MenuFactory.php
@@ -4,7 +4,7 @@
*
* This file is part of the MediaWiki skin Chameleon.
*
- * @copyright 2013 - 2014, Stephan Gambke
+ * @copyright 2013 - 2015, Stephan Gambke
* @license GNU General Public License, version 3 (or any later version)
*
* The Chameleon skin is free software: you can redistribute it and/or modify
@@ -49,9 +49,7 @@
$message = \Message::newFromKey( $message );
}
- if ( !is_a( $message, '\\Message' ) ) {
- throw new \MWException( 'String, array of strings or
Message object expected. Got ' . is_object( $message ) ? get_class( $message )
: gettype( $message ) . '.' );
- }
+ $this->assert( is_a( $message, '\\Message' ), 'String, array of
strings or Message object expected.', $message );
if ( $forContent ) {
$message = $message->inContentLanguage();
@@ -73,12 +71,20 @@
*/
public function getMenuFromMessageText( $text, $forContent = false ) {
- if ( !is_string( $text ) ) {
- throw new \MWException( 'String expected. Got ' .
is_object( $text ) ? get_class( $text ) : gettype( $text ) . '.' );
- }
+ $this->assert( is_string( $text ), 'String expected.', $text );
$lines = explode( "\n", trim( $text ) );
return new MenuFromLines( $lines, $forContent );
}
+
+ /**
+ * @param $message
+ * @throws \MWException
+ */
+ protected function assert( $condition, $message, $target ) {
+ if ( !$condition ) {
+ throw new \MWException( $message . 'Got ' . (is_object(
$target ) ? get_class( $target ) : gettype( $target )) . '.' );
+ }
+ }
}
diff --git a/src/Menu/MenuFromLines.php b/src/Menu/MenuFromLines.php
index b47c638..ea55fab 100644
--- a/src/Menu/MenuFromLines.php
+++ b/src/Menu/MenuFromLines.php
@@ -4,7 +4,7 @@
*
* This file is part of the MediaWiki skin Chameleon.
*
- * @copyright 2013 - 2014, Stephan Gambke
+ * @copyright 2013 - 2015, Stephan Gambke
* @license GNU General Public License, version 3 (or any later version)
*
* The Chameleon skin is free software: you can redistribute it and/or modify
@@ -73,13 +73,12 @@
*/
public function getHtml() {
- if ( $this->html !== null ) {
- return $this->html;
+ if ( $this->html === null ) {
+
+ $this->parseLines();
+ $this->html = $this->buildHtml();
+
}
-
- $this->parseLines();
-
- $this->html = $this->buildHtml();
return $this->html;
}
diff --git a/tests/phpunit/Unit/Menu/MenuFromLinesTest.php
b/tests/phpunit/Unit/Menu/MenuFromLinesTest.php
index 08a150b..cd8cbcc 100644
--- a/tests/phpunit/Unit/Menu/MenuFromLinesTest.php
+++ b/tests/phpunit/Unit/Menu/MenuFromLinesTest.php
@@ -2,7 +2,7 @@
/**
* File containing the MenuFromLinesTest class
*
- * @copyright (C) 2013 - 2014, Stephan Gambke
+ * @copyright (C) 2013 - 2015, Stephan Gambke
* @license GNU General Public License, version 3 (or any later version)
*
* The Chameleon skin is free software: you can redistribute it and/or modify
@@ -59,25 +59,61 @@
}
/**
+ * @covers ::__construct
*
+ * Just checking that giving an item data parameter does not immediately
+ * break the constructor. No actual functionality beyond that is tested.
+ */
+ public function testCanConstructWithItemData() {
+
+ $lines = array();
+
+ /** @var MenuFromLines $instance */
+ $instance = new MenuFromLines( $lines, true, array(
+ 'text' => 'foo',
+ 'href' => 'bar',
+ 'depth' => 42
+ ) );
+
+ $this->assertInstanceOf(
+ 'Skins\Chameleon\Menu\MenuFromLines',
+ $instance
+ );
+ }
+
+ /**
+ * @covers ::getHtml
+ * @covers ::parseLines
*/
public function testBuildEmptyMenu() {
$lines = array(
'',
'* Foo',
- '** FooBar',
- '*** FooBarBaz',
+ '** | FooBar',
+ '*** http://foo.com | FooBarBaz',
+ '*** # | FooBarQuok',
'* Test | Bar',
);
$ap = $GLOBALS[ 'wgArticlePath' ];
- $expected = "\t<ul>\n\t\t<li>\n\t\t\t<a href=\"" . str_replace(
'$1', 'Foo', $ap ) .
- "\">Foo</a>\n\t\t\t<ul>\n\t\t\t\t<li>\n\t\t\t\t\t<a
href=\"" . str_replace( '$1', 'FooBar', $ap ) .
- "\">FooBar</a>\n\t\t\t\t\t<ul>\n\t\t\t\t\t\t<li><a
href=\"" . str_replace( '$1', 'FooBarBaz', $ap ) .
-
"\">FooBarBaz</a></li>\n\t\t\t\t\t</ul>\n\t\t\t\t</li>\n\t\t\t</ul>\n\t\t</li>\n\t\t<li><a
href=\"" . str_replace( '$1', 'Test', $ap ) .
- "\">Bar</a></li>\n\t</ul>\n";
+ $expected =
+ "\t<ul>\n" .
+ "\t\t<li>\n" .
+ "\t\t\t<a href=\"" . str_replace( '$1', 'Foo', $ap ) .
"\">Foo</a>\n" .
+ "\t\t\t<ul>\n" .
+ "\t\t\t\t<li>\n" .
+ "\t\t\t\t\t<a href=\"#" . "\">FooBar</a>\n" .
+ "\t\t\t\t\t<ul>\n" .
+ "\t\t\t\t\t\t<li><a
href=\"http://foo.com\">FooBarBaz</a></li>\n" .
+ "\t\t\t\t\t\t<li><a href=\"#\">FooBarQuok</a></li>\n" .
+ "\t\t\t\t\t</ul>\n" .
+ "\t\t\t\t</li>\n" .
+ "\t\t\t</ul>\n" .
+ "\t\t</li>\n" .
+ "\t\t<li><a href=\"" . str_replace( '$1', 'Test', $ap )
. "\">Bar</a></li>\n" .
+ "\t</ul>\n";
/** @var MenuFromLines $instance */
$instance = new MenuFromLines( $lines, true );
--
To view, visit https://gerrit.wikimedia.org/r/232642
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia43fd38e938a17fad52750125253e247285554ea
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/skins/chameleon
Gerrit-Branch: master
Gerrit-Owner: Foxtrott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits