| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Lamoda\QueueBundle\Tests\Unit\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JMS\Serializer\SerializerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Lamoda\QueueBundle\Entity\QueueEntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Lamoda\QueueBundle\Exception\UnexpectedValueException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Lamoda\QueueBundle\Factory\EntityFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Lamoda\QueueBundle\Job\AbstractJob; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Lamoda\QueueBundle\Tests\Unit\Job\StubJob; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Lamoda\QueueBundle\Tests\Unit\QueueEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Lamoda\QueueBundle\Tests\Unit\SymfonyMockTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class EntityFactoryTest extends PHPUnit_Framework_TestCase | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     use SymfonyMockTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param AbstractJob          $job | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @param QueueEntityInterface $expected | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @dataProvider dataCreateQueue() | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function testCreateQueue(AbstractJob $job, QueueEntityInterface $expected): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $serializer = $this->getJMSSerializer(['serialize']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $serializer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->method('serialize') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ->willReturn('{"id":1}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $factory = $this->createFactory($serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertEquals($expected, $factory->createQueue($job)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function dataCreateQueue(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 new StubJob(1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 new QueueEntity('queue', 'exchange', StubJob::class, ['id' => 1]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 (function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     $job = new StubJob(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     $job->setScheduleAt(new \DateTime('25.01.10')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     return $job; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 })(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 (function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     $queue = new QueueEntity('queue', 'exchange', StubJob::class, ['id' => 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     $queue->setScheduled(new \DateTime('25.01.10')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     return $queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 })(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function testJsonDecodeError(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->expectException(UnexpectedValueException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->expectExceptionCode(4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->expectExceptionMessage('json_decode error: Syntax error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $serializer = $this->getJMSSerializer(['serialize']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $serializer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->method('serialize') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ->willReturn('abrakadabra'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $factory = $this->createFactory($serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $factory->createQueue(new StubJob(1)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private function createFactory(SerializerInterface $serializer): EntityFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return new EntityFactory($serializer, QueueEntity::class); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |