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

can_hydrate_embed_many_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional;
4
5
use Zenstruck\Foundry\Proxy;
6
use Zenstruck\Foundry\Tests\Fixtures\Document\Category;
7
use Zenstruck\Foundry\Tests\Fixtures\Document\Comment;
8
use Zenstruck\Foundry\Tests\Fixtures\Document\Post;
9
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\CategoryFactory;
10
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\CommentFactory;
11
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\PostFactory;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class ODMModelFactoryTest extends ModelFactoryTest
17
{
18
    protected function setUp(): void
19
    {
20
        if (false === \getenv('MONGO_URL')) {
21
            self::markTestSkipped('doctrine/odm not enabled.');
22
        }
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function can_use_factory_for_embedded_object(): void
29
    {
30
        $proxyObject = CommentFactory::createOne(['user' => 'some user', 'body' => 'some body']);
31
        self::assertInstanceOf(Proxy::class, $proxyObject);
32
        self::assertFalse($proxyObject->isPersisted());
33
34
        $comment = $proxyObject->object();
35
        self::assertInstanceOf(Comment::class, $comment);
36
        self::assertSame('some user', $comment->getUser());
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function can_hydrate_embed_many_fields(): void
43
    {
44
        PostFactory::createOne([
45
            'title' => 'foo',
46
            'comments' => CommentFactory::new()->many(4),
47
        ]);
48
49
        $posts = PostFactory::findBy(['title' => 'foo']);
50
        self::assertCount(1, $posts);
51
52
        $post = $posts[0]->object();
53
        self::assertInstanceOf(Post::class, $post);
54
        self::assertCount(4, $post->getComments());
55
        self::assertContainsOnlyInstancesOf(Comment::class, $post->getComments());
56
    }
57
58
    protected function categoryClass(): string
59
    {
60
        return Category::class;
61
    }
62
63
    protected function categoryFactoryClass(): string
64
    {
65
        return CategoryFactory::class;
66
    }
67
}
68