Issues (131)

tests/Functional/ODMModelFactoryTest.php (1 issue)

Labels
Severity
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\Document\User;
10
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\CategoryFactory;
11
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\CommentFactory;
12
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\PostFactory;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
final class ODMModelFactoryTest extends ModelFactoryTest
18
{
19
    protected function setUp(): void
20
    {
21
        if (false === \getenv('MONGO_URL')) {
22
            self::markTestSkipped('doctrine/odm not enabled.');
23
        }
24
    }
25
26
    /**
27
     * @test
28
     */
29
    public function can_use_factory_for_embedded_object(): void
30
    {
31
        $proxyObject = CommentFactory::createOne(['user' => new User('some user'), 'body' => 'some body']);
32
        self::assertInstanceOf(Proxy::class, $proxyObject);
33
        self::assertFalse($proxyObject->isPersisted());
34
35
        $comment = $proxyObject->object();
36
        self::assertInstanceOf(Comment::class, $comment);
37
        self::assertEquals(new User('some user'), $comment->getUser());
38
        self::assertSame('some body', $comment->getBody());
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function can_hydrate_embed_many_fields(): void
45
    {
46
        PostFactory::createOne([
47
            'title' => 'foo',
48
            'comments' => CommentFactory::new()->many(4),
49
        ]);
50
51
        $posts = PostFactory::findBy(['title' => 'foo']);
52
        self::assertCount(1, $posts);
53
54
        $post = $posts[0]->object();
55
        self::assertInstanceOf(Post::class, $post);
56
        self::assertCount(4, $post->getComments());
57
        self::assertContainsOnlyInstancesOf(Comment::class, $post->getComments());
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function can_create_one_with_nested_embedded(): void
64
    {
65
        PostFactory::new()->withComments()->create(['title' => 'foo']);
0 ignored issues
show
The method withComments() does not exist on Zenstruck\Foundry\ModelFactory. 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

65
        PostFactory::new()->/** @scrutinizer ignore-call */ withComments()->create(['title' => 'foo']);
Loading history...
66
67
        $posts = PostFactory::findBy(['title' => 'foo']);
68
        self::assertCount(1, $posts);
69
    }
70
71
    protected function categoryClass(): string
72
    {
73
        return Category::class;
74
    }
75
76
    protected function categoryFactoryClass(): string
77
    {
78
        return CategoryFactory::class;
79
    }
80
}
81