Passed
Pull Request — master (#127)
by Wouter
02:39
created

ProxyGeneratorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 27
c 1
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A can_auto_refresh_all_methods() 0 9 1
A can_only_auto_refresh_getters() 0 9 1
A setUp() 0 15 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit\Proxy;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use Doctrine\ORM\UnitOfWork;
8
use Doctrine\Persistence\ManagerRegistry;
9
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Zenstruck\Foundry\Configuration;
12
use Zenstruck\Foundry\Proxy\ProxyGenerator;
13
use Zenstruck\Foundry\Tests\Fixtures\Entity\Tag;
14
15
class ProxyGeneratorTest extends TestCase
16
{
17
    /** @var EntityManagerInterface|MockObject */
18
    private $objectManager;
19
    /** @var ManagerRegistry|MockObject */
20
    private $managerRegistry;
21
    /** @var Configuration|MockObject */
22
    private $config;
23
    /** @var ProxyGenerator */
24
    private $generator;
25
26
    protected function setUp(): void
27
    {
28
        $unitOfWork = $this->createMock(UnitOfWork::class);
29
        $unitOfWork->expects($this->any())->method('getEntityChangeSet')->willReturn([]);
30
31
        $this->objectManager = $this->createMock(EntityManagerInterface::class);
32
        $this->objectManager->expects($this->any())->method('contains')->willReturn(true);
33
        $this->objectManager->expects($this->any())->method('getClassMetadata')->willReturn($this->createMock(ClassMetadata::class));
34
        $this->objectManager->expects($this->any())->method('getUnitOfWork')->willReturn($unitOfWork);
35
36
        $this->managerRegistry = $this->createMock(ManagerRegistry::class);
37
38
        $this->config = new Configuration();
39
        $this->config->setManagerRegistry($this->managerRegistry);
40
        $this->generator = new ProxyGenerator($this->config);
41
    }
42
43
    /** @test */
44
    public function can_auto_refresh_all_methods(): void
45
    {
46
        $tag = new Tag();
47
        $this->managerRegistry->expects($this->any())->method('getManagerForClass')->with(Tag::class)->willReturn($this->objectManager);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\Persistence\ManagerRegistry. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        $this->managerRegistry->/** @scrutinizer ignore-call */ 
48
                                expects($this->any())->method('getManagerForClass')->with(Tag::class)->willReturn($this->objectManager);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        $proxyTag = $this->generator->generate($tag);
49
50
        $this->objectManager->expects($this->exactly(2))->method('refresh')->with($tag);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\ORM\EntityManagerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $this->objectManager->/** @scrutinizer ignore-call */ 
51
                              expects($this->exactly(2))->method('refresh')->with($tag);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $proxyTag->setName('proxy');
52
        $this->assertEquals('proxy', $proxyTag->getName());
53
    }
54
55
    /** @test */
56
    public function can_only_auto_refresh_getters(): void
57
    {
58
        $tag = new Tag();
59
        $this->managerRegistry->expects($this->any())->method('getManagerForClass')->with(Tag::class)->willReturn($this->objectManager);
60
        $proxyTag = $this->generator->generate($tag, ['get*']);
61
62
        $this->objectManager->expects($this->once())->method('refresh')->with($tag);
63
        $proxyTag->setName('proxy');
64
        $this->assertEquals('proxy', $proxyTag->getName());
65
    }
66
}
67