Best Phpunit code snippet using TestSuite.setBackupGlobals
TestSuite.php
Source:TestSuite.php
...452 $_test->setPreserveGlobalState($preserveGlobalState);453 }454 }455 if ($backupSettings['backupGlobals'] !== NULL) {456 $_test->setBackupGlobals(457 $backupSettings['backupGlobals']458 );459 }460 if ($backupSettings['backupStaticAttributes'] !== NULL) {461 $_test->setBackupStaticAttributes(462 $backupSettings['backupStaticAttributes']463 );464 }465 $test->addTest($_test, $groups);466 }467 }468 }469 else {470 $test = new $className;471 }472 }473 }474 if (!isset($test)) {475 throw new PHPUnit_Framework_Exception('No valid test provided.');476 }477 if ($test instanceof PHPUnit_Framework_TestCase) {478 $test->setName($name);479 if ($runTestInSeparateProcess) {480 $test->setRunTestInSeparateProcess(TRUE);481 if ($preserveGlobalState !== NULL) {482 $test->setPreserveGlobalState($preserveGlobalState);483 }484 }485 if ($backupSettings['backupGlobals'] !== NULL) {486 $test->setBackupGlobals($backupSettings['backupGlobals']);487 }488 if ($backupSettings['backupStaticAttributes'] !== NULL) {489 $test->setBackupStaticAttributes(490 $backupSettings['backupStaticAttributes']491 );492 }493 }494 return $test;495 }496 /**497 * Creates a default TestResult object.498 *499 * @return PHPUnit_Framework_TestResult500 */501 protected function createResult()502 {503 return new PHPUnit_Framework_TestResult;504 }505 /**506 * Returns the name of the suite.507 *508 * @return string509 */510 public function getName()511 {512 return $this->name;513 }514 /**515 * Returns the test groups of the suite.516 *517 * @return array518 * @since Method available since Release 3.2.0519 */520 public function getGroups()521 {522 return array_keys($this->groups);523 }524 /**525 * Runs the tests and collects their result in a TestResult.526 *527 * @param PHPUnit_Framework_TestResult $result528 * @param mixed $filter529 * @param array $groups530 * @param array $excludeGroups531 * @param boolean $processIsolation532 * @return PHPUnit_Framework_TestResult533 * @throws PHPUnit_Framework_Exception534 */535 public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE)536 {537 if ($result === NULL) {538 $result = $this->createResult();539 }540 $result->startTestSuite($this);541 $doSetup = TRUE;542 if (!empty($excludeGroups)) {543 foreach ($this->groups as $_group => $_tests) {544 if (in_array($_group, $excludeGroups) &&545 count($_tests) == count($this->tests)) {546 $doSetup = FALSE;547 }548 }549 }550 if ($doSetup) {551 try {552 $this->setUp();553 if ($this->testCase &&554 // Some extensions use test names that are not classes;555 // The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.556 class_exists($this->name, false) &&557 method_exists($this->name, 'setUpBeforeClass')) {558 call_user_func(array($this->name, 'setUpBeforeClass'));559 }560 }561 catch (PHPUnit_Framework_SkippedTestSuiteError $e) {562 $numTests = count($this);563 for ($i = 0; $i < $numTests; $i++) {564 $result->addFailure($this, $e, 0);565 }566 return $result;567 }568 catch (Exception $e) {569 $numTests = count($this);570 for ($i = 0; $i < $numTests; $i++) {571 $result->addError($this, $e, 0);572 }573 return $result;574 }575 }576 if (empty($groups)) {577 $tests = $this->tests;578 } else {579 $tests = new SplObjectStorage;580 foreach ($groups as $group) {581 if (isset($this->groups[$group])) {582 foreach ($this->groups[$group] as $test) {583 $tests->attach($test);584 }585 }586 }587 }588 foreach ($tests as $test) {589 if ($result->shouldStop()) {590 break;591 }592 if ($test instanceof PHPUnit_Framework_TestSuite) {593 $test->setBackupGlobals($this->backupGlobals);594 $test->setBackupStaticAttributes($this->backupStaticAttributes);595 $test->run(596 $result, $filter, $groups, $excludeGroups, $processIsolation597 );598 } else {599 $runTest = TRUE;600 if ($filter !== FALSE ) {601 $tmp = PHPUnit_Util_Test::describe($test, FALSE);602 if ($tmp[0] != '') {603 $name = join('::', $tmp);604 } else {605 $name = $tmp[1];606 }607 if (preg_match($filter, $name) == 0) {608 $runTest = FALSE;609 }610 }611 if ($runTest && !empty($excludeGroups)) {612 foreach ($this->groups as $_group => $_tests) {613 if (in_array($_group, $excludeGroups)) {614 foreach ($_tests as $_test) {615 if ($test === $_test) {616 $runTest = FALSE;617 break 2;618 }619 }620 }621 }622 }623 if ($runTest) {624 if ($test instanceof PHPUnit_Framework_TestCase) {625 $test->setBackupGlobals($this->backupGlobals);626 $test->setBackupStaticAttributes(627 $this->backupStaticAttributes628 );629 $test->setRunTestInSeparateProcess($processIsolation);630 }631 $this->runTest($test, $result);632 }633 }634 }635 if ($doSetup) {636 if ($this->testCase &&637 // Some extensions use test names that are not classes;638 // The method_exists() triggers an autoload call that causes issues with die()ing autoloaders.639 class_exists($this->name, false) &&640 method_exists($this->name, 'tearDownAfterClass')) {641 call_user_func(array($this->name, 'tearDownAfterClass'));642 }643 $this->tearDown();644 }645 $result->endTestSuite($this);646 return $result;647 }648 /**649 * Runs a test.650 *651 * @param PHPUnit_Framework_Test $test652 * @param PHPUnit_Framework_TestResult $result653 */654 public function runTest(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result)655 {656 $test->run($result);657 }658 /**659 * Sets the name of the suite.660 *661 * @param string662 */663 public function setName($name)664 {665 $this->name = $name;666 }667 /**668 * Returns the test at the given index.669 *670 * @param integer671 * @return PHPUnit_Framework_Test672 */673 public function testAt($index)674 {675 if (isset($this->tests[$index])) {676 return $this->tests[$index];677 } else {678 return FALSE;679 }680 }681 /**682 * Returns the tests as an enumeration.683 *684 * @return array685 */686 public function tests()687 {688 return $this->tests;689 }690 /**691 * Mark the test suite as skipped.692 *693 * @param string $message694 * @throws PHPUnit_Framework_SkippedTestSuiteError695 * @since Method available since Release 3.0.0696 */697 public function markTestSuiteSkipped($message = '')698 {699 throw new PHPUnit_Framework_SkippedTestSuiteError($message);700 }701 /**702 * @param ReflectionClass $class703 * @param ReflectionMethod $method704 */705 protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method)706 {707 $name = $method->getName();708 if ($this->isPublicTestMethod($method)) {709 $test = self::createTest($class, $name);710 if ($test instanceof PHPUnit_Framework_TestCase ||711 $test instanceof PHPUnit_Framework_TestSuite_DataProvider) {712 $test->setDependencies(713 PHPUnit_Util_Test::getDependencies($class->getName(), $name)714 );715 }716 $this->addTest($test, PHPUnit_Util_Test::getGroups(717 $class->getName(), $name)718 );719 }720 else if ($this->isTestMethod($method)) {721 $this->addTest(722 self::warning(723 sprintf(724 'Test method "%s" in test class "%s" is not public.',725 $name,726 $class->getName()727 )728 )729 );730 }731 }732 /**733 * @param ReflectionMethod $method734 * @return boolean735 */736 public static function isPublicTestMethod(ReflectionMethod $method)737 {738 return (self::isTestMethod($method) && $method->isPublic());739 }740 /**741 * @param ReflectionMethod $method742 * @return boolean743 */744 public static function isTestMethod(ReflectionMethod $method)745 {746 if (strpos($method->name, 'test') === 0) {747 return TRUE;748 }749 // @scenario on TestCase::testMethod()750 // @test on TestCase::testMethod()751 return strpos($method->getDocComment(), '@test') !== FALSE ||752 strpos($method->getDocComment(), '@scenario') !== FALSE;753 }754 /**755 * @param string $message756 * @return PHPUnit_Framework_Warning757 */758 protected static function warning($message)759 {760 return new PHPUnit_Framework_Warning($message);761 }762 /**763 * @param boolean $backupGlobals764 * @since Method available since Release 3.3.0765 */766 public function setBackupGlobals($backupGlobals)767 {768 if (is_null($this->backupGlobals) && is_bool($backupGlobals)) {769 $this->backupGlobals = $backupGlobals;770 }771 }772 /**773 * @param boolean $backupStaticAttributes774 * @since Method available since Release 3.4.0775 */776 public function setBackupStaticAttributes($backupStaticAttributes)777 {778 if (is_null($this->backupStaticAttributes) &&779 is_bool($backupStaticAttributes)) {780 $this->backupStaticAttributes = $backupStaticAttributes;...
setBackupGlobals
Using AI Code Generation
1require_once 'PHPUnit/Framework/TestSuite.php';2require_once 'PHPUnit/TextUI/TestRunner.php';3require_once 'PHPUnit/Extensions/PhptTestCase.php';4$suite = new PHPUnit_Framework_TestSuite('PHPUnit_Extensions_PhptTestCase');5$suite->setBackupGlobals(true);6PHPUnit_TextUI_TestRunner::run($suite);7require_once 'PHPUnit/Framework/TestSuite.php';8require_once 'PHPUnit/TextUI/TestRunner.php';9require_once 'PHPUnit/Extensions/PhptTestCase.php';10$suite = new PHPUnit_Framework_TestSuite('PHPUnit_Extensions_PhptTestCase');11$suite->setBackupGlobals(false);12PHPUnit_TextUI_TestRunner::run($suite);13require_once 'PHPUnit/Framework/TestSuite.php';14require_once 'PHPUnit/TextUI/TestRunner.php';15require_once 'PHPUnit/Extensions/PhptTestCase.php';16$suite = new PHPUnit_Framework_TestSuite('PHPUnit_Extensions_PhptTestCase');17PHPUnit_TextUI_TestRunner::run($suite);18I think this is expected behavior. The global variables are backed up when the test suite is run, not for each test case. (I'm not sure how the backup is implemented, but I think this is the case.)19The global variables are backed up when the test suite is run, not for each test case. (I'm not sure how the backup is implemented, but I think this is the case.)20I think this is expected behavior. The global variables are backed up when the test suite is run, not for each test case. (I'm not sure how the backup is implemented, but I think this is the case.)
setBackupGlobals
Using AI Code Generation
1$suite = new PHPUnit_Framework_TestSuite('AllTests');2$suite->setBackupGlobals(false);3PHPUnit_TextUI_TestRunner::run($suite);4$suite = new PHPUnit_Framework_TestSuite('AllTests');5$suite->setBackupGlobals(true);6PHPUnit_TextUI_TestRunner::run($suite);7{8 public function testOne()9 {10 }11 public function testTwo()12 {13 }14}15{16 public function testOne()17 {
setBackupGlobals
Using AI Code Generation
1require_once("simpletest/unit_tester.php");2require_once("simpletest/reporter.php");3require_once("simpletest/mock_objects.php");4require_once("simpletest/mock_objects.php");5require_once("simpletest/mock_objects.php");6class test extends UnitTestCase {7 public function testBackupGlobals() {8 $test = new TestSuite();9 $test->setBackupGlobals(false);10 $this->assertFalse($test->getBackupGlobals());11 }12}
setBackupGlobals
Using AI Code Generation
1$test = new TestSuite("TestSuite");2$test->setBackupGlobals(false);3$test->addTestFile('test.php');4$test->run(new TextReporter());5{6 public function testBackupGlobals()7 {8 $this->assertTrue($GLOBALS['foo']);9 }10}11{12 public function testOne()13 {14 $this->skip('skipping this test');15 }16}17{18 public function testOne()19 {20 $this->markTestIncomplete('This test is not yet implemented');21 }22}23The above code will mark the testOne() method as incomplete
setBackupGlobals
Using AI Code Generation
1require_once 'PHPUnit/Framework.php';2require_once 'PHPUnit/Extensions/PhptTestSuite.php';3$suite = new PHPUnit_Extensions_PhptTestSuite('tests');4$suite->setBackupGlobals(true);5PHPUnit_TextUI_TestRunner::run($suite);6{7 public function test()8 {9 $this->assertTrue(isset($GLOBALS['var']));10 }11}12OK (1 test, 1 assertion)13require_once 'PHPUnit/Framework.php';14require_once 'PHPUnit/Extensions/PhptTestSuite.php';15$suite = new PHPUnit_Extensions_PhptTestSuite('tests');16$suite->setBackupGlobals(false);17PHPUnit_TextUI_TestRunner::run($suite);18{19 public function test()20 {21 $this->assertTrue(isset($GLOBALS['var']));22 }23}24OK (1 test, 1 assertion)25require_once 'PHPUnit/Framework.php';26require_once 'PHPUnit/Extensions/PhptTestSuite.php';27$suite = new PHPUnit_Extensions_PhptTestSuite('tests');28$suite->setBackupGlobals(true);29PHPUnit_TextUI_TestRunner::run($suite);30{31 public function test()32 {
setBackupGlobals
Using AI Code Generation
1require_once 'simpletest/unit_tester.php';2require_once 'simpletest/reporter.php';3class TestOfFoo extends UnitTestCase {4 function testGlobal() {5 $this->assertTrue(isset($GLOBALS['foo']));6 }7}8class TestOfBar extends UnitTestCase {9 function testGlobal() {10 $this->assertTrue(isset($GLOBALS['bar']));11 }12}13$test = new TestSuite('Testing globals');14$test->addTestFile('1.php');15$test->addTestFile('2.php');16$test->setBackupGlobals(false);17$test->run(new HtmlReporter());18require_once 'simpletest/unit_tester.php';19require_once 'simpletest/reporter.php';20class TestOfFoo extends UnitTestCase {21 static $foo;22 function testStatic() {23 $this->assertTrue(isset(self::$foo));24 }25}26class TestOfBar extends UnitTestCase {27 static $bar;28 function testStatic() {29 $this->assertTrue(isset(self::$bar));30 }31}32$test = new TestSuite('Testing statics');33$test->addTestFile('1.php');34$test->addTestFile('2.php');35$test->setBackupStaticAttributes(false);36$test->run(new HtmlReporter());37require_once 'simpletest/unit_tester.php';38require_once 'simpletest/reporter.php';39class TestOfFoo extends UnitTestCase {40 function testIncludePath() {41 $this->assertTrue(file_exists('foo.php'));42 }43}44class TestOfBar extends UnitTestCase {45 function testIncludePath() {46 $this->assertTrue(file_exists('bar.php'));47 }48}49$test = new TestSuite('Testing include path');50$test->addTestFile('1.php');51$test->addTestFile('2.php');52$test->addIncludePath('include');53$test->run(new HtmlReporter());
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with setBackupGlobals on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!