[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpunit/tests/ -> basic_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * PHPUnit integration tests
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Test basic_testcase extra features and PHPUnit Moodle integration.
  31   *
  32   * @package    core
  33   * @category   phpunit
  34   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_phpunit_basic_testcase extends basic_testcase {
  38      protected $testassertexecuted = false;
  39  
  40      protected function setUp() {
  41          parent::setUp();
  42          if ($this->getName() === 'test_setup_assert') {
  43              $this->assertTrue(true);
  44              $this->testassertexecuted = true;
  45              return;
  46          }
  47      }
  48  
  49      /**
  50       * Tests that bootstrapping has occurred correctly
  51       * @return void
  52       */
  53      public function test_bootstrap() {
  54          global $CFG;
  55          $this->assertTrue(isset($CFG->httpswwwroot));
  56          $this->assertEquals($CFG->httpswwwroot, $CFG->wwwroot);
  57          $this->assertEquals($CFG->prefix, $CFG->phpunit_prefix);
  58      }
  59  
  60      /**
  61       * This is just a verification if I understand the PHPUnit assert docs right --skodak
  62       * @return void
  63       */
  64      public function test_assert_behaviour() {
  65          // Arrays.
  66          $a = array('a', 'b', 'c');
  67          $b = array('a', 'c', 'b');
  68          $c = array('a', 'b', 'c');
  69          $d = array('a', 'b', 'C');
  70          $this->assertNotEquals($a, $b);
  71          $this->assertNotEquals($a, $d);
  72          $this->assertEquals($a, $c);
  73          $this->assertEquals($a, $b, '', 0, 10, true);
  74  
  75          // Objects.
  76          $a = new stdClass();
  77          $a->x = 'x';
  78          $a->y = 'y';
  79          $b = new stdClass(); // Switched order.
  80          $b->y = 'y';
  81          $b->x = 'x';
  82          $c = $a;
  83          $d = new stdClass();
  84          $d->x = 'x';
  85          $d->y = 'y';
  86          $d->z = 'z';
  87          $this->assertEquals($a, $b);
  88          $this->assertNotSame($a, $b);
  89          $this->assertEquals($a, $c);
  90          $this->assertSame($a, $c);
  91          $this->assertNotEquals($a, $d);
  92  
  93          // String comparison.
  94          $this->assertEquals(1, '1');
  95          $this->assertEquals(null, '');
  96  
  97          $this->assertNotEquals(1, '1 ');
  98          $this->assertNotEquals(0, '');
  99          $this->assertNotEquals(null, '0');
 100          $this->assertNotEquals(array(), '');
 101  
 102          // Other comparison.
 103          $this->assertEquals(null, null);
 104          $this->assertEquals(false, null);
 105          $this->assertEquals(0, null);
 106  
 107          // Emptiness.
 108          $this->assertEmpty(0);
 109          $this->assertEmpty(0.0);
 110          $this->assertEmpty('');
 111          $this->assertEmpty('0');
 112          $this->assertEmpty(false);
 113          $this->assertEmpty(null);
 114          $this->assertEmpty(array());
 115  
 116          $this->assertNotEmpty(1);
 117          $this->assertNotEmpty(0.1);
 118          $this->assertNotEmpty(-1);
 119          $this->assertNotEmpty(' ');
 120          $this->assertNotEmpty('0 ');
 121          $this->assertNotEmpty(true);
 122          $this->assertNotEmpty(array(null));
 123          $this->assertNotEmpty(new stdClass());
 124      }
 125  
 126      /**
 127       * Make sure there are no sloppy Windows line endings
 128       * that would break our tests.
 129       */
 130      public function test_lineendings() {
 131          $string = <<<STRING
 132  a
 133  b
 134  STRING;
 135          $this->assertSame("a\nb", $string, 'Make sure all project files are checked out with unix line endings.');
 136  
 137      }
 138  
 139      /**
 140       * Make sure asserts in setUp() do not create problems.
 141       */
 142      public function test_setup_assert() {
 143          $this->assertTrue($this->testassertexecuted);
 144          $this->testassertexecuted = false;
 145      }
 146  
 147      // Uncomment following tests to see logging of unexpected changes in global state and database.
 148      /*
 149          public function test_db_modification() {
 150              global $DB;
 151              $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
 152          }
 153  
 154          public function test_cfg_modification() {
 155              global $CFG;
 156              $CFG->xx = 'yy';
 157              unset($CFG->admin);
 158              $CFG->rolesactive = 0;
 159          }
 160  
 161          public function test_user_modification() {
 162              global $USER;
 163              $USER->id = 10;
 164          }
 165  
 166          public function test_course_modification() {
 167              global $COURSE;
 168              $COURSE->id = 10;
 169          }
 170  
 171          public function test_all_modifications() {
 172              global $DB, $CFG, $USER, $COURSE;
 173              $DB->set_field('user', 'confirmed', 1, array('id'=>-1));
 174              $CFG->xx = 'yy';
 175              unset($CFG->admin);
 176              $CFG->rolesactive = 0;
 177              $USER->id = 10;
 178              $COURSE->id = 10;
 179          }
 180  
 181          public function test_transaction_problem() {
 182              global $DB;
 183              $DB->start_delegated_transaction();
 184          }
 185      */
 186  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1