Skip to content

Commit 7f7058e

Browse files
authored
Don't mock what you don't own (#66)
* Do not rely on mock for Doctrine manager nor registry * Do not rely on mock for event dispatcher * Do not rely on mock for symfony normalizer nor denormalizer * Remove phpspec/prophecy-phpunit dev dependency from packages that are no longer using it
1 parent f398d72 commit 7f7058e

5 files changed

+118
-91
lines changed

Diff for: composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
}
2222
},
2323
"require-dev": {
24-
"phpspec/prophecy-phpunit": "^2.0",
2524
"phpunit/phpunit": "^9.5"
2625
},
2726
"autoload-dev": {

Diff for: tests/DenormalizeItemProcessorTest.php

+36-51
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,22 @@
88
use DateTimeImmutable;
99
use Generator;
1010
use PHPUnit\Framework\TestCase;
11-
use Prophecy\Argument;
12-
use Prophecy\PhpUnit\ProphecyTrait;
13-
use Prophecy\Prophecy\ObjectProphecy;
14-
use Symfony\Component\Serializer\Exception\ExceptionInterface;
15-
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11+
use Symfony\Component\Serializer\Exception\UnsupportedException;
1612
use Yokai\Batch\Bridge\Symfony\Serializer\DenormalizeItemProcessor;
1713
use Yokai\Batch\Job\Item\Exception\SkipItemException;
14+
use Yokai\Batch\Job\Item\Exception\SkipItemOnError;
15+
use Yokai\Batch\Tests\Bridge\Symfony\Serializer\Dummy\DummyNormalizer;
16+
use Yokai\Batch\Tests\Bridge\Symfony\Serializer\Dummy\FailingNormalizer;
1817

1918
final class DenormalizeItemProcessorTest extends TestCase
2019
{
21-
use ProphecyTrait;
22-
23-
/**
24-
* @var ObjectProphecy|DenormalizerInterface
25-
*/
26-
private $denormalizer;
27-
28-
protected function setUp(): void
29-
{
30-
$this->denormalizer = $this->prophesize(DenormalizerInterface::class);
31-
}
32-
3320
/**
3421
* @dataProvider sets
3522
*/
3623
public function testProcess(string $type, ?string $format, array $context, $item, $expected): void
3724
{
38-
$this->denormalizer->supportsDenormalization($item, $type, $format)
39-
->shouldBeCalled()
40-
->willReturn(true);
41-
$this->denormalizer->denormalize($item, $type, $format, $context)
42-
->shouldBeCalled()
43-
->willReturn($expected);
44-
45-
$processor = new DenormalizeItemProcessor($this->denormalizer->reveal(), $type, $format, $context);
25+
$denormalizer = new DummyNormalizer(true, $expected);
26+
$processor = new DenormalizeItemProcessor($denormalizer, $type, $format, $context);
4627

4728
self::assertSame($expected, $processor->process($item));
4829
}
@@ -52,39 +33,43 @@ public function testProcess(string $type, ?string $format, array $context, $item
5233
*/
5334
public function testUnsupported(string $type, ?string $format, array $context, $item): void
5435
{
55-
$this->expectException(SkipItemException::class);
56-
57-
$this->denormalizer->supportsDenormalization($item, $type, $format)
58-
->shouldBeCalled()
59-
->willReturn(false);
60-
$this->denormalizer->denormalize(Argument::cetera())
61-
->shouldNotBeCalled();
62-
63-
$processor = new DenormalizeItemProcessor($this->denormalizer->reveal(), $type, $format, $context);
64-
65-
$processor->process($item);
36+
$denormalizer = new DummyNormalizer(false, null);
37+
$processor = new DenormalizeItemProcessor($denormalizer, $type, $format, $context);
38+
39+
$exception = null;
40+
try {
41+
$processor->process($item);
42+
} catch (SkipItemException $exception) {
43+
// just capture the exception
44+
}
45+
46+
self::assertNotNull($exception, 'Processor has thrown an exception');
47+
$cause = $exception->getCause();
48+
self::assertInstanceOf(SkipItemOnError::class, $cause);
49+
/** @var SkipItemOnError $cause */
50+
self::assertSame('Unable to denormalize item. Not supported.', $cause->getError()->getMessage());
6651
}
6752

6853
/**
6954
* @dataProvider sets
7055
*/
7156
public function testException(string $type, ?string $format, array $context, $item): void
7257
{
73-
$this->expectException(SkipItemException::class);
74-
75-
$this->denormalizer->supportsDenormalization($item, $type, $format)
76-
->shouldBeCalled()
77-
->willReturn(true);
78-
$this->denormalizer->denormalize($item, $type, $format, $context)
79-
->shouldBeCalled()
80-
->willThrow(
81-
new class extends \Exception implements ExceptionInterface {
82-
}
83-
);
84-
85-
$processor = new DenormalizeItemProcessor($this->denormalizer->reveal(), $type, $format, $context);
86-
87-
$processor->process($item);
58+
$denormalizer = new FailingNormalizer($exceptionThrown = new UnsupportedException());
59+
$processor = new DenormalizeItemProcessor($denormalizer, $type, $format, $context);
60+
61+
$exception = null;
62+
try {
63+
$processor->process($item);
64+
} catch (SkipItemException $exception) {
65+
// just capture the exception
66+
}
67+
68+
self::assertNotNull($exception, 'Processor has thrown an exception');
69+
$cause = $exception->getCause();
70+
self::assertInstanceOf(SkipItemOnError::class, $cause);
71+
/** @var SkipItemOnError $cause */
72+
self::assertSame($exceptionThrown, $cause->getError());
8873
}
8974

9075
public function sets(): Generator

Diff for: tests/Dummy/DummyNormalizer.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Serializer\Dummy;
6+
7+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
10+
final class DummyNormalizer implements NormalizerInterface, DenormalizerInterface
11+
{
12+
public function __construct(
13+
private bool $supports,
14+
private mixed $value,
15+
) {
16+
}
17+
18+
public function supportsNormalization(mixed $data, string $format = null)
19+
{
20+
return $this->supports;
21+
}
22+
23+
public function normalize(mixed $object, string $format = null, array $context = [])
24+
{
25+
return $this->value;
26+
}
27+
28+
public function supportsDenormalization(mixed $data, string $type, string $format = null)
29+
{
30+
return $this->supports;
31+
}
32+
33+
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
34+
{
35+
return $this->value;
36+
}
37+
}

Diff for: tests/Dummy/FailingNormalizer.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Serializer\Dummy;
6+
7+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
8+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10+
11+
final class FailingNormalizer implements NormalizerInterface, DenormalizerInterface
12+
{
13+
public function __construct(
14+
private ExceptionInterface $exception,
15+
) {
16+
}
17+
18+
public function supportsNormalization(mixed $data, string $format = null)
19+
{
20+
return true;
21+
}
22+
23+
public function normalize(mixed $object, string $format = null, array $context = [])
24+
{
25+
throw $this->exception;
26+
}
27+
28+
public function supportsDenormalization(mixed $data, string $type, string $format = null)
29+
{
30+
return true;
31+
}
32+
33+
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
34+
{
35+
throw $this->exception;
36+
}
37+
}

Diff for: tests/NormalizeItemProcessorTest.php

+8-39
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,22 @@
88
use DateTimeImmutable;
99
use Generator;
1010
use PHPUnit\Framework\TestCase;
11-
use Prophecy\Argument;
12-
use Prophecy\PhpUnit\ProphecyTrait;
13-
use Prophecy\Prophecy\ObjectProphecy;
1411
use Symfony\Component\Serializer\Exception\BadMethodCallException;
15-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1612
use Yokai\Batch\Bridge\Symfony\Serializer\NormalizeItemProcessor;
1713
use Yokai\Batch\Job\Item\Exception\SkipItemException;
1814
use Yokai\Batch\Job\Item\Exception\SkipItemOnError;
15+
use Yokai\Batch\Tests\Bridge\Symfony\Serializer\Dummy\DummyNormalizer;
16+
use Yokai\Batch\Tests\Bridge\Symfony\Serializer\Dummy\FailingNormalizer;
1917

2018
final class NormalizeItemProcessorTest extends TestCase
2119
{
22-
use ProphecyTrait;
23-
24-
/**
25-
* @var ObjectProphecy|NormalizerInterface
26-
*/
27-
private $normalizer;
28-
29-
protected function setUp(): void
30-
{
31-
$this->normalizer = $this->prophesize(NormalizerInterface::class);
32-
}
33-
3420
/**
3521
* @dataProvider sets
3622
*/
3723
public function testProcess(?string $format, array $context, $item, $expected): void
3824
{
39-
$this->normalizer->supportsNormalization($item, $format)
40-
->shouldBeCalled()
41-
->willReturn(true);
42-
$this->normalizer->normalize($item, $format, $context)
43-
->shouldBeCalled()
44-
->willReturn($expected);
45-
46-
$processor = new NormalizeItemProcessor($this->normalizer->reveal(), $format, $context);
25+
$normalizer = new DummyNormalizer(true, $expected);
26+
$processor = new NormalizeItemProcessor($normalizer, $format, $context);
4727

4828
self::assertSame($expected, $processor->process($item));
4929
}
@@ -53,13 +33,8 @@ public function testProcess(?string $format, array $context, $item, $expected):
5333
*/
5434
public function testUnsupported(?string $format, array $context, $item): void
5535
{
56-
$this->normalizer->supportsNormalization($item, $format)
57-
->shouldBeCalled()
58-
->willReturn(false);
59-
$this->normalizer->normalize(Argument::cetera())
60-
->shouldNotBeCalled();
61-
62-
$processor = new NormalizeItemProcessor($this->normalizer->reveal(), $format, $context);
36+
$normalizer = new DummyNormalizer(false, null);
37+
$processor = new NormalizeItemProcessor($normalizer, $format, $context);
6338

6439
$exception = null;
6540
try {
@@ -80,14 +55,8 @@ public function testUnsupported(?string $format, array $context, $item): void
8055
*/
8156
public function testException(?string $format, array $context, $item): void
8257
{
83-
$this->normalizer->supportsNormalization($item, $format)
84-
->shouldBeCalled()
85-
->willReturn(true);
86-
$this->normalizer->normalize($item, $format, $context)
87-
->shouldBeCalled()
88-
->willThrow($exceptionThrown = new BadMethodCallException());
89-
90-
$processor = new NormalizeItemProcessor($this->normalizer->reveal(), $format, $context);
58+
$normalizer = new FailingNormalizer($exceptionThrown = new BadMethodCallException());
59+
$processor = new NormalizeItemProcessor($normalizer, $format, $context);
9160

9261
$exception = null;
9362
try {

0 commit comments

Comments
 (0)