Skip to content

Commit

Permalink
Add tests, remove hook functionality
Browse files Browse the repository at this point in the history
At some point, we might add hooks again with a better consideration
on how to actually add them in a sustainable way. Adding templates
manually is not going to scale.
  • Loading branch information
mooeypoo committed Dec 30, 2018
1 parent 4d8444d commit 2acb345
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 54 deletions.
46 changes: 4 additions & 42 deletions includes/ExtensionDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ public function setLicense( $license ) {
}
}

public function getLicense() {
return $this->license;
}

public function setDevEnv( $isPhp, $isJs ) {
$this->devEnvironment[ 'php' ] = (bool)$isPhp;
$this->devEnvironment[ 'js' ] = (bool)$isJs;
Expand Down Expand Up @@ -126,6 +122,10 @@ public function isEnvironment( $env ) {
);
}

public function getLicense() {
return $this->license;
}

public function hasSpecialPage() {
return ( (bool) $this->specialName );
}
Expand All @@ -134,22 +134,6 @@ public function getSpecialPageClassName() {
return 'Special' . str_replace( ' ', '_', $this->specialName );
}

public function getHooks() {
return $this->hooks;
}

protected function sanitizeHookFunctionName( $hook ) {
$hook = str_replace( '::', ':', $hook );
$parts = explode( ':', $hook );
$newHook = '';

foreach ( $parts as $p ) {
$newHook .= ucfirst( $p );
}

return $newHook;
}

public function getAllParams() {
$params = array(
'name' => $this->name,
Expand All @@ -176,13 +160,6 @@ public function getAllParams() {
),
);

if ( count( $this->getHooks() ) > 0 ) {
$params[ 'hooks' ] = [];

foreach ( $this->getHooks() as $hook ) {
$params[ 'hooks' ][] = $hook;
}
}
return $params;
}

Expand Down Expand Up @@ -251,21 +228,6 @@ public function getExtensionJson( $outputAsString = false ) {
$json[ 'AutoloadClasses' ][ $this->getSpecialPageClassName() ] = 'specials/' . $this->getSpecialPageClassName() . '.php';
}

// Hooks
$hookClassName = $this->getName() . 'Hooks';
if ( $this->isEnvironment( 'js' ) || count( $this->getHooks() ) ) {
$json[ 'AutoloadClasses' ][ $hookClassName ] = 'Hooks.php';
}

// Hook list
if ( count( $this->getHooks() ) > 0 ) {
$json[ 'Hooks' ] = [];

foreach ( $this->getHooks() as $hook ) {
$json[ 'Hooks' ][ $hook ] = [ $hookClassName . '::on' . $this->sanitizeHookFunctionName( $hook ) ];
}

}
if ( $this->isEnvironment( 'js' ) ) {
if ( !isset( $json[ 'Hooks' ] ) ) {
$json[ 'Hooks' ] = [];
Expand Down
11 changes: 0 additions & 11 deletions includes/Templating.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,4 @@ public function render( $templateName, $data = array() ) {

return $this->twig->render( $filename, $data );
}

public function clearCache() {
// Twig no longer clears cache on its own
// We'll have to clear it manually
$files = glob( dirname( __DIR__ ) . '/' . $this->cacheDir . '/*' );
foreach( $files as $file ) {
if ( is_file( $file ) ) {
// unlink( $file );
}
}
}
}
36 changes: 36 additions & 0 deletions tests/ExtensionDetailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use MWStew\Builder\ExtensionDetails;
use PHPUnit\Framework\TestCase;

class ExtensionDetailsTest extends TestCase {
public function testGetters() {
$data = [
'name' => 'FooBar',
'title' => 'Foo bar',
'author' => 'Moe Schmoe',
'version' => '1.2.3',
'description' => 'A test description',
'url' => 'http://www.example.com/foobar',
'license' => 'MIT',
'dev_php' => true,
'dev_js' => true,
'specialpage_name' => 'Foobar',
'specialpage_title' => 'Get the foobar',
'specialpage_intro' => 'A great new special foobar page.'
];

$details = new ExtensionDetails( $data );

$this->assertEquals( 'FooBar', $details->getName() );
$this->assertEquals( 'fooBar', $details->getLowerCamelName() );
$this->assertEquals( 'FooBar', $details->getClassName() );
$this->assertEquals( true, $details->isEnvironment( 'js' ) );
$this->assertEquals( true, $details->isEnvironment( 'php' ) );
$this->assertEquals( 'MIT', $details->getLicense() );
$this->assertEquals( true, $details->hasSpecialPage() );
$this->assertEquals( 'SpecialFoobar', $details->getSpecialPageClassName() );
$this->assertEquals( 'special-foobar', $details->getSpecialPageKeyFormat() );
}

}
109 changes: 108 additions & 1 deletion tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,90 @@ public function testGeneratorTools() {
'Nonexisting set of keys'
);
}
public function testFileList() {
$cases = [
[
'data' => [ 'name' => 'testName' ],
'msg' => 'Name only',
'expected' => [
'extension.json',
'i18n/en.json',
'i18n/qqq.json'
]
],
[
'data' => [ 'name' => 'testName', 'specialpage_name' => 'Foobar' ],
'msg' => 'Special page',
'expected' => [
'extension.json',
'i18n/en.json',
'i18n/qqq.json',
'specials/SpecialFoobar.php',
'testName.alias.php',
]
],
[
'data' => [ 'name' => 'testName', 'dev_js' => true ],
'msg' => 'JS development environment',
'expected' => [
'extension.json',
'i18n/en.json',
'i18n/qqq.json',
'.eslintrc.json',
'.stylelintrc',
'Gruntfile.js',
'package.json',
'modules/ext.testName.js',
'modules/ext.testName.css',
'tests/testName.test.js',
'tests/.eslintrc.json',
]
],
[
'data' => [ 'name' => 'testName', 'dev_php' => true ],
'msg' => 'PHP development environment',
'expected' => [
'extension.json',
'i18n/en.json',
'i18n/qqq.json',
'composer.json',
'tests/testName.test.php',
]
],
[
'data' => [ 'name' => 'testName', 'dev_js' => true, 'dev_php' => true ],
'msg' => 'PHP and JS development environments',
'expected' => [
'extension.json',
'i18n/en.json',
'i18n/qqq.json',
'.eslintrc.json',
'.stylelintrc',
'Gruntfile.js',
'package.json',
'modules/ext.testName.js',
'modules/ext.testName.css',
'tests/testName.test.js',
'tests/.eslintrc.json',
'composer.json',
'tests/testName.test.php',
]
],
];

foreach ( $cases as $testCase ) {
$generator = new Generator( $testCase['data'] );
$files = $generator->getFiles();

$this->assertEquals(
$testCase[ 'expected' ],
array_keys( $files ),
$testCase[ 'msg' ]
);
}
}

public function testFiles() {
public function testFileContents() {
$cases = [
[
'data' => [ 'name' => 'testName' ],
Expand All @@ -108,6 +190,31 @@ public function testFiles() {
],
],
],
[
'data' => [ 'name' => 'testName', 'url' => 'http://www.demo.com/testURL' ],
'msg' => 'Giving name and URL',
'config' => [],
'expectedFileCount' => 3,
'expectedFiles' => [
'extension.json' => [
'name' => 'testName',
'namemsg' => 'testName',
'descriptionmsg' => 'testName-desc',
'MessagesDirs' => [
'testName' => [ 'i18n' ]
],
'url' => 'http://www.demo.com/testURL'
],
'i18n/en.json' => [
'testName' => 'testName',
'testName-desc' => '',
],
'i18n/qqq.json' => [
'testName' => 'The name of the extension',
'testName-desc' => '{{desc|name=testName|url=http://www.demo.com/testURL}}',
],
],
],
[
'data' => [ 'name' => 'secondTest', 'title' => 'Some random extension' ],
'msg' => 'Giving name and display name',
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory>../includes</directory>
<exclude>
<file>../includes/Zipper.php</file>
</exclude>
</whitelist>
</filter>
<logging>
Expand Down

0 comments on commit 2acb345

Please sign in to comment.