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

Skip to content

Commit 43a730a

Browse files
committed
do not use PHPUnit mock objects without configured expectations
1 parent 5d5dc91 commit 43a730a

21 files changed

Lines changed: 258 additions & 237 deletions

Tests/CacheWarmer/CompiledClassMetadataCacheWarmerTest.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,28 @@
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
final class CompiledClassMetadataCacheWarmerTest extends TestCase
2223
{
2324
public function testItImplementsCacheWarmerInterface()
2425
{
25-
$classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class);
26-
$filesystem = $this->createMock(Filesystem::class);
27-
28-
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem);
26+
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], new ClassMetadataFactory(new AttributeLoader()), new ClassMetadataFactoryCompiler(), new Filesystem());
2927

3028
$this->assertInstanceOf(CacheWarmerInterface::class, $compiledClassMetadataCacheWarmer);
3129
}
3230

3331
public function testItIsAnOptionalCacheWarmer()
3432
{
35-
$classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class);
36-
$filesystem = $this->createMock(Filesystem::class);
37-
38-
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem);
33+
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], new ClassMetadataFactory(new AttributeLoader()), new ClassMetadataFactoryCompiler(), new Filesystem());
3934

4035
$this->assertTrue($compiledClassMetadataCacheWarmer->isOptional());
4136
}
4237

4338
public function testItDumpCompiledClassMetadatas()
4439
{
45-
$classMetadataFactory = $this->createMock(ClassMetadataFactoryInterface::class);
46-
4740
$code = <<<EOF
4841
<?php
4942
@@ -60,7 +53,7 @@ public function testItDumpCompiledClassMetadatas()
6053
->with('/var/cache/prod/serializer.class.metadata.php', $code)
6154
;
6255

63-
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], $classMetadataFactory, new ClassMetadataFactoryCompiler(), $filesystem);
56+
$compiledClassMetadataCacheWarmer = new CompiledClassMetadataCacheWarmer([], new ClassMetadataFactory(new AttributeLoader()), new ClassMetadataFactoryCompiler(), $filesystem);
6457

6558
$compiledClassMetadataCacheWarmer->warmUp('/var/cache/prod');
6659
}

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

@@ -79,9 +78,7 @@ public function testOutputWithClassArgument()
7978

8079
public function testOutputWithInvalidClassArgument()
8180
{
82-
$serializer = $this->createMock(ClassMetadataFactoryInterface::class);
83-
84-
$command = new DebugCommand($serializer);
81+
$command = new DebugCommand(new ClassMetadataFactory(new AttributeLoader()));
8582

8683
$tester = new CommandTester($command);
8784
$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
@@ -42,8 +42,8 @@ public function testForwardsToEncoder()
4242

4343
public function testCollectEncodingData()
4444
{
45-
$encoder = $this->createMock(EncoderInterface::class);
46-
$decoder = $this->createMock(DecoderInterface::class);
45+
$encoder = $this->createStub(EncoderInterface::class);
46+
$decoder = $this->createStub(DecoderInterface::class);
4747

4848
$dataCollector = $this->createMock(SerializerDataCollector::class);
4949
$dataCollector
@@ -61,8 +61,8 @@ public function testCollectEncodingData()
6161

6262
public function testNotCollectEncodingDataIfNoDebugTraceId()
6363
{
64-
$encoder = $this->createMock(EncoderInterface::class);
65-
$decoder = $this->createMock(DecoderInterface::class);
64+
$encoder = $this->createStub(EncoderInterface::class);
65+
$decoder = $this->createStub(DecoderInterface::class);
6666

6767
$dataCollector = $this->createMock(SerializerDataCollector::class);
6868
$dataCollector->expects($this->never())->method('collectEncoding');
@@ -76,22 +76,22 @@ public function testCannotEncodeIfNotEncoder()
7676
{
7777
$this->expectException(\BadMethodCallException::class);
7878

79-
(new TraceableEncoder($this->createMock(DecoderInterface::class), new SerializerDataCollector()))->encode('data', 'format');
79+
(new TraceableEncoder($this->createStub(DecoderInterface::class), new SerializerDataCollector()))->encode('data', 'format');
8080
}
8181

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

86-
(new TraceableEncoder($this->createMock(EncoderInterface::class), new SerializerDataCollector()))->decode('data', 'format');
86+
(new TraceableEncoder($this->createStub(EncoderInterface::class), new SerializerDataCollector()))->decode('data', 'format');
8787
}
8888

8989
public function testSupports()
9090
{
91-
$encoder = $this->createMock(EncoderInterface::class);
91+
$encoder = $this->createStub(EncoderInterface::class);
9292
$encoder->method('supportsEncoding')->willReturn(true);
9393

94-
$decoder = $this->createMock(DecoderInterface::class);
94+
$decoder = $this->createStub(DecoderInterface::class);
9595
$decoder->method('supportsDecoding')->willReturn(true);
9696

9797
$traceableEncoder = new TraceableEncoder($encoder, new SerializerDataCollector());

Tests/Debug/TraceableNormalizerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function testForwardsToNormalizer()
4444

4545
public function testCollectNormalizationData()
4646
{
47-
$normalizer = $this->createMock(NormalizerInterface::class);
47+
$normalizer = $this->createStub(NormalizerInterface::class);
4848
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
49-
$denormalizer = $this->createMock(DenormalizerInterface::class);
49+
$denormalizer = $this->createStub(DenormalizerInterface::class);
5050
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
5151

5252
$dataCollector = $this->createMock(SerializerDataCollector::class);
@@ -65,9 +65,9 @@ public function testCollectNormalizationData()
6565

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

7373
$dataCollector = $this->createMock(SerializerDataCollector::class);
@@ -82,23 +82,23 @@ public function testCannotNormalizeIfNotNormalizer()
8282
{
8383
$this->expectException(\BadMethodCallException::class);
8484

85-
(new TraceableNormalizer($this->createMock(DenormalizerInterface::class), new SerializerDataCollector()))->normalize('data');
85+
(new TraceableNormalizer($this->createStub(DenormalizerInterface::class), new SerializerDataCollector()))->normalize('data');
8686
}
8787

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

92-
(new TraceableNormalizer($this->createMock(NormalizerInterface::class), new SerializerDataCollector()))->denormalize('data', 'type');
92+
(new TraceableNormalizer($this->createStub(NormalizerInterface::class), new SerializerDataCollector()))->denormalize('data', 'type');
9393
}
9494

9595
public function testSupports()
9696
{
97-
$normalizer = $this->createMock(NormalizerInterface::class);
97+
$normalizer = $this->createStub(NormalizerInterface::class);
9898
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
9999
$normalizer->method('supportsNormalization')->willReturn(true);
100100

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

Tests/Debug/TraceableSerializerTest.php

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

107107
public function testAddDebugTraceIdInContext()
108108
{
109-
$serializer = $this->createMock(Serializer::class);
109+
$serializer = $this->createStub(Serializer::class);
110110

111111
foreach (['serialize', 'deserialize', 'normalize', 'denormalize', 'encode', 'decode'] as $method) {
112112
$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)