-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from scribu/tests
Unit tests
- Loading branch information
Showing
7 changed files
with
377 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
composer.lock | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
|
||
env: | ||
- WP_VERSION=master WP_MULTISITE=0 | ||
- WP_VERSION=3.4.2 WP_MULTISITE=0 | ||
- WP_VERSION=master WP_MULTISITE=1 | ||
- WP_VERSION=3.4.2 WP_MULTISITE=1 | ||
|
||
before_install: | ||
- git submodule update --init --recursive | ||
|
||
before_script: | ||
# set up WP install | ||
- WP_CORE_DIR=/tmp/wordpress/ | ||
- wget -nv -O /tmp/wordpress.tar.gz https://github.com/WordPress/WordPress/tarball/$WP_VERSION | ||
- mkdir -p $WP_CORE_DIR | ||
- tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR | ||
- plugin_slug=$(basename $(pwd)) | ||
- plugin_dir=$WP_CORE_DIR/wp-content/plugins/$plugin_slug | ||
- cd .. | ||
- mv $plugin_slug $plugin_dir | ||
# set up testing suite | ||
- export WP_TESTS_DIR=/tmp/wordpress-tests/ | ||
- svn co --ignore-externals http://unit-tests.svn.wordpress.org/trunk/ $WP_TESTS_DIR | ||
- cd $WP_TESTS_DIR | ||
- cp wp-tests-config-sample.php wp-tests-config.php | ||
- sed -i "s:dirname( __FILE__ ) . '/wordpress/':'$WP_CORE_DIR':" wp-tests-config.php | ||
- sed -i "s/yourdbnamehere/wordpress_test/" wp-tests-config.php | ||
- sed -i "s/yourusernamehere/root/" wp-tests-config.php | ||
- sed -i "s/yourpasswordhere//" wp-tests-config.php | ||
# set up database | ||
- mysql -e 'CREATE DATABASE wordpress_test;' -uroot | ||
# prepare for running the tests | ||
- cd $plugin_dir | ||
|
||
script: phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"license": "GPL2", | ||
"require": { | ||
"php": ">=5.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "3.7.x", | ||
"fluentdom/fluentdom": "dev-master" | ||
}, | ||
"minimum-stability": "dev" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<phpunit | ||
bootstrap="tests/bootstrap.php" | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
<testsuites> | ||
<testsuite> | ||
<directory prefix="test-" suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./scb</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
$GLOBALS['wp_tests_options'] = array( | ||
'active_plugins' => array( basename( dirname( dirname( __FILE__ ) ) ) . '/plugin.php' ), | ||
); | ||
|
||
require getenv( 'WP_TESTS_DIR' ) . '/includes/bootstrap.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,297 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/fluentdom/fluentdom/src/FluentDOM.php'; | ||
|
||
class FormsTest extends PHPUnit_Framework_TestCase { | ||
|
||
function testTextInput() { | ||
$fd = self::domify( scbForms::input( array( | ||
'name' => array( 'foo', 'bar', 'baz' ), | ||
'type' => 'text', | ||
'value' => '<em>foobar</em>' | ||
) ) ); | ||
|
||
$el = $fd->find('//input'); | ||
|
||
$this->assertEquals( 'foo[bar][baz]', $el->attr('name') ); | ||
$this->assertEquals( '<em>foobar</em>', $el->attr('value') ); | ||
} | ||
|
||
function testInputWithValue() { | ||
$data = array( | ||
'foo' => array( | ||
'bar' => 42 | ||
), | ||
'bar' => 'wrong' | ||
); | ||
|
||
$output = scbForms::input( array( | ||
'name' => array( 'foo', 'bar' ), | ||
'type' => 'text', | ||
'value' => '<em>foobar</em>' | ||
), $data ); | ||
|
||
$el = self::domify( $output )->find('//input'); | ||
|
||
$this->assertEquals( 'foo[bar]', $el->attr('name') ); | ||
$this->assertEquals( 42, $el->attr('value') ); | ||
} | ||
|
||
function testTextarea() { | ||
$fd = self::domify( scbForms::input( array( | ||
'name' => __FUNCTION__, | ||
'type' => 'textarea', | ||
'value' => '<em>foobar</em>' | ||
) ) ); | ||
|
||
$el = $fd->find('//textarea'); | ||
|
||
$this->assertEquals( __FUNCTION__, $el->attr('name') ); | ||
$this->assertEquals( '<em>foobar</em>', $el->text() ); | ||
} | ||
|
||
function testRadio() { | ||
$choices = array( 'foo', 'bar' ); | ||
|
||
$fd = self::domify( scbForms::input( array( | ||
'name' => __FUNCTION__, | ||
'type' => 'radio', | ||
'choices' => $choices, | ||
) ) ); | ||
|
||
$labels = $fd->find('//label'); | ||
|
||
$this->assertCount( count( $choices ), $labels ); | ||
|
||
foreach ( $labels as $i => $label ) { | ||
$label = FluentDOM( $label ); | ||
|
||
$el = $label->find('.//input[@type="radio"]'); | ||
|
||
$this->assertEquals( __FUNCTION__, $el->attr('name') ); | ||
|
||
$this->assertEquals( $choices[ $i ], $el->attr('value') ); | ||
|
||
$this->assertEquals( ' ' . $choices[ $i ], $label->text() ); | ||
} | ||
|
||
// should always have the first radio checked | ||
$checked = $fd->find('//input[@checked]'); | ||
|
||
$this->assertCount( 1, $checked ); | ||
|
||
$this->assertEquals( $choices[0], $checked->attr('value') ); | ||
} | ||
|
||
function testSingleCheckbox() { | ||
$output = scbForms::input( array( | ||
'name' => 'fruit', | ||
'type' => 'checkbox', | ||
'value' => 'orange', | ||
'desc' => 'Orange' | ||
) ); | ||
|
||
$label = self::domify( $output )->find('//label'); | ||
|
||
$this->assertEquals( ' Orange', $label->text() ); | ||
|
||
$el = $label->find('.//input[@type="checkbox"]'); | ||
|
||
$this->assertEquals( 'fruit', $el->attr('name') ); | ||
|
||
$this->assertEquals( 'orange', $el->attr('value') ); | ||
|
||
$this->assertEmpty( $el->attr( 'checked' ) ); | ||
} | ||
|
||
function testSingleCheckboxWithValue() { | ||
$args = array( | ||
'name' => 'fruit', | ||
'type' => 'checkbox', | ||
'desc' => 'Orange' | ||
); | ||
|
||
$output = scbForms::input_with_value( $args, false ); | ||
|
||
$el = self::domify( $output )->find('//input[@type="checkbox"]'); | ||
|
||
$this->assertEmpty( $el->attr( 'checked' ) ); | ||
|
||
$output = scbForms::input_with_value( $args, true ); | ||
|
||
$el = self::domify( $output )->find('//input[@type="checkbox"]'); | ||
|
||
$this->assertNotEmpty( $el->attr( 'checked' ) ); | ||
} | ||
|
||
function testCheckbox() { | ||
$choices = array( 'foo', 'bar' ); | ||
|
||
$args = array( | ||
'name' => array( 'maxi', 'pads' ), | ||
'type' => 'checkbox', | ||
'choices' => $choices | ||
); | ||
|
||
$fd = self::domify( scbForms::input( $args ) ); | ||
|
||
$labels = $fd->find('//label'); | ||
|
||
$this->assertCount( count( $choices ), $labels ); | ||
|
||
foreach ( $labels as $i => $label ) { | ||
$label = FluentDOM( $label ); | ||
|
||
$el = $label->find('.//input[@type="checkbox"]'); | ||
|
||
$this->assertEquals( 'maxi[pads][]', $el->attr('name') ); | ||
|
||
$this->assertEquals( $choices[ $i ], $el->attr('value') ); | ||
|
||
$this->assertEmpty( $el->attr( 'checked' ) ); | ||
|
||
$this->assertEquals( ' ' . $choices[ $i ], $label->text() ); | ||
} | ||
} | ||
|
||
function testCheckboxWithValues() { | ||
$selected = array( 'a', 'c' ); | ||
$choices = array( 'a', 'b', 'c', 'd' ); | ||
|
||
$output = scbForms::input_with_value( array( | ||
'name' => __FUNCTION__, | ||
'type' => 'checkbox', | ||
'choices' => $choices | ||
), $selected ); | ||
|
||
$labels = self::domify( $output )->find('//label'); | ||
|
||
$this->assertCount( count( $choices ), $labels ); | ||
|
||
foreach ( $labels as $i => $label ) { | ||
$value = $choices[ $i ]; | ||
|
||
$label = FluentDOM( $label ); | ||
|
||
$el = $label->find('.//input[@type="checkbox"]'); | ||
|
||
$this->assertEquals( __FUNCTION__ . '[]', $el->attr('name') ); | ||
|
||
$this->assertEquals( $value, $el->attr('value') ); | ||
|
||
if ( in_array( $value, $selected ) ) | ||
$this->assertNotEmpty( $el->attr( 'checked' ) ); | ||
else | ||
$this->assertEmpty( $el->attr( 'checked' ) ); | ||
|
||
$this->assertEquals( ' ' . $value, $label->text() ); | ||
} | ||
} | ||
|
||
function testSelect() { | ||
$choices = array( | ||
'green' => 'Green', | ||
'blue' => 'Blue', | ||
'white' => 'White' | ||
); | ||
|
||
$args = array( | ||
'name' => __FUNCTION__, | ||
'type' => 'select', | ||
'choices' => $choices, | ||
'desc' => 'Some extra text' | ||
); | ||
|
||
$label = self::domify( scbForms::input( $args ) )->find('//label'); | ||
|
||
$this->assertStringEndsWith( $args['desc'], $label->text() ); | ||
|
||
$options = $label->find('.//select/option'); | ||
|
||
$this->assertCount( count( $choices ), $options ); | ||
|
||
foreach ( $options as $option ) { | ||
$el = FluentDOM( $option ); | ||
|
||
list( $value, $title ) = each( $choices ); | ||
|
||
$this->assertEquals( $value, $el->attr('value') ); | ||
|
||
$this->assertEquals( $title, $el->text() ); | ||
|
||
$this->assertEmpty( $el->attr( 'selected' ) ); | ||
} | ||
} | ||
|
||
function testSelectWithText() { | ||
$choices = array( | ||
'green' => 'Green', | ||
'blue' => 'Blue', | ||
'white' => 'White' | ||
); | ||
|
||
$args = array( | ||
'name' => __FUNCTION__, | ||
'type' => 'select', | ||
'choices' => $choices, | ||
'text' => 'Enter a color', | ||
'desc' => 'Some extra text', | ||
); | ||
|
||
$label = self::domify( scbForms::input( $args ) )->find('//label'); | ||
|
||
$this->assertStringEndsWith( $args['desc'], $label->text() ); | ||
|
||
$options = $label->find('.//select/option'); | ||
|
||
$this->assertCount( count( $choices ) + 1, $options ); | ||
|
||
$first_option = FluentDOM( $options->item(0) ); | ||
|
||
$this->assertEquals( $args['text'], $first_option->text() ); | ||
|
||
$this->assertEmpty( $first_option->attr('value') ); | ||
|
||
$this->assertNotEmpty( $first_option->attr('selected') ); | ||
} | ||
|
||
function testSelectWithValue() { | ||
$choices = array( | ||
'1/2', | ||
'1', | ||
'1 1/3', | ||
); | ||
|
||
$args = array( | ||
'name' => __FUNCTION__, | ||
'type' => 'select', | ||
'choices' => $choices, | ||
); | ||
|
||
$output = scbForms::input_with_value( $args, '1 1/3' ); | ||
|
||
$label = self::domify( $output )->find('//label'); | ||
|
||
$selected = $label->find('.//select/option[@selected]'); | ||
|
||
$this->assertCount( 1, $selected ); | ||
|
||
$this->assertEquals( '1 1/3', $selected->attr('value') ); | ||
} | ||
|
||
private static function domify( $str ) { | ||
$fd = new FluentDOM; | ||
|
||
$xml = <<<XML | ||
<html> | ||
<head></head> | ||
<body> | ||
$str | ||
</body> | ||
</html> | ||
XML; | ||
|
||
return FluentDOM( $xml ); | ||
} | ||
} | ||
|