diff --git a/Tests/Caster/DoctrineCasterTest.php b/Tests/Caster/DoctrineCasterTest.php new file mode 100644 index 00000000..85f6293b --- /dev/null +++ b/Tests/Caster/DoctrineCasterTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\ORM\PersistentCollection; +use PHPUnit\Framework\TestCase; +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; + +/** + * @requires function \Doctrine\Common\Collections\ArrayCollection::__construct + */ +class DoctrineCasterTest extends TestCase +{ + use VarDumperTestTrait; + + public function testCastPersistentCollection() + { + $classMetadata = new ClassMetadata(__CLASS__); + + $collection = new PersistentCollection($this->createMock(EntityManagerInterface::class), $classMetadata, new ArrayCollection(['test'])); + + $expected = <<assertDumpMatchesFormat($expected, $collection); + } +} diff --git a/Tests/Caster/ExceptionCasterTest.php b/Tests/Caster/ExceptionCasterTest.php index 73545701..2609eb7f 100644 --- a/Tests/Caster/ExceptionCasterTest.php +++ b/Tests/Caster/ExceptionCasterTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\VarDumper\Tests\Caster; use PHPUnit\Framework\TestCase; +use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext; use Symfony\Component\VarDumper\Caster\Caster; use Symfony\Component\VarDumper\Caster\ExceptionCaster; use Symfony\Component\VarDumper\Caster\FrameStub; @@ -29,6 +30,21 @@ private function getTestException($msg, &$ref = null) return new \Exception(''.$msg); } + private function getTestError($msg): \Error + { + return new \Error(''.$msg); + } + + private function getTestErrorException($msg): \ErrorException + { + return new \ErrorException(''.$msg); + } + + private function getTestSilencedErrorContext(): SilencedErrorContext + { + return new SilencedErrorContext(\E_ERROR, __FILE__, __LINE__); + } + protected function tearDown(): void { ExceptionCaster::$srcContext = 1; @@ -61,6 +77,79 @@ public function testDefaultSettings() $this->assertSame(['foo'], $ref); } + public function testDefaultSettingsOnError() + { + $e = $this->getTestError('foo'); + + $expectedDump = <<<'EODUMP' +Error { + #message: "foo" + #code: 0 + #file: "%sExceptionCasterTest.php" + #line: %d + trace: { + %s%eTests%eCaster%eExceptionCasterTest.php:%d { + Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestError($msg): Error + › { + › return new \Error(''.$msg); + › } + } + %s%eTests%eCaster%eExceptionCasterTest.php:%d { …} +%A +EODUMP; + + $this->assertDumpMatchesFormat($expectedDump, $e); + } + + public function testDefaultSettingsOnErrorException() + { + $e = $this->getTestErrorException('foo'); + + $expectedDump = <<<'EODUMP' +ErrorException { + #message: "foo" + #code: 0 + #file: "%sExceptionCasterTest.php" + #line: %d + #severity: E_ERROR + trace: { + %s%eTests%eCaster%eExceptionCasterTest.php:%d { + Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestErrorException($msg): ErrorException + › { + › return new \ErrorException(''.$msg); + › } + } + %s%eTests%eCaster%eExceptionCasterTest.php:%d { …} +%A +EODUMP; + + $this->assertDumpMatchesFormat($expectedDump, $e); + } + + /** + * @requires function \Symfony\Component\ErrorHandler\Exception\SilencedErrorContext::__construct + */ + public function testCastSilencedErrorContext() + { + $e = $this->getTestSilencedErrorContext(); + + $expectedDump = <<<'EODUMP' +Symfony\Component\ErrorHandler\Exception\SilencedErrorContext { + +count: 1 + -severity: E_ERROR + trace: { + %s%eTests%eCaster%eExceptionCasterTest.php:%d { + › { + › return new SilencedErrorContext(\E_ERROR, __FILE__, __LINE__); + › } + } + } +} +EODUMP; + + $this->assertDumpMatchesFormat($expectedDump, $e); + } + public function testSeek() { $e = $this->getTestException(2); diff --git a/Tests/Caster/FiberCasterTest.php b/Tests/Caster/FiberCasterTest.php new file mode 100644 index 00000000..41ba099d --- /dev/null +++ b/Tests/Caster/FiberCasterTest.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; + +class FiberCasterTest extends TestCase +{ + use VarDumperTestTrait; + + public function testCastFiberNotStarted() + { + $fiber = new \Fiber(static fn() => true); + + $expected = <<assertDumpEquals($expected, $fiber); + } + + public function testCastFiberTerminated() + { + $fiber = new \Fiber(static fn () => true); + $fiber->start(); + + $expected = <<assertDumpEquals($expected, $fiber); + } + + public function testCastFiberSuspended() + { + $fiber = new \Fiber(\Fiber::suspend(...)); + $fiber->start(); + + $expected = <<assertDumpEquals($expected, $fiber); + } + + public function testCastFiberRunning() + { + $fiber = new \Fiber(function () { + $expected = <<assertDumpEquals($expected, \Fiber::getCurrent()); + }); + + $fiber->start(); + } +} diff --git a/Tests/Dumper/ContextProvider/RequestContextProviderTest.php b/Tests/Dumper/ContextProvider/RequestContextProviderTest.php new file mode 100644 index 00000000..5c141595 --- /dev/null +++ b/Tests/Dumper/ContextProvider/RequestContextProviderTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Dumper\ContextProvider; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\VarDumper\Cloner\Data; +use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider; + +/** + * @requires function \Symfony\Component\HttpFoundation\RequestStack::__construct + */ +class RequestContextProviderTest extends TestCase +{ + public function testGetContextOnNullRequest() + { + $requestStack = new RequestStack(); + $provider = new RequestContextProvider($requestStack); + + $this->assertNull($provider->getContext()); + } + + public function testGetContextOnRequest() + { + $request = Request::create('https://example.org/', 'POST'); + $request->attributes->set('_controller', 'MyControllerClass'); + + $requestStack = new RequestStack(); + $requestStack->push($request); + + $context = (new RequestContextProvider($requestStack))->getContext(); + $this->assertSame('https://example.org/', $context['uri']); + $this->assertSame('POST', $context['method']); + $this->assertInstanceOf(Data::class, $context['controller']); + $this->assertSame('MyControllerClass', $context['controller']->getValue()); + $this->assertSame('https://example.org/', $context['uri']); + $this->assertArrayHasKey('identifier', $context); + } +}