From 08ad325702848661355cde4a650bc20df2a7fe32 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 12 May 2012 21:11:53 +0200 Subject: [PATCH] MDL-32960 put phpunit integration tests to one test suite and execute them first --- .../tests/advanced_test.php} | 193 +----------------- lib/phpunit/tests/basic_test.php | 155 ++++++++++++++ .../tests/fixtures/sample_dataset.csv | 0 .../tests/fixtures/sample_dataset.xml | 0 lib/phpunit/tests/generator_test.php | 88 ++++++++ phpunit.xml.dist | 3 + 6 files changed, 247 insertions(+), 192 deletions(-) rename lib/{tests/phpunit_test.php => phpunit/tests/advanced_test.php} (62%) create mode 100644 lib/phpunit/tests/basic_test.php rename lib/{ => phpunit}/tests/fixtures/sample_dataset.csv (100%) rename lib/{ => phpunit}/tests/fixtures/sample_dataset.xml (100%) create mode 100644 lib/phpunit/tests/generator_test.php diff --git a/lib/tests/phpunit_test.php b/lib/phpunit/tests/advanced_test.php similarity index 62% rename from lib/tests/phpunit_test.php rename to lib/phpunit/tests/advanced_test.php index d24a0d1e34b..e82164ddd86 100644 --- a/lib/tests/phpunit_test.php +++ b/lib/phpunit/tests/advanced_test.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * PHPUnit integration unit tests + * PHPUnit integration tests * * @package core * @category phpunit @@ -26,135 +26,6 @@ defined('MOODLE_INTERNAL') || die(); -/** - * Test basic_testcase extra features and PHPUnit Moodle integration. - * - * @package core - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class core_phpunit_basic_testcase extends basic_testcase { - - /** - * Tests that bootstrapping has occurred correctly - * @return void - */ - public function test_bootstrap() { - global $CFG; - $this->assertTrue(isset($CFG->httpswwwroot)); - $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot); - $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix); - } - - /** - * This is just a verification if I understand the PHPUnit assert docs right --skodak - * @return void - */ - public function test_assert_behaviour() { - // arrays - $a = array('a', 'b', 'c'); - $b = array('a', 'c', 'b'); - $c = array('a', 'b', 'c'); - $d = array('a', 'b', 'C'); - $this->assertNotEquals($a, $b); - $this->assertNotEquals($a, $d); - $this->assertEquals($a, $c); - $this->assertEquals($a, $b, '', 0, 10, true); - - // objects - $a = new stdClass(); - $a->x = 'x'; - $a->y = 'y'; - $b = new stdClass(); // switched order - $b->y = 'y'; - $b->x = 'x'; - $c = $a; - $d = new stdClass(); - $d->x = 'x'; - $d->y = 'y'; - $d->z = 'z'; - $this->assertEquals($a, $b); - $this->assertNotSame($a, $b); - $this->assertEquals($a, $c); - $this->assertSame($a, $c); - $this->assertNotEquals($a, $d); - - // string comparison - $this->assertEquals(1, '1'); - $this->assertEquals(null, ''); - - $this->assertNotEquals(1, '1 '); - $this->assertNotEquals(0, ''); - $this->assertNotEquals(null, '0'); - $this->assertNotEquals(array(), ''); - - // other comparison - $this->assertEquals(null, null); - $this->assertEquals(false, null); - $this->assertEquals(0, null); - - // emptiness - $this->assertEmpty(0); - $this->assertEmpty(0.0); - $this->assertEmpty(''); - $this->assertEmpty('0'); - $this->assertEmpty(false); - $this->assertEmpty(null); - $this->assertEmpty(array()); - - $this->assertNotEmpty(1); - $this->assertNotEmpty(0.1); - $this->assertNotEmpty(-1); - $this->assertNotEmpty(' '); - $this->assertNotEmpty('0 '); - $this->assertNotEmpty(true); - $this->assertNotEmpty(array(null)); - $this->assertNotEmpty(new stdClass()); - } - -// Uncomment following tests to see logging of unexpected changes in global state and database -/* - public function test_db_modification() { - global $DB; - $DB->set_field('user', 'confirmed', 1, array('id'=>-1)); - } - - public function test_cfg_modification() { - global $CFG; - $CFG->xx = 'yy'; - unset($CFG->admin); - $CFG->rolesactive = 0; - } - - public function test_user_modification() { - global $USER; - $USER->id = 10; - } - - public function test_course_modification() { - global $COURSE; - $COURSE->id = 10; - } - - public function test_all_modifications() { - global $DB, $CFG, $USER, $COURSE; - $DB->set_field('user', 'confirmed', 1, array('id'=>-1)); - $CFG->xx = 'yy'; - unset($CFG->admin); - $CFG->rolesactive = 0; - $USER->id = 10; - $COURSE->id = 10; - } - - public function test_transaction_problem() { - global $DB; - $DB->start_delegated_transaction(); - } -*/ -} - - /** * Test advanced_testcase extra features. * @@ -391,65 +262,3 @@ class core_phpunit_advanced_testcase extends advanced_testcase { $this->assertTrue($DB->record_exists('user', array('username'=>'onemore'))); } } - - -/** - * Test data generator - * - * @package core - * @category phpunit - * @copyright 2012 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class core_phpunit_generator_testcase extends advanced_testcase { - public function test_create() { - global $DB; - - $this->resetAfterTest(true); - $generator = $this->getDataGenerator(); - - $count = $DB->count_records('user'); - $user = $generator->create_user(); - $this->assertEquals($count+1, $DB->count_records('user')); - - $count = $DB->count_records('course_categories'); - $category = $generator->create_category(); - $this->assertEquals($count+1, $DB->count_records('course_categories')); - - $count = $DB->count_records('course'); - $course = $generator->create_course(); - $this->assertEquals($count+1, $DB->count_records('course')); - - $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3)); - $this->assertEquals($course->id, $section->course); - - $scale = $generator->create_scale(); - $this->assertNotEmpty($scale); - } - - public function test_create_module() { - global $CFG, $SITE; - if (!file_exists("$CFG->dirroot/mod/page/")) { - $this->markTestSkipped('Can not find standard Page module'); - } - - $this->resetAfterTest(true); - $generator = $this->getDataGenerator(); - - $page = $generator->create_module('page', array('course'=>$SITE->id)); - $this->assertNotEmpty($page); - } - - public function test_create_block() { - global $CFG; - if (!file_exists("$CFG->dirroot/blocks/online_users/")) { - $this->markTestSkipped('Can not find standard Online users block'); - } - - $this->resetAfterTest(true); - $generator = $this->getDataGenerator(); - - $page = $generator->create_block('online_users'); - $this->assertNotEmpty($page); - } -} diff --git a/lib/phpunit/tests/basic_test.php b/lib/phpunit/tests/basic_test.php new file mode 100644 index 00000000000..2ad03cb0d94 --- /dev/null +++ b/lib/phpunit/tests/basic_test.php @@ -0,0 +1,155 @@ +. + +/** + * PHPUnit integration tests + * + * @package core + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Test basic_testcase extra features and PHPUnit Moodle integration. + * + * @package core + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_phpunit_basic_testcase extends basic_testcase { + + /** + * Tests that bootstrapping has occurred correctly + * @return void + */ + public function test_bootstrap() { + global $CFG; + $this->assertTrue(isset($CFG->httpswwwroot)); + $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot); + $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix); + } + + /** + * This is just a verification if I understand the PHPUnit assert docs right --skodak + * @return void + */ + public function test_assert_behaviour() { + // arrays + $a = array('a', 'b', 'c'); + $b = array('a', 'c', 'b'); + $c = array('a', 'b', 'c'); + $d = array('a', 'b', 'C'); + $this->assertNotEquals($a, $b); + $this->assertNotEquals($a, $d); + $this->assertEquals($a, $c); + $this->assertEquals($a, $b, '', 0, 10, true); + + // objects + $a = new stdClass(); + $a->x = 'x'; + $a->y = 'y'; + $b = new stdClass(); // switched order + $b->y = 'y'; + $b->x = 'x'; + $c = $a; + $d = new stdClass(); + $d->x = 'x'; + $d->y = 'y'; + $d->z = 'z'; + $this->assertEquals($a, $b); + $this->assertNotSame($a, $b); + $this->assertEquals($a, $c); + $this->assertSame($a, $c); + $this->assertNotEquals($a, $d); + + // string comparison + $this->assertEquals(1, '1'); + $this->assertEquals(null, ''); + + $this->assertNotEquals(1, '1 '); + $this->assertNotEquals(0, ''); + $this->assertNotEquals(null, '0'); + $this->assertNotEquals(array(), ''); + + // other comparison + $this->assertEquals(null, null); + $this->assertEquals(false, null); + $this->assertEquals(0, null); + + // emptiness + $this->assertEmpty(0); + $this->assertEmpty(0.0); + $this->assertEmpty(''); + $this->assertEmpty('0'); + $this->assertEmpty(false); + $this->assertEmpty(null); + $this->assertEmpty(array()); + + $this->assertNotEmpty(1); + $this->assertNotEmpty(0.1); + $this->assertNotEmpty(-1); + $this->assertNotEmpty(' '); + $this->assertNotEmpty('0 '); + $this->assertNotEmpty(true); + $this->assertNotEmpty(array(null)); + $this->assertNotEmpty(new stdClass()); + } + +// Uncomment following tests to see logging of unexpected changes in global state and database + /* + public function test_db_modification() { + global $DB; + $DB->set_field('user', 'confirmed', 1, array('id'=>-1)); + } + + public function test_cfg_modification() { + global $CFG; + $CFG->xx = 'yy'; + unset($CFG->admin); + $CFG->rolesactive = 0; + } + + public function test_user_modification() { + global $USER; + $USER->id = 10; + } + + public function test_course_modification() { + global $COURSE; + $COURSE->id = 10; + } + + public function test_all_modifications() { + global $DB, $CFG, $USER, $COURSE; + $DB->set_field('user', 'confirmed', 1, array('id'=>-1)); + $CFG->xx = 'yy'; + unset($CFG->admin); + $CFG->rolesactive = 0; + $USER->id = 10; + $COURSE->id = 10; + } + + public function test_transaction_problem() { + global $DB; + $DB->start_delegated_transaction(); + } + */ +} diff --git a/lib/tests/fixtures/sample_dataset.csv b/lib/phpunit/tests/fixtures/sample_dataset.csv similarity index 100% rename from lib/tests/fixtures/sample_dataset.csv rename to lib/phpunit/tests/fixtures/sample_dataset.csv diff --git a/lib/tests/fixtures/sample_dataset.xml b/lib/phpunit/tests/fixtures/sample_dataset.xml similarity index 100% rename from lib/tests/fixtures/sample_dataset.xml rename to lib/phpunit/tests/fixtures/sample_dataset.xml diff --git a/lib/phpunit/tests/generator_test.php b/lib/phpunit/tests/generator_test.php new file mode 100644 index 00000000000..eb5df6ba0e4 --- /dev/null +++ b/lib/phpunit/tests/generator_test.php @@ -0,0 +1,88 @@ +. + +/** + * PHPUnit integration tests + * + * @package core + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Test data generator + * + * @package core + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_phpunit_generator_testcase extends advanced_testcase { + public function test_create() { + global $DB; + + $this->resetAfterTest(true); + $generator = $this->getDataGenerator(); + + $count = $DB->count_records('user'); + $user = $generator->create_user(); + $this->assertEquals($count+1, $DB->count_records('user')); + + $count = $DB->count_records('course_categories'); + $category = $generator->create_category(); + $this->assertEquals($count+1, $DB->count_records('course_categories')); + + $count = $DB->count_records('course'); + $course = $generator->create_course(); + $this->assertEquals($count+1, $DB->count_records('course')); + + $section = $generator->create_course_section(array('course'=>$course->id, 'section'=>3)); + $this->assertEquals($course->id, $section->course); + + $scale = $generator->create_scale(); + $this->assertNotEmpty($scale); + } + + public function test_create_module() { + global $CFG, $SITE; + if (!file_exists("$CFG->dirroot/mod/page/")) { + $this->markTestSkipped('Can not find standard Page module'); + } + + $this->resetAfterTest(true); + $generator = $this->getDataGenerator(); + + $page = $generator->create_module('page', array('course'=>$SITE->id)); + $this->assertNotEmpty($page); + } + + public function test_create_block() { + global $CFG; + if (!file_exists("$CFG->dirroot/blocks/online_users/")) { + $this->markTestSkipped('Can not find standard Online users block'); + } + + $this->resetAfterTest(true); + $generator = $this->getDataGenerator(); + + $page = $generator->create_block('online_users'); + $this->assertNotEmpty($page); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index be46f6bf505..82bf8fbaa04 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -20,6 +20,9 @@ + + lib/phpunit/tests + lib/ddl/tests lib/dml/tests