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

ProxyGeneratorTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 65
c 1
b 0
f 0
dl 0
loc 197
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A can_refetch_object_if_object_manager_has_been_cleared() 0 3 1
A can_autorefresh_between_kernel_boots() 0 15 1
A without_auto_refresh_keeps_disabled_if_originally_disabled() 0 23 1
A without_auto_refresh_solves_the_auto_refresh_problem() 0 15 1
A can_force_set_and_save() 0 3 1
A force_set_all_solves_the_auto_refresh_problem() 0 3 1
A without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disabled_originally() 0 21 1
A exception_thrown_if_trying_to_refresh_deleted_object() 0 3 1
A can_remove_and_assert_not_persisted() 0 3 1
A can_assert_persisted() 0 3 1
A exception_thrown_if_trying_to_autorefresh_object_with_unsaved_changes() 0 12 1
A can_force_set_multiple_fields() 0 3 1
A can_autorefresh_entity_with_embedded_object() 0 17 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional;
4
5
use Zenstruck\Foundry\AnonymousFactory;
6
use Zenstruck\Foundry\Tests\Fixtures\Entity\Contact;
7
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
8
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithProxyGenerator;
9
10
/**
11
 * @author Wouter de Jong <[email protected]>
12
 */
13
final class ProxyGeneratorTest extends ProxyTest
14
{
15
    protected static $POST_FACTORY = PostFactoryWithProxyGenerator::class;
16
17
    /**
18
     * @test
19
     */
20
    public function can_assert_persisted(): void
21
    {
22
        $this->markTestSkipped('not supported');
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function can_remove_and_assert_not_persisted(): void
29
    {
30
        $this->markTestSkipped('not supported');
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function can_refetch_object_if_object_manager_has_been_cleared(): void
37
    {
38
        $this->markTestSkipped('not supported');
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function exception_thrown_if_trying_to_refresh_deleted_object(): void
45
    {
46
        $this->markTestSkipped('not supported');
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function can_force_set_and_save(): void
53
    {
54
        $this->markTestSkipped('not supported');
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function can_force_set_multiple_fields(): void
61
    {
62
        $this->markTestSkipped('not supported');
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function exception_thrown_if_trying_to_autorefresh_object_with_unsaved_changes(): void
69
    {
70
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
71
72
        $this->assertSame('old title', $post->getTitle());
73
        $this->assertSame('old body', $post->getBody());
74
75
        $post->setTitle('new title');
76
77
        $this->expectException(\RuntimeException::class);
78
79
        $post->setBody('new body');
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function can_autorefresh_between_kernel_boots(): void
86
    {
87
        $this->markTestSkipped('not supported');
88
89
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
90
91
        $this->assertSame('old title', $post->getTitle());
92
        $this->assertSame('old body', $post->getBody());
93
94
        // reboot kernel
95
        static::ensureKernelShutdown();
96
        static::bootKernel();
97
98
        $this->assertSame('old title', $post->getTitle());
99
        $this->assertSame('old body', $post->getBody());
100
    }
101
102
    /**
103
     * @test
104
     */
105
    public function can_autorefresh_entity_with_embedded_object(): void
106
    {
107
        $contactFactory = AnonymousFactory::new(Contact::class)->withProxyGenerator();
108
        $contact = $contactFactory->create(['name' => 'john']);
109
110
        $this->assertSame('john', $contact->getName());
111
112
        // I discovered when autorefreshing the second time, the embedded
113
        // object is included in the changeset when using UOW::recomputeSingleEntityChangeSet().
114
        // Changing to UOW::computeChangeSet() fixes this.
115
        $this->assertSame('john', $contact->getName());
116
        $this->assertNull($contact->getAddress()->getValue());
117
118
        $contact->getAddress()->setValue('address');
119
        $contactFactory->persist($contact);
120
121
        $this->assertSame('address', $contact->getAddress()->getValue());
122
123
        // todo unsupported
124
        //static::ensureKernelShutdown();
125
        //static::bootKernel();
126
127
        //$this->assertSame('address', $contact->getAddress()->getValue());
128
    }
129
130
    /**
131
     * @test
132
     */
133
    public function force_set_all_solves_the_auto_refresh_problem(): void
134
    {
135
        $this->markTestSkipped('not supported');
136
    }
137
138
    /**
139
     * @test
140
     */
141
    public function without_auto_refresh_solves_the_auto_refresh_problem(): void
142
    {
143
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
144
145
        $this->assertSame('old title', $post->getTitle());
146
        $this->assertSame('old body', $post->getBody());
147
148
        static::$POST_FACTORY::withoutAutoRefresh($post, static function(Post $post) {
149
            $post->setTitle('new title');
150
            $post->setBody('new body');
151
        });
152
        static::$POST_FACTORY::persist($post);
153
154
        $this->assertSame('new title', $post->getTitle());
155
        $this->assertSame('new body', $post->getBody());
156
    }
157
158
    /**
159
     * @test
160
     */
161
    public function without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disabled_originally(): void
162
    {
163
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
164
165
        static::$POST_FACTORY::disableAutoRefresh($post);
166
167
        $this->assertSame('old title', $post->getTitle());
168
        $this->assertSame('old body', $post->getBody());
169
170
        static::$POST_FACTORY::withoutAutoRefresh($post, static function(Post $post) {
171
            $post->setTitle('new title');
172
            $post->setBody('new body');
173
        });
174
175
        $post->setTitle('another new title');
176
        $post->setBody('another new body');
177
178
        static::$POST_FACTORY::persist($post);
179
180
        $this->assertSame('another new title', $post->getTitle());
181
        $this->assertSame('another new body', $post->getBody());
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function without_auto_refresh_keeps_disabled_if_originally_disabled(): void
188
    {
189
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
190
191
        static::$POST_FACTORY::disableAutoRefresh($post);
192
193
        $this->assertSame('old title', $post->getTitle());
194
        $this->assertSame('old body', $post->getBody());
195
196
        static::$POST_FACTORY::withoutAutoRefresh($post, static function(Post $post) {
197
            $post->setTitle('new title');
198
            $post->setBody('new body');
199
        });
200
201
        static::$POST_FACTORY::persist($post);
202
203
        $post->setTitle('another new title');
204
        $post->setBody('another new body');
205
206
        static::$POST_FACTORY::persist($post);
207
208
        $this->assertSame('another new title', $post->getTitle());
209
        $this->assertSame('another new body', $post->getBody());
210
    }
211
}
212