8000 [VarDumper] Add a bit of test coverage · adpauly/symfony@f73f224 · GitHub
[go: up one dir, main page]

Skip to content

Commit f73f224

Browse files
[VarDumper] Add a bit of test coverage
1 parent 10c73d3 commit f73f224

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use Doctrine\Common\Collections\ArrayCollection;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Doctrine\ORM\Mapping\ClassMetadata;
17+
use Doctrine\ORM\PersistentCollection;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
20+
21+
/**
22+
* @requires function \Doctrine\Common\Collections\ArrayCollection::__construct
23+
*/
24+
class DoctrineCasterTest extends TestCase
25+
{
26+
use VarDumperTestTrait;
27+
28+
public function testCastPersistentCollection()
29+
{
30+
$classMetadata = new ClassMetadata(__CLASS__);
31+
32+
$collection = new PersistentCollection($this->createMock(EntityManagerInterface::class), $classMetadata, new ArrayCollection(['test']));
33+
34+
$expected = <<<EODUMP
35+
Doctrine\ORM\PersistentCollection {
36+
%A
37+
-em: Mock_EntityManagerInterface_%s { …3}
38+
-backRefFieldName: null
39+
-typeClass: Doctrine\ORM\Mapping\ClassMetadata { …}
40+
%A
41+
EODUMP;
42+
43+
$this->assertDumpMatchesFormat($expected, $collection);
44+
}
45+
}

src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext;
1516
use Symfony\Component\VarDumper\Caster\Caster;
1617
use Symfony\Component\VarDumper\Caster\ExceptionCaster;
1718
use Symfony\Component\VarDumper\Caster\FrameStub;
@@ -29,6 +30,21 @@ private function getTestException($msg, &$ref = null)
2930
return new \Exception(''.$msg);
3031
}
3132

33+
private function getTestError($msg): \Error
34+
{
35+
return new \Error(''.$msg);
36+
}
37+
38+
private function getTestErrorException($msg): \ErrorException
39+
{
40+
return new \ErrorException(''.$msg);
41+
}
42+
43+
private function getTestSilencedErrorContext(): SilencedErrorContext
44+
{
45+
return new SilencedErrorContext(\E_ERROR, __FILE__, __LINE__);
46+
}
47+
3248
protected function tearDown(): void
3349
{
3450
ExceptionCaster::$srcContext = 1;
@@ -61,6 +77,79 @@ public function testDefaultSettings()
6177
$this->assertSame(['foo'], $ref);
6278
}
6379

80+
public function testDefaultSettingsOnError()
81+
{
82+
$e = $this->getTestError('foo');
83+
84+
$expectedDump = <<<'EODUMP'
85+
Error {
86+
#message: "foo"
87+
#code: 0
88+
#file: "%sExceptionCasterTest.php"
89+
#line: %d
90+
trace: {
91+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
92+
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestError($msg): Error
93+
› {
94+
› return new \Error(''.$msg);
95+
› }
96+
}
97+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
98+
%A
99+
EODUMP;
100+
101+
$this->assertDumpMatchesFormat($expectedDump, $e);
102+
}
103+
104+
public function testDefaultSettingsOnErrorException()
105+
{
106+
$e = $this->getTestErrorException('foo');
107+
108+
$expectedDump = <<<'EODUMP'
109+
ErrorException {
110+
#message: "foo"
111+
#code: 0
112+
#file: "%sExceptionCasterTest.php"
113+
#line: %d
114+
#severity: E_ERROR
115+
trace: {
116+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
117+
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestErrorException($msg): ErrorException
118+
› {
119+
› return new \ErrorException(''.$msg);
120+
› }
121+
}
122+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
123+
%A
124+
EODUMP;
125+
126+
$this->assertDumpMatchesFormat($expectedDump, $e);
127+
}
128+
129+
/**
130+
* @requires function \Symfony\Component\ErrorHandler\Exception\SilencedErrorContext::__construct
131+
*/
132+
public function testCastSilencedErrorContext()
133+
{
134+
$e = $this->getTestSilencedErrorContext();
135+
136+
$expectedDump = <<<'EODUMP'
137+
Symfony\Component\ErrorHandler\Exception\SilencedErrorContext {
138+
+count: 1
139+
-severity: E_ERROR
140+
trace: {
141+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
142+
› {
143+
› return new SilencedErrorContext(\E_ERROR, __FILE__, __LINE__);
144+
› }
145+
}
146+
}
147+
}
148+
EODUMP;
149+
150+
$this->assertDumpMatchesFormat($expectedDump, $e);
151+
}
152+
64153
public function testSeek()
65154
{
66155
$e = $this->getTestException(2);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
16+
17+
/**
18+
* @requires PHP 8.1
19+
*/
20+
class FiberCasterTest extends TestCase
21+
{
22+
use VarDumperTestTrait;
23+
24+
public function testCastFiberNotStarted()
25+
{
26+
$fiber = new \Fiber(static function () {
27+
return true;
28+
});
29+
30+
$expected = <<<EODUMP
31+
Fiber {
32+
status: "not started"
33+
}
34+
EODUMP;
35+
36+
$this->assertDumpEquals($expected, $fiber);
37+
}
38+
39+
public function testCastFiberTerminated()
40+
{
41+
$fiber = new \Fiber(static function () {
42+
return true;
43+
});
44+
$fiber->start();
45+
46+
$expected = <<<EODUMP
47+
Fiber {
48+
status: "terminated"
49+
}
50+
EODUMP;
51+
52+
$this->assertDumpEquals($expected, $fiber);
53+
}
54+
55+
public function testCastFiberSuspended()
56+
{
57+
$fiber = new \Fiber(static function () {
58+
\Fiber::suspend();
59+
});
60+
$fiber->start();
61+
62+
$expected = <<<EODUMP
63+
Fiber {
64+
status: "suspended"
65+
}
66+
EODUMP;
67+
68+
$this->assertDumpEquals($expected, $fiber);
69+
}
70+
71+
public function testCastFiberRunning()
72+
{
73+
$fiber = new \Fiber(function () {
74+
$expected = <<<EODUMP
75+
Fiber {
76+
status: "running"
77+
}
78+
EODUMP;
79+
80+
$this->assertDumpEquals($expected, \Fiber::getCurrent());
81+
});
82+
83+
$fiber->start();
84+
}
85+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dumper\ContextProvider;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\RequestStack;
17+
use Symfony\Component\VarDumper\Cloner\Data;
18+
use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
19+
20+
/**
21+
* @requires function \Symfony\Component\HttpFoundation\RequestStack::__construct
22+
*/
23+
class RequestContextProviderTest extends TestCase
24+
{
25+
public function testGetContextOnNullRequest()
26+
{
27+
$requestStack = new RequestStack();
28+
$provider = new RequestContextProvider($requestStack);
29+
30+
$this->assertNull($provider->getContext());
31+
}
32+
33+
public function testGetContextOnRequest()
34+
{
35+
$request = Request::create('https://example.org/', 'POST');
36+
$request->attributes->set('_controller', 'MyControllerClass');
37+
38+
$requestStack = new RequestStack();
39+
$requestStack->push($request);
40+
41+
$context = (new RequestContextProvider($requestStack))->getContext();
42+
$this->assertSame('https://example.org/', $context['uri']);
43+
$this->assertSame('POST', $context['method']);
44+
$this->assertInstanceOf(Data::class, $context['controller']);
45+
$this->assertSame('MyControllerClass', $context['controller']->getValue());
46+
$this->assertSame('https://example.org/', $context['uri']);
47+
$this->assertArrayHasKey('identifier', $context);
48+
}
49+
}

0 commit comments

Comments
 (0)
0