Permalink
Browse files

Merge pull request #1 from scribu/tests

Unit tests
  • Loading branch information...
Cristi Burcă
Cristi Burcă committed Feb 21, 2013
2 parents cdaaa06 + 6d4557c commit 8bfdae2f0be582fc9c88a0fb96da1c8c54103e50
Showing with 377 additions and 1 deletion.
  1. +2 −0 .gitignore
  2. +40 −0 .travis.yml
  3. +11 −0 composer.json
  4. +19 −0 phpunit.xml
  5. +1 −1 scb
  6. +7 −0 tests/bootstrap.php
  7. +297 −0 tests/test-forms.php
View
@@ -0,0 +1,2 @@
+composer.lock
+vendor/
View
@@ -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
View
@@ -0,0 +1,11 @@
+{
+ "license": "GPL2",
+ "require": {
+ "php": ">=5.2"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "3.7.x",
+ "fluentdom/fluentdom": "dev-master"
+ },
+ "minimum-stability": "dev"
+}
View
@@ -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>
2 scb
Submodule scb updated 1 files
+12 −3 Forms.php
View
@@ -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';
View
@@ -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 );
+ }
+}
+

0 comments on commit 8bfdae2

Please sign in to comment.