anonymous//tests/Unit/ProxyTest.php$0
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 2
wmc 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\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...
8
use Zenstruck\Foundry\Factory;
9
use Zenstruck\Foundry\Proxy;
10
use Zenstruck\Foundry\Test\Factories;
11
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class ProxyTest extends TestCase
17
{
18
    use Factories;
19
20
    /**
21
     * @test
22
     */
23
    public function can_force_get_and_set_non_public_properties(): void
24
    {
25
        $proxy = new Proxy(new Category());
26
27
        $this->assertNull($proxy->forceGet('name'));
28
29
        $proxy->forceSet('name', 'foo');
30
31
        $this->assertSame('foo', $proxy->forceGet('name'));
32
    }
33
34
    /**
35
     * @test
36
     */
37
    public function can_access_wrapped_objects_properties(): void
38
    {
39
        $proxy = new Proxy(new class() {
40
            public $property;
41
        });
42
43
        $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...
44
45
        $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...
46
47
        $this->assertSame('foo', $proxy->property);
48
49
        $this->assertTrue(isset($proxy->property));
50
51
        unset($proxy->property);
52
53
        $this->assertFalse(isset($proxy->property));
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function cannot_refresh_unpersisted_proxy(): void
60
    {
61
        $this->expectException(\RuntimeException::class);
62
        $this->expectExceptionMessage('Cannot refresh unpersisted object (Zenstruck\Foundry\Tests\Fixtures\Entity\Category).');
63
64
        (new Proxy(new Category()))->refresh();
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function saving_unpersisted_proxy_changes_it_to_a_persisted_proxy(): void
71
    {
72
        $registry = $this->createMock(ManagerRegistry::class);
73
        $registry
74
            ->method('getManagerForClass')
75
            ->with(Category::class)
76
            ->willReturn($this->createMock(ObjectManager::class))
77
        ;
78
79
        Factory::configuration()->setManagerRegistry($registry);
80
81
        $category = new Proxy(new Category());
82
83
        $this->assertFalse($category->isPersisted());
84
85
        $category->save();
86
87
        $this->assertTrue($category->isPersisted());
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function can_use_without_auto_refresh_with_proxy_or_object_typehint(): void
94
    {
95
        $proxy = new Proxy(new Category());
96
        $calls = 0;
97
98
        $proxy
99
            ->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

99
            ->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...
100
                ++$calls;
101
            })
102
            ->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

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