Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6726b42

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: do not use PHPUnit mock objects without configured expectations fix low deps test with Console component < 6.4 [FrameworkBundle] Check for console package before register `CommandDataCollector` do not use PHPUnit mock objects without configured expectations [FrameworkBundle] Ensure a fresh container is used after cache warmup in KernelTestCase do not use PHPUnit mock objects without configured expectations do not use PHPUnit mock objects without configured expectations [Finder] Fix `Finder::append()` breaking generic typing contract
2 parents 92eb2ec + 43a730a commit 6726b42

21 files changed

Lines changed: 258 additions & 235 deletions

Tests/CacheWarmer/CompiledClassMetadataCacheWarmerTest.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
use Symfony\Component\Filesystem\Filesystem;
1616
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1717
use Symfony\Component\Serializer\CacheWarmer\CompiledClassMetadataCacheWarmer;
18+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1819
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryCompiler;
19-
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
20+
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
2021

2122
/**
2223
* @group legacy
@@ -25,28 +26,20 @@ final class CompiledClassMetadataCacheWarmerTest extends TestCase
2526
{
2627
public function testItImplementsCacheWarmerInterface()
2728
{
28-
$classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class);
29-
$filesystem = $this->createMock(Filesystem::class);
30-
31-
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem);
29+
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], new ClassMetadataFactory(new AttributeLoader()), new ClassMetadataFactoryCompiler(), new Filesystem());
3230

3331
$this->assertInstanceOf(CacheWarmerInterface::class, $compiledClassMetadataCacheWarmer);
3432
}
3533

3634
public function testItIsAnOptionalCacheWarmer()
3735
{
38-
$classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class);
39-
$filesystem = $this->createMock(Filesystem::class);
40-
41-
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem);
36+
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], new ClassMetadataFactory(new AttributeLoader()), new ClassMetadataFactoryCompiler(), new Filesystem());
4237

4338
$this->assertTrue($compiledClassMetadataCacheWarmer->isOptional());
4439
}
4540

4641
public function testItDumpCompiledClassMetadatas()
4742
{
48-
$classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class);
49-
5043
$code = <<<EOF
5144
<?php
5245
@@ -63,7 +56,7 @@ public function testItDumpCompiledClassMetadatas()
6356
->with('/var/cache/prod/serializer.class.metadata.php', $code)
6457
;
6558

66-
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem);
59+
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], new ClassMetadataFactory(new AttributeLoader()), new ClassMetadataFactoryCompiler(), $filesystem);
6760

6861
$compiledClassMetadataCacheWarmer->warmUp('/var/cache/prod');
6962
}

Tests/Command/DebugCommandTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Console\Tester\CommandTester;
1616
use Symfony\Component\Serializer\Command\DebugCommand;
1717
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
18-
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
1918
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
2019
use Symfony\Component\Serializer\Tests\Dummy\DummyClassOne;
2120
use Symfony\Component\Serializer\Tests\Dummy\DummyClassWithDiscriminatorMap;
@@ -117,9 +116,7 @@ public function testOutputWithDiscriminatorMapClass()
117116

118117
public function testOutputWithInvalidClassArgument()
119118
{
120-
$serializer = $this->createMock(ClassMetadataFactoryInterface::class);
121-
122-
$command = new DebugCommand($serializer);
119+
$command = new DebugCommand(new ClassMetadataFactory(new AttributeLoader()));
123120

124121
$tester = new CommandTester($command);
125122
$tester->execute(['class' => 'App\\NotFoundResource'], ['decorated' => false]);

Tests/Debug/TraceableEncoderTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function testCollectEncodingData()
4444
{
4545
$serializerName = uniqid('name', true);
4646

47-
$encoder = $this->createMock(EncoderInterface::class);
48-
$decoder = $this->createMock(DecoderInterface::class);
47+
$encoder = $this->createStub(EncoderInterface::class);
48+
$decoder = $this->createStub(DecoderInterface::class);
4949

5050
$dataCollector = $this->createMock(SerializerDataCollector::class);
5151
$dataCollector
@@ -63,8 +63,8 @@ public function testCollectEncodingData()
6363

6464
public function testNotCollectEncodingDataIfNoDebugTraceId()
6565
{
66-
$encoder = $this->createMock(EncoderInterface::class);
67-
$decoder = $this->createMock(DecoderInterface::class);
66+
$encoder = $this->createStub(EncoderInterface::class);
67+
$decoder = $this->createStub(DecoderInterface::class);
6868

6969
$dataCollector = $this->createMock(SerializerDataCollector::class);
7070
$dataCollector->expects($this->never())->method('collectEncoding');
@@ -78,22 +78,22 @@ public function testCannotEncodeIfNotEncoder()
7878
{
7979
$this->expectException(\BadMethodCallException::class);
8080

81-
(new TraceableEncoder($this->createMock(DecoderInterface::class), new SerializerDataCollector(), 'default'))->encode('data', 'format');
81+
(new TraceableEncoder($this->createStub(DecoderInterface::class), new SerializerDataCollector(), 'default'))->encode('data', 'format');
8282
}
8383

8484
public function testCannotDecodeIfNotDecoder()
8585
{
8686
$this->expectException(\BadMethodCallException::class);
8787

88-
(new TraceableEncoder($this->createMock(EncoderInterface::class), new SerializerDataCollector(), 'default'))->decode('data', 'format');
88+
(new TraceableEncoder($this->createStub(EncoderInterface::class), new SerializerDataCollector(), 'default'))->decode('data', 'format');
8989
}
9090

9191
public function testSupports()
9292
{
93-
$encoder = $this->createMock(EncoderInterface::class);
93+
$encoder = $this->createStub(EncoderInterface::class);
9494
$encoder->method('supportsEncoding')->willReturn(true);
9595

96-
$decoder = $this->createMock(DecoderInterface::class);
96+
$decoder = $this->createStub(DecoderInterface::class);
9797
$decoder->method('supportsDecoding')->willReturn(true);
9898

9999
$traceableEncoder = new TraceableEncoder($encoder, new SerializerDataCollector(), 'default');

Tests/Debug/TraceableNormalizerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function testCollectNormalizationData()
4646
{
4747
$serializerName = uniqid('name', true);
4848

49-
$normalizer = $this->createMock(NormalizerInterface::class);
49+
$normalizer = $this->createStub(NormalizerInterface::class);
5050
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
51-
$denormalizer = $this->createMock(DenormalizerInterface::class);
51+
$denormalizer = $this->createStub(DenormalizerInterface::class);
5252
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
5353

5454
$dataCollector = $this->createMock(SerializerDataCollector::class);
@@ -67,9 +67,9 @@ public function testCollectNormalizationData()
6767

6868
public function testNotCollectNormalizationDataIfNoDebugTraceId()
6969
{
70-
$normalizer = $this->createMock(NormalizerInterface::class);
70+
$normalizer = $this->createStub(NormalizerInterface::class);
7171
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
72-
$denormalizer = $this->createMock(DenormalizerInterface::class);
72+
$denormalizer = $this->createStub(DenormalizerInterface::class);
7373
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
7474

7575
$dataCollector = $this->createMock(SerializerDataCollector::class);
@@ -84,23 +84,23 @@ public function testCannotNormalizeIfNotNormalizer()
8484
{
8585
$this->expectException(\BadMethodCallException::class);
8686

87-
(new TraceableNormalizer($this->createMock(DenormalizerInterface::class), new SerializerDataCollector(), 'default'))->normalize('data');
87+
(new TraceableNormalizer($this->createStub(DenormalizerInterface::class), new SerializerDataCollector(), 'default'))->normalize('data');
8888
}
8989

9090
public function testCannotDenormalizeIfNotDenormalizer()
9191
{
9292
$this->expectException(\BadMethodCallException::class);
9393

94-
(new TraceableNormalizer($this->createMock(NormalizerInterface::class), new SerializerDataCollector(), 'default'))->denormalize('data', 'type');
94+
(new TraceableNormalizer($this->createStub(NormalizerInterface::class), new SerializerDataCollector(), 'default'))->denormalize('data', 'type');
9595
}
9696

9797
public function testSupports()
9898
{
99-
$normalizer = $this->createMock(NormalizerInterface::class);
99+
$normalizer = $this->createStub(NormalizerInterface::class);
100100
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
101101
$normalizer->method('supportsNormalization')->willReturn(true);
102102

103-
$denormalizer = $this->createMock(DenormalizerInterface::class);
103+
$denormalizer = $this->createStub(DenormalizerInterface::class);
104104
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
105105
$denormalizer->method('supportsDenormalization')->willReturn(true);
106106

Tests/Debug/TraceableSerializerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testCollectData()
108108

109109
public function testAddDebugTraceIdInContext()
110110
{
111-
$serializer = $this->createMock(Serializer::class);
111+
$serializer = $this->createStub(Serializer::class);
112112

113113
foreach (['serialize', 'deserialize', 'normalize', 'denormalize', 'encode', 'decode'] as $method) {
114114
$serializer->method($method)->willReturnCallback(function (): string {

Tests/Encoder/ChainDecoderTest.php

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Serializer\Tests\Encoder;
1313

14-
use PHPUnit\Framework\MockObject\MockObject;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\Component\Serializer\Encoder\ChainDecoder;
1716
use Symfony\Component\Serializer\Encoder\ContextAwareDecoderInterface;
@@ -24,72 +23,90 @@ class ChainDecoderTest extends TestCase
2423
private const FORMAT_2 = 'format2';
2524
private const FORMAT_3 = 'format3';
2625

27-
private ChainDecoder $chainDecoder;
28-
private MockObject&ContextAwareDecoderInterface $decoder1;
29-
private MockObject&DecoderInterface $decoder2;
30-
31-
protected function setUp(): void
32-
{
33-
$this->decoder1 = $this->createMock(ContextAwareDecoderInterface::class);
34-
$this->decoder1
35-
->method('supportsDecoding')
36-
->willReturnMap([
37-
[self::FORMAT_1, [], true],
38-
[self::FORMAT_2, [], false],
39-
[self::FORMAT_3, [], false],
40-
[self::FORMAT_3, ['foo' => 'bar'], true],
41-
[self::FORMAT_3, ['foo' => 'bar2'], false],
42-
]);
43-
44-
$this->decoder2 = $this->createMock(DecoderInterface::class);
45-
$this->decoder2
46-
->method('supportsDecoding')
47-
->willReturnMap([
48-
[self::FORMAT_1, [], false],
49-
[self::FORMAT_2, [], true],
50-
[self::FORMAT_3, [], false],
51-
[self::FORMAT_3, ['foo' => 'bar'], false],
52-
[self::FORMAT_3, ['foo' => 'bar2'], true],
53-
]);
54-
55-
$this->chainDecoder = new ChainDecoder([$this->decoder1, $this->decoder2]);
56-
}
57-
5826
public function testSupportsDecoding()
5927
{
60-
$this->decoder1
28+
$decoder1 = $this->createDecoder1();
29+
$decoder1
6130
->method('decode')
6231
->willReturn('result1');
63-
$this->decoder2
32+
$decoder2 = $this->createDecoder2();
33+
$decoder2
6434
->method('decode')
6535
->willReturn('result2');
36+
$chainDecoder = new ChainDecoder([$decoder1, $decoder2]);
6637

67-
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
68-
$this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_1, []));
38+
$this->assertTrue($chainDecoder->supportsDecoding(self::FORMAT_1));
39+
$this->assertEquals('result1', $chainDecoder->decode('', self::FORMAT_1, []));
6940

70-
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
71-
$this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_2, []));
41+
$this->assertTrue($chainDecoder->supportsDecoding(self::FORMAT_2));
42+
$this->assertEquals('result2', $chainDecoder->decode('', self::FORMAT_2, []));
7243

73-
$this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
44+
$this->assertFalse($chainDecoder->supportsDecoding(self::FORMAT_3));
7445

75-
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
76-
$this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar']));
46+
$this->assertTrue($chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
47+
$this->assertEquals('result1', $chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar']));
7748

78-
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar2']));
79-
$this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar2']));
49+
$this->assertTrue($chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar2']));
50+
$this->assertEquals('result2', $chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar2']));
8051
}
8152

8253
public function testDecode()
8354
{
84-
$this->decoder1->expects($this->never())->method('decode');
85-
$this->decoder2->expects($this->once())->method('decode');
55+
$decoder1 = $this->createDecoder1(true);
56+
$decoder1->expects($this->never())->method('decode');
57+
$decoder2 = $this->createDecoder2(true);
58+
$decoder2->expects($this->once())->method('decode');
59+
$chainDecoder = new ChainDecoder([$decoder1, $decoder2]);
8660

87-
$this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
61+
$chainDecoder->decode('string_to_decode', self::FORMAT_2);
8862
}
8963

9064
public function testDecodeUnsupportedFormat()
9165
{
66+
$chainDecoder = new ChainDecoder([$this->createDecoder1(), $this->createDecoder2()]);
9267
$this->expectException(RuntimeException::class);
93-
$this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
68+
$chainDecoder->decode('string_to_decode', self::FORMAT_3);
69+
}
70+
71+
private function createDecoder1(bool $mock = false): DecoderInterface
72+
{
73+
if ($mock) {
74+
$decoder = $this->createMock(ContextAwareDecoderInterface::class);
75+
} else {
76+
$decoder = $this->createStub(ContextAwareDecoderInterface::class);
77+
}
78+
79+
$decoder
80+
->method('supportsDecoding')
81+
->willReturnMap([
82+
[self::FORMAT_1, [], true],
83+
[self::FORMAT_2, [], false],
84+
[self::FORMAT_3, [], false],
85+
[self::FORMAT_3, ['foo' => 'bar'], true],
86+
[self::FORMAT_3, ['foo' => 'bar2'], false],
87+
]);
88+
89+
return $decoder;
90+
}
91+
92+
private function createDecoder2(bool $mock = false): DecoderInterface
93+
{
94+
if ($mock) {
95+
$decoder = $this->createMock(DecoderInterface::class);
96+
} else {
97+
$decoder = $this->createStub(DecoderInterface::class);
98+
}
99+
100+
$decoder
101+
->method('supportsDecoding')
102+
->willReturnMap([
103+
[self::FORMAT_1, [], false],
104+
[self::FORMAT_2, [], true],
105+
[self::FORMAT_3, [], false],
106+
[self::FORMAT_3, ['foo' => 'bar'], false],
107+
[self::FORMAT_3, ['foo' => 'bar2'], true],
108+
]);
109+
110+
return $decoder;
94111
}
95112
}

0 commit comments

Comments
 (0)