RestorerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of sebastian/global-state.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace SebastianBergmann\GlobalState;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * Class Restorer.
  15. */
  16. class RestorerTest extends TestCase
  17. {
  18. public static function setUpBeforeClass()
  19. {
  20. $GLOBALS['varBool'] = false;
  21. $GLOBALS['varNull'] = null;
  22. $_GET['varGet'] = 0;
  23. }
  24. /**
  25. * Check global variables are correctly backuped and restored (unit test).
  26. *
  27. * @covers \SebastianBergmann\GlobalState\Restorer::restoreGlobalVariables
  28. * @covers \SebastianBergmann\GlobalState\Restorer::restoreSuperGlobalArray
  29. *
  30. * @uses \SebastianBergmann\GlobalState\Blacklist::isGlobalVariableBlacklisted
  31. * @uses \SebastianBergmann\GlobalState\Snapshot::__construct
  32. * @uses \SebastianBergmann\GlobalState\Snapshot::blacklist
  33. * @uses \SebastianBergmann\GlobalState\Snapshot::canBeSerialized
  34. * @uses \SebastianBergmann\GlobalState\Snapshot::globalVariables
  35. * @uses \SebastianBergmann\GlobalState\Snapshot::setupSuperGlobalArrays
  36. * @uses \SebastianBergmann\GlobalState\Snapshot::snapshotGlobals
  37. * @uses \SebastianBergmann\GlobalState\Snapshot::snapshotSuperGlobalArray
  38. * @uses \SebastianBergmann\GlobalState\Snapshot::superGlobalArrays
  39. * @uses \SebastianBergmann\GlobalState\Snapshot::superGlobalVariables
  40. */
  41. public function testRestorerGlobalVariable()
  42. {
  43. $snapshot = new Snapshot(null, true, false, false, false, false, false, false, false, false);
  44. $restorer = new Restorer;
  45. $restorer->restoreGlobalVariables($snapshot);
  46. $this->assertArrayHasKey('varBool', $GLOBALS);
  47. $this->assertEquals(false, $GLOBALS['varBool']);
  48. $this->assertArrayHasKey('varNull', $GLOBALS);
  49. $this->assertEquals(null, $GLOBALS['varNull']);
  50. $this->assertArrayHasKey('varGet', $_GET);
  51. $this->assertEquals(0, $_GET['varGet']);
  52. }
  53. /**
  54. * Check global variables are correctly backuped and restored.
  55. *
  56. * The real test is the second, but the first has to be executed to backup the globals.
  57. *
  58. * @backupGlobals enabled
  59. * @covers \SebastianBergmann\GlobalState\Restorer::restoreGlobalVariables
  60. * @covers \SebastianBergmann\GlobalState\Restorer::restoreSuperGlobalArray
  61. *
  62. * @uses \SebastianBergmann\GlobalState\Blacklist::addClassNamePrefix
  63. * @uses \SebastianBergmann\GlobalState\Blacklist::isGlobalVariableBlacklisted
  64. * @uses \SebastianBergmann\GlobalState\Snapshot::__construct
  65. * @uses \SebastianBergmann\GlobalState\Snapshot::blacklist
  66. * @uses \SebastianBergmann\GlobalState\Snapshot::canBeSerialized
  67. * @uses \SebastianBergmann\GlobalState\Snapshot::globalVariables
  68. * @uses \SebastianBergmann\GlobalState\Snapshot::setupSuperGlobalArrays
  69. * @uses \SebastianBergmann\GlobalState\Snapshot::snapshotGlobals
  70. * @uses \SebastianBergmann\GlobalState\Snapshot::snapshotSuperGlobalArray
  71. * @uses \SebastianBergmann\GlobalState\Snapshot::superGlobalArrays
  72. * @uses \SebastianBergmann\GlobalState\Snapshot::superGlobalVariables
  73. */
  74. public function testIntegrationRestorerGlobalVariables()
  75. {
  76. $this->assertArrayHasKey('varBool', $GLOBALS);
  77. $this->assertEquals(false, $GLOBALS['varBool']);
  78. $this->assertArrayHasKey('varNull', $GLOBALS);
  79. $this->assertEquals(null, $GLOBALS['varNull']);
  80. $this->assertArrayHasKey('varGet', $_GET);
  81. $this->assertEquals(0, $_GET['varGet']);
  82. }
  83. /**
  84. * Check global variables are correctly backuped and restored.
  85. *
  86. * @depends testIntegrationRestorerGlobalVariables
  87. */
  88. public function testIntegrationRestorerGlobalVariables2()
  89. {
  90. $this->assertArrayHasKey('varBool', $GLOBALS);
  91. $this->assertEquals(false, $GLOBALS['varBool']);
  92. $this->assertArrayHasKey('varNull', $GLOBALS);
  93. $this->assertEquals(null, $GLOBALS['varNull']);
  94. $this->assertArrayHasKey('varGet', $_GET);
  95. $this->assertEquals(0, $_GET['varGet']);
  96. }
  97. }