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

ProxyGeneratorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
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...
8
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...
9
use Zenstruck\Foundry\Configuration;
10
use Zenstruck\Foundry\ProxyGenerator;
11
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
12
use Zenstruck\Foundry\Tests\Fixtures\Entity\Tag;
13
14
class ProxyGeneratorTest extends TestCase
15
{
16
    /** @var ObjectManager|MockObject */
17
    private $objectManager;
18
    /** @var ManagerRegistry|MockObject */
19
    private $managerRegistry;
20
    /** @var Configuration|MockObject */
21
    private $config;
22
    /** @var ProxyGenerator */
23
    private $generator;
24
25
    protected function setUp(): void
26
    {
27
        $this->objectManager = $this->createMock(ObjectManager::class);
28
        $this->managerRegistry = $this->createMock(ManagerRegistry::class);
29
        $this->config = new Configuration();
30
        $this->config->setManagerRegistry($this->managerRegistry);
31
        $this->generator = new ProxyGenerator($this->config);
32
    }
33
34
    /** @test */
35
    public function can_auto_refresh_all_methods(): void
36
    {
37
        $tag = new Tag();
38
        $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

38
        $this->managerRegistry->/** @scrutinizer ignore-call */ 
39
                                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...
39
        $proxyTag = $this->generator->generate($tag);
40
41
        $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\Persistence\ObjectManager. ( Ignorable by Annotation )

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

41
        $this->objectManager->/** @scrutinizer ignore-call */ 
42
                              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...
42
        $proxyTag->setName('proxy');
43
        $this->assertEquals('proxy', $proxyTag->getName());
44
    }
45
46
    /** @test */
47
    public function can_only_auto_refresh_getters(): void
48
    {
49
        $tag = new Tag();
50
        $this->managerRegistry->expects($this->any())->method('getManagerForClass')->with(Tag::class)->willReturn($this->objectManager);
51
        $proxyTag = $this->generator->generate($tag, ['get*']);
52
53
        $this->objectManager->expects($this->once())->method('refresh')->with($tag);
54
        $proxyTag->setName('proxy');
55
        $this->assertEquals('proxy', $proxyTag->getName());
56
    }
57
}
58