Passed
Pull Request — master (#127)
by Wouter
03:31
created

can_remove_and_assert_not_persisted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
88
89
        $this->assertSame('old title', $post->getTitle());
90
        $this->assertSame('old body', $post->getBody());
91
92
        // reboot kernel
93
        static::ensureKernelShutdown();
94
        static::bootKernel();
95
96
        $this->assertSame('old title', $post->getTitle());
97
        $this->assertSame('old body', $post->getBody());
98
    }
99
100
    /**
101
     * @test
102
     */
103
    public function can_autorefresh_entity_with_embedded_object(): void
104
    {
105
        $contactFactory = AnonymousFactory::new(Contact::class)->withProxyGenerator();
106
        $contact = $contactFactory->create(['name' => 'john']);
107
108
        $this->assertSame('john', $contact->getName());
109
110
        // I discovered when autorefreshing the second time, the embedded
111
        // object is included in the changeset when using UOW::recomputeSingleEntityChangeSet().
112
        // Changing to UOW::computeChangeSet() fixes this.
113
        $this->assertSame('john', $contact->getName());
114
        $this->assertNull($contact->getAddress()->getValue());
115
116
        $contact->getAddress()->setValue('address');
117
        $contactFactory->persist($contact);
118
119
        $this->assertSame('address', $contact->getAddress()->getValue());
120
121
        // todo unsupported
122
        //static::ensureKernelShutdown();
123
        //static::bootKernel();
124
125
        //$this->assertSame('address', $contact->getAddress()->getValue());
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function force_set_all_solves_the_auto_refresh_problem(): void
132
    {
133
        $this->markTestSkipped('not supported');
134
    }
135
136
    /**
137
     * @test
138
     */
139
    public function without_auto_refresh_solves_the_auto_refresh_problem(): void
140
    {
141
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
142
143
        $this->assertSame('old title', $post->getTitle());
144
        $this->assertSame('old body', $post->getBody());
145
146
        static::$POST_FACTORY::withoutAutoRefresh($post, static function(Post $post) {
147
            $post->setTitle('new title');
148
            $post->setBody('new body');
149
        });
150
        static::$POST_FACTORY::persist($post);
151
152
        $this->assertSame('new title', $post->getTitle());
153
        $this->assertSame('new body', $post->getBody());
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disabled_originally(): void
160
    {
161
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
162
163
        static::$POST_FACTORY::disableAutoRefresh($post);
164
165
        $this->assertSame('old title', $post->getTitle());
166
        $this->assertSame('old body', $post->getBody());
167
168
        static::$POST_FACTORY::withoutAutoRefresh($post, static function(Post $post) {
169
            $post->setTitle('new title');
170
            $post->setBody('new body');
171
        });
172
173
        $post->setTitle('another new title');
174
        $post->setBody('another new body');
175
176
        static::$POST_FACTORY::persist($post);
177
178
        $this->assertSame('another new title', $post->getTitle());
179
        $this->assertSame('another new body', $post->getBody());
180
    }
181
182
    /**
183
     * @test
184
     */
185
    public function without_auto_refresh_keeps_disabled_if_originally_disabled(): void
186
    {
187
        $post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']);
188
189
        static::$POST_FACTORY::disableAutoRefresh($post);
190
191
        $this->assertSame('old title', $post->getTitle());
192
        $this->assertSame('old body', $post->getBody());
193
194
        static::$POST_FACTORY::withoutAutoRefresh($post, static function(Post $post) {
195
            $post->setTitle('new title');
196
            $post->setBody('new body');
197
        });
198
199
        static::$POST_FACTORY::persist($post);
200
201
        $post->setTitle('another new title');
202
        $post->setBody('another new body');
203
204
        static::$POST_FACTORY::persist($post);
205
206
        $this->assertSame('another new title', $post->getTitle());
207
        $this->assertSame('another new body', $post->getBody());
208
    }
209
}
210