Skip to content

Commit

Permalink
test cases; extracted method for retrieving the banner list
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiNissen committed Nov 10, 2015
1 parent 0a48ad6 commit b004036
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 19 deletions.
43 changes: 24 additions & 19 deletions inc/wikibox.class.php
Expand Up @@ -10,25 +10,7 @@ function __construct( $url, $cache = null, $cachedir = null ) {
}

function pick_page( $listPage, $purge = false, $testPage = false ) {
$list = $this->rip_page( $listPage, 'raw', $purge );
if ( !$list && !$testPage ) {
return false;
}

$list = preg_replace( '@<!--.*?-->@s', '', $list );
if ( !preg_match_all( '/^\*+.*\[\[(.+?)( *\|.*?)?\]\]/m', $list, $mm, PREG_SET_ORDER ) ) {
$mm = array();
}
if ( !$mm && !$testPage) {
return false;
}

$list = array();

foreach ( $mm as $m ) {
$list[] = WikiRip::normalizeTitle( $m[1] );
}

$list = $this->fetchList( $listPage, $purge, $testPage );
if ( $testPage ) {
$page = WikiRip::normalizeTitle( "Web:{$testPage}" );
$this->cache_duration = 0; //disable cache for testing
Expand All @@ -55,4 +37,27 @@ function pick_page( $listPage, $purge = false, $testPage = false ) {

return $html;
}

private function fetchList( $listPage, $purge = false, $testPage = false ) {
$list = $this->rip_page( $listPage, 'raw', $purge );
if ( !$list && !$testPage ) {
return false;
}

$list = preg_replace( '@<!--.*?-->@s', '', $list );
$items = array();
preg_match_all( '/^\*+.*\[\[(.+?)( *\|.*?)?\]\]/m', $list, $items, PREG_SET_ORDER );
if ( !$items && !$testPage ) {
return false;
}

$list = array();

foreach ( $items as $item ) {
$list[] = WikiRip::normalizeTitle( $item[1] );
}

return $list;
}

}
90 changes: 90 additions & 0 deletions tests/phpunit/WikiBoxTest.php
@@ -0,0 +1,90 @@
<?php
require_once __DIR__ . "/../../inc/wikibox.class.php";

/**
* @covers WikiBox
*
* @licence GNU GPL v2+
* @author Kai Nissen
*/
class WikiBoxTest extends \PHPUnit_Framework_TestCase {

public function listProvider() {
return array(
array(
'* [[Web:Banner/Testbanner]]',
'Web:Banner/Testbanner'
),
array(
'# [[Web:Banner/Testbanner]]',
null
),
array(
<<<'NOW'
# [[Web:Banner/Testbanner]]
* [[Web:Banner/Some_other_banner]]
NOW
,
'Web:Banner/Some_other_banner'
),
);
}

/** @dataProvider listProvider */
public function testPickPageReturnsPageContent( $listContent, $expectedPageTitle ) {
$wikibox = $this->createNewWikiBox();

$wikibox->expects( $this->at( 0 ) )
->method( 'rip_page' )
->with( 'Web:Banner', 'raw' )
->willReturn( $listContent );

$wikibox->expects( $this->at( 1 ) )
->method( 'rip_page' )
->with( $expectedPageTitle, 'render' );

$wikibox->expects( $this->exactly( 2 ) )
->method( 'rip_page' );

$wikibox->pick_page( 'Web:Banner', true, false );
}

public function testWhenPassingTestPageTitleNotInListAndMarkedAsTest_pickPageReturnsPageContent() {
$wikibox = $this->createNewWikiBox();

$wikibox->expects( $this->any() )
->method( 'rip_page' )
->will( $this->onConsecutiveCalls( '', 'wikibox-test' ) );

$test = $wikibox->pick_page( 'Web:Banner', true, 'Web:Banner/Some_banner' );
$this->assertEquals( 'wikibox-test', $test );
}

public function testWhenPassingTestPageTitleNotInList_pickPageReturnsErrorMessage() {
$wikibox = $this->createNewWikiBox();

$wikibox->expects( $this->at( 0 ) )
->method( 'rip_page' )
->with( 'Web:Banner', 'raw' )
->willReturn( '' );

$wikibox->expects( $this->at( 1 ) )
->method( 'rip_page' )
->with( 'Web:Banner/Some_banner', 'render' )
->willReturn( 'page content' );

$test = $wikibox->pick_page( 'Web:Banner', true, 'Banner/Some_banner' );
$this->assertEquals(
'<i class="error">inactive feature page not marked with "wikibox-test": Web:Banner/Some_banner</i>',
$test
);
}

private function createNewWikiBox() {
return $this->getMockBuilder( 'WikiBox' )
->setConstructorArgs( array( 'test.url' ) )
->setMethods( array( 'rip_page' ) )
->getMock();
}

}

0 comments on commit b004036

Please sign in to comment.