Passed
Pull Request — master (#153)
by Kevin
03:37
created

ORMProxyTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 66
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A can_autorefresh_entity_with_embedded_object() 0 23 1
A setUp() 0 4 2
A cannot_convert_to_string_if_underlying_object_cant() 0 6 1
A on_php_versions_less_than_7_4_if_underlying_object_is_missing_to_string_proxy_to_string_returns_note() 0 3 1
A postFactoryClass() 0 3 1
A registryServiceId() 0 3 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional;
4
5
use Zenstruck\Foundry\AnonymousFactory;
6
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
7
use Zenstruck\Foundry\Tests\Fixtures\Entity\Contact;
8
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
9
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class ORMProxyTest extends ProxyTest
15
{
16
    protected function setUp(): void
17
    {
18
        if (false === \getenv('DATABASE_URL')) {
19
            self::markTestSkipped('doctrine/orm not enabled.');
20
        }
21
    }
22
23
    /**
24
     * @test
25
     * @requires PHP >= 7.4
26
     */
27
    public function cannot_convert_to_string_if_underlying_object_cant(): void
28
    {
29
        $this->expectException(\RuntimeException::class);
30
        $this->expectExceptionMessage(\sprintf('Proxied object "%s" cannot be converted to a string.', Category::class));
31
32
        (string) CategoryFactory::createOne();
33
    }
34
35
    /**
36
     * @test
37
     * @requires PHP < 7.4
38
     */
39
    public function on_php_versions_less_than_7_4_if_underlying_object_is_missing_to_string_proxy_to_string_returns_note(): void
40
    {
41
        $this->assertSame('(no __toString)', (string) CategoryFactory::createOne());
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function can_autorefresh_entity_with_embedded_object(): void
48
    {
49
        $contact = AnonymousFactory::new(Contact::class)->create(['name' => 'john'])
50
            ->enableAutoRefresh()
51
        ;
52
53
        $this->assertSame('john', $contact->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

53
        $this->assertSame('john', $contact->/** @scrutinizer ignore-call */ getName());
Loading history...
54
55
        // I discovered when autorefreshing the second time, the embedded
56
        // object is included in the changeset when using UOW::recomputeSingleEntityChangeSet().
57
        // Changing to UOW::computeChangeSet() fixes this.
58
        $this->assertSame('john', $contact->getName());
59
        $this->assertNull($contact->getAddress()->getValue());
0 ignored issues
show
Bug introduced by
The method getAddress() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

59
        $this->assertNull($contact->/** @scrutinizer ignore-call */ getAddress()->getValue());
Loading history...
60
61
        $contact->getAddress()->setValue('address');
62
        $contact->save();
63
64
        $this->assertSame('address', $contact->getAddress()->getValue());
65
66
        self::ensureKernelShutdown();
67
        self::bootKernel();
68
69
        $this->assertSame('address', $contact->getAddress()->getValue());
70
    }
71
72
    protected function postFactoryClass(): string
73
    {
74
        return PostFactory::class;
75
    }
76
77
    protected function registryServiceId(): string
78
    {
79
        return 'doctrine';
80
    }
81
}
82