Passed
Push — master ( fb5b4f...f054e3 )
by Kevin
03:29
created

ProxyTest.php$0 ➔ functionWithNoTypeHint()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
use Zenstruck\Foundry\Proxy;
8
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
9
use Zenstruck\Foundry\Tests\UnitTestCase;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class ProxyTest extends UnitTestCase
15
{
16
    /**
17
     * @test
18
     */
19
    public function can_force_get_and_set_non_public_properties(): void
20
    {
21
        $proxy = new Proxy(new Category());
22
23
        $this->assertNull($proxy->forceGet('name'));
24
25
        $proxy->forceSet('name', 'foo');
26
27
        $this->assertSame('foo', $proxy->forceGet('name'));
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function can_access_wrapped_objects_properties(): void
34
    {
35
        $proxy = new Proxy(new class() {
36
            public $property;
37
        });
38
39
        $this->assertFalse(isset($proxy->property));
0 ignored issues
show
Bug Best Practice introduced by
The property property does not exist on Zenstruck\Foundry\Proxy. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
41
        $proxy->property = 'foo';
0 ignored issues
show
Bug Best Practice introduced by
The property property does not exist on Zenstruck\Foundry\Proxy. Since you implemented __set, consider adding a @property annotation.
Loading history...
42
43
        $this->assertSame('foo', $proxy->property);
44
45
        $this->assertTrue(isset($proxy->property));
46
47
        unset($proxy->property);
48
49
        $this->assertFalse(isset($proxy->property));
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function cannot_refresh_unpersisted_proxy(): void
56
    {
57
        $this->expectException(\RuntimeException::class);
58
        $this->expectExceptionMessage('Cannot refresh unpersisted object (Zenstruck\Foundry\Tests\Fixtures\Entity\Category).');
59
60
        (new Proxy(new Category()))->refresh();
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function saving_unpersisted_proxy_changes_it_to_a_persisted_proxy(): void
67
    {
68
        $registry = $this->createMock(ManagerRegistry::class);
69
        $registry
70
            ->expects($this->exactly(2))
71
            ->method('getManagerForClass')
72
            ->with(Category::class)
73
            ->willReturn($this->createMock(ObjectManager::class))
74
        ;
75
76
        $this->configuration->setManagerRegistry($registry);
0 ignored issues
show
Bug introduced by
The method setManagerRegistry() does not exist on null. ( Ignorable by Annotation )

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

76
        $this->configuration->/** @scrutinizer ignore-call */ 
77
                              setManagerRegistry($registry);

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...
77
78
        $category = new Proxy(new Category());
79
80
        $this->assertFalse($category->isPersisted());
81
82
        $category->save();
83
84
        $this->assertTrue($category->isPersisted());
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function can_use_without_auto_refresh_with_proxy_or_object_typehint(): void
91
    {
92
        $proxy = new Proxy(new Category());
93
        $calls = 0;
94
95
        $proxy
96
            ->withoutAutoRefresh(static function(Proxy $proxy) use (&$calls) {
0 ignored issues
show
Unused Code introduced by
The parameter $proxy is not used and could be removed. ( Ignorable by Annotation )

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

96
            ->withoutAutoRefresh(static function(/** @scrutinizer ignore-unused */ Proxy $proxy) use (&$calls) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
                ++$calls;
98
            })
99
            ->withoutAutoRefresh(static function(Category $category) use (&$calls) {
0 ignored issues
show
Unused Code introduced by
The parameter $category is not used and could be removed. ( Ignorable by Annotation )

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

99
            ->withoutAutoRefresh(static function(/** @scrutinizer ignore-unused */ Category $category) use (&$calls) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
                ++$calls;
101
            })
102
            ->withoutAutoRefresh(function($proxy) use (&$calls) {
103
                $this->assertInstanceOf(Proxy::class, $proxy);
104
                ++$calls;
105
            })
106
            ->withoutAutoRefresh(static function() use (&$calls) {
107
                ++$calls;
108
            })
109
            ->withoutAutoRefresh([$this, 'functionWithProxy'])
110
            ->withoutAutoRefresh([$this, 'functionWithObject'])
111
            ->withoutAutoRefresh([$this, 'functionWithNoTypeHint'])
112
            ->withoutAutoRefresh([self::class, 'staticFunctionWithProxy'])
113
            ->withoutAutoRefresh([self::class, 'staticFunctionWithObject'])
114
            ->withoutAutoRefresh([self::class, 'staticFunctionWithNoTypeHint'])
115
        ;
116
117
        $this->assertSame(4, $calls);
118
    }
119
120
    public function functionWithProxy(Proxy $proxy)
121
    {
122
        $this->assertInstanceOf(Category::class, $proxy->object());
123
    }
124
125
    public function functionWithObject(Category $category)
126
    {
127
        $this->assertInstanceOf(Category::class, $category);
128
    }
129
130
    public function functionWithNoTypeHint($proxy)
131
    {
132
        $this->assertInstanceOf(Proxy::class, $proxy);
133
        $this->assertInstanceOf(Category::class, $proxy->object());
134
    }
135
136
    public static function staticFunctionWithProxy(Proxy $proxy)
137
    {
138
        self::assertInstanceOf(Category::class, $proxy->object());
139
    }
140
141
    public static function staticFunctionWithObject(Category $category)
142
    {
143
        self::assertInstanceOf(Category::class, $category);
144
    }
145
146
    public static function staticFunctionWithNoTypeHint($proxy)
147
    {
148
        self::assertInstanceOf(Proxy::class, $proxy);
149
        self::assertInstanceOf(Category::class, $proxy->object());
150
    }
151
}
152