| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zenstruck\Foundry\Tests\Functional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Zenstruck\Foundry\AnonymousFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Zenstruck\Foundry\Test\Factories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Zenstruck\Foundry\Test\ResetDatabase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Zenstruck\Foundry\Tests\Fixtures\Entity\Address; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Zenstruck\Foundry\Tests\Fixtures\Entity\Tag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function Zenstruck\Foundry\create; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function Zenstruck\Foundry\factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Kevin Bond <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | final class FactoryTest extends KernelTestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     use Factories, ResetDatabase; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function many_to_one_relationship(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $categoryFactory = factory(Category::class, ['name' => 'foo']); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $category = create(Category::class, ['name' => 'bar']); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $postA = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $categoryFactory]); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $postB = create(Post::class, ['title' => 'title', 'body' => 'body', 'category' => $category]); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->assertSame('foo', $postA->getCategory()->getName()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertSame('bar', $postB->getCategory()->getName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     public function one_to_many_relationship(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $category = create(Category::class, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             'name' => 'bar', | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             'posts' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                 factory(Post::class, ['title' => 'Post A', 'body' => 'body']), | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                 create(Post::class, ['title' => 'Post B', 'body' => 'body']), | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $posts = \array_map( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             static function($post) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 return $post->getTitle(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             }, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $category->getPosts()->toArray() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertCount(2, $posts); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertContains('Post A', $posts); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertContains('Post B', $posts); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     public function many_to_many_relationship(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $post = create(Post::class, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             'title' => 'title', | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             'body' => 'body', | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             'tags' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 factory(Tag::class, ['name' => 'Tag A']), | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 create(Tag::class, ['name' => 'Tag B']), | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $tags = \array_map( | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             static function($tag) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 return $tag->getName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             }, | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             $post->getTags()->toArray() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $this->assertCount(2, $tags); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $this->assertContains('Tag A', $tags); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $this->assertContains('Tag B', $tags); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function many_to_many_reverse_relationship(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $tag = create(Tag::class, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'name' => 'bar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'posts' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 factory(Post::class, ['title' => 'Post A', 'body' => 'body']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 create(Post::class, ['title' => 'Post B', 'body' => 'body']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $posts = \array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             static function($post) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 return $post->getTitle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $tag->getPosts()->toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertCount(2, $posts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertContains('Post A', $posts); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |         $this->assertContains('Post B', $posts); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     public function creating_with_factory_attribute_persists_the_factory(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $object = (new AnonymousFactory(Post::class))->create([ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |             'title' => 'title', | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             'body' => 'body', | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             'category' => new AnonymousFactory(Category::class, ['name' => 'name']), | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         $this->assertNotNull($object->getCategory()->getId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function can_create_embeddable(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $object1 = (new AnonymousFactory(Address::class))->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $object2 = (new AnonymousFactory(Address::class))->create(['value' => 'an address']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->assertNull($object1->getValue()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |         $this->assertSame('an address', $object2->getValue()); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 139 |  |  |  |