SnapshotTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php declare(strict_types=1);
  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. namespace SebastianBergmann\GlobalState;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\GlobalState\TestFixture\BlacklistedInterface;
  13. use SebastianBergmann\GlobalState\TestFixture\SnapshotClass;
  14. use SebastianBergmann\GlobalState\TestFixture\SnapshotTrait;
  15. /**
  16. * @covers \SebastianBergmann\GlobalState\Snapshot
  17. *
  18. * @uses \SebastianBergmann\GlobalState\Blacklist
  19. */
  20. final class SnapshotTest extends TestCase
  21. {
  22. /**
  23. * @var Blacklist
  24. */
  25. private $blacklist;
  26. protected function setUp(): void
  27. {
  28. $this->blacklist = new Blacklist;
  29. }
  30. public function testStaticAttributes(): void
  31. {
  32. SnapshotClass::init();
  33. $this->blacklistAllLoadedClassesExceptSnapshotClass();
  34. $snapshot = new Snapshot($this->blacklist, false, true, false, false, false, false, false, false, false);
  35. $expected = [
  36. SnapshotClass::class => [
  37. 'string' => 'string',
  38. 'objects' => [new \stdClass],
  39. ],
  40. ];
  41. $this->assertEquals($expected, $snapshot->staticAttributes());
  42. }
  43. public function testConstants(): void
  44. {
  45. $snapshot = new Snapshot($this->blacklist, false, false, true, false, false, false, false, false, false);
  46. $this->assertArrayHasKey('GLOBALSTATE_TESTSUITE', $snapshot->constants());
  47. }
  48. public function testFunctions(): void
  49. {
  50. $snapshot = new Snapshot($this->blacklist, false, false, false, true, false, false, false, false, false);
  51. $functions = $snapshot->functions();
  52. $this->assertContains('sebastianbergmann\globalstate\testfixture\snapshotfunction', $functions);
  53. $this->assertNotContains('assert', $functions);
  54. }
  55. public function testClasses(): void
  56. {
  57. $snapshot = new Snapshot($this->blacklist, false, false, false, false, true, false, false, false, false);
  58. $classes = $snapshot->classes();
  59. $this->assertContains(TestCase::class, $classes);
  60. $this->assertNotContains(Exception::class, $classes);
  61. }
  62. public function testInterfaces(): void
  63. {
  64. $snapshot = new Snapshot($this->blacklist, false, false, false, false, false, true, false, false, false);
  65. $interfaces = $snapshot->interfaces();
  66. $this->assertContains(BlacklistedInterface::class, $interfaces);
  67. $this->assertNotContains(\Countable::class, $interfaces);
  68. }
  69. public function testTraits(): void
  70. {
  71. \spl_autoload_call('SebastianBergmann\GlobalState\TestFixture\SnapshotTrait');
  72. $snapshot = new Snapshot($this->blacklist, false, false, false, false, false, false, true, false, false);
  73. $this->assertContains(SnapshotTrait::class, $snapshot->traits());
  74. }
  75. public function testIniSettings(): void
  76. {
  77. $snapshot = new Snapshot($this->blacklist, false, false, false, false, false, false, false, true, false);
  78. $iniSettings = $snapshot->iniSettings();
  79. $this->assertArrayHasKey('date.timezone', $iniSettings);
  80. $this->assertEquals('Etc/UTC', $iniSettings['date.timezone']);
  81. }
  82. public function testIncludedFiles(): void
  83. {
  84. $snapshot = new Snapshot($this->blacklist, false, false, false, false, false, false, false, false, true);
  85. $this->assertContains(__FILE__, $snapshot->includedFiles());
  86. }
  87. private function blacklistAllLoadedClassesExceptSnapshotClass(): void
  88. {
  89. foreach (\get_declared_classes() as $class) {
  90. if ($class === SnapshotClass::class) {
  91. continue;
  92. }
  93. $this->blacklist->addClass($class);
  94. }
  95. }
  96. }