|
3 | 3 | namespace spec\Vivait\StringGeneratorBundle\EventListener; |
4 | 4 |
|
5 | 5 | use Doctrine\Common\Annotations\Reader; |
| 6 | + |
| 7 | + |
| 8 | +use Doctrine\ORM\EntityManagerInterface; |
| 9 | +use Doctrine\ORM\EntityRepository; |
| 10 | +use Doctrine\ORM\Event\LifecycleEventArgs; |
| 11 | +use Doctrine\ORM\Mapping\ClassMetadata; |
6 | 12 | use PhpSpec\ObjectBehavior; |
7 | 13 | use Prophecy\Argument; |
8 | 14 | use Vivait\StringGeneratorBundle\Annotation\GeneratorAnnotation; |
|
11 | 17 |
|
12 | 18 | class GeneratorListenerSpec extends ObjectBehavior |
13 | 19 | { |
| 20 | + public $mockEntity; |
| 21 | + |
14 | 22 | function it_is_initializable() |
15 | 23 | { |
16 | 24 | $this->shouldHaveType('Vivait\StringGeneratorBundle\EventListener\GeneratorListener'); |
17 | 25 | } |
18 | 26 |
|
19 | | - function let(Reader $reader, Registry $registry) |
| 27 | + function let(Reader $reader, Registry $registry, EntityRepository $entityRepository, ClassMetadata $meta, EntityManagerInterface $entityManager, LifecycleEventArgs $args) |
20 | 28 | { |
21 | 29 | $this->beConstructedWith($reader, $registry); |
| 30 | + |
| 31 | + //Set up for EM etc. |
| 32 | + $this->mockEntity = new Entity(); |
| 33 | + $args->getEntity()->willReturn($this->mockEntity); |
| 34 | + $args->getEntityManager()->willReturn($entityManager); |
| 35 | + $entityManager->getClassMetadata(Argument::any())->willReturn($meta); |
| 36 | + $entityManager->getRepository(Argument::any())->willReturn($entityRepository); |
| 37 | + $meta->getName()->willReturn('Entity'); |
| 38 | + |
22 | 39 | } |
23 | 40 |
|
24 | | - function it_performs_callbacks_on_the_generator(StringGenerator $generator, Entity $mockEntity) |
| 41 | + function it_performs_callbacks_on_the_generator(StringGenerator $generator) |
25 | 42 | { |
26 | 43 | $annotation = new GeneratorAnnotation([]); |
27 | 44 | $annotation->callbacks = ['setChars' => 'abcdef']; |
28 | | - $this->shouldNotThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $mockEntity); |
| 45 | + $this->shouldNotThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $this->mockEntity); |
29 | 46 |
|
30 | 47 | $annotation->callbacks = ['noMethod' => 'something']; |
31 | | - $this->shouldThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $mockEntity); |
| 48 | + $this->shouldThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $this->mockEntity); |
32 | 49 | } |
33 | 50 |
|
34 | | - function it_can_get_callback_values_from_annotated_object(StringGenerator $generator, Entity $mockEntity) |
| 51 | + function it_can_get_callback_values_from_annotated_object(StringGenerator $generator) |
35 | 52 | { |
36 | 53 | $annotation = new GeneratorAnnotation([]); |
37 | | - $annotation->callbacks = ['setPrefix' => 'getPrefix']; |
| 54 | + $annotation->callbacks = ['setPrefix' => 'createPrefix']; |
38 | 55 |
|
39 | | - $mockEntity->getPrefix()->willReturn('VIVA_'); |
| 56 | + $this->mockEntity->createPrefix(); |
40 | 57 | $generator->setPrefix('VIVA_')->shouldBeCalled(); |
41 | | - $this->performCallbacks($generator, $annotation, $mockEntity); |
| 58 | + $this->performCallbacks($generator, $annotation, $this->mockEntity); |
| 59 | + } |
| 60 | + |
| 61 | + function it_sets_null_properties_if_override_set_to_true(Registry $registry, Reader $reader, LifecycleEventArgs $args, StringGenerator $generator) |
| 62 | + { |
| 63 | + $annotation = new GeneratorAnnotation([]); |
| 64 | + $annotation->override = false; |
| 65 | + $annotation->unique = false; |
| 66 | + |
| 67 | + $reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]); |
| 68 | + |
| 69 | + $registry->get(Argument::any())->willReturn($generator)->shouldBeCalled(); |
| 70 | + $generator->generate()->shouldBeCalled(); |
| 71 | + $generator->setLength(Argument::any())->shouldBeCalled(); |
| 72 | + |
| 73 | + $this->prePersist($args); |
| 74 | + } |
| 75 | + function it_wont_set_non_null_properties_if_override_set_to_true(Reader $reader, LifecycleEventArgs $args, StringGenerator $generator) |
| 76 | + { |
| 77 | + $this->mockEntity->setName('Robin'); |
| 78 | + $annotation = new GeneratorAnnotation([]); |
| 79 | + $annotation->override = false; |
| 80 | + |
| 81 | + $reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]); |
| 82 | + $generator->generate()->shouldNotBeCalled(); |
| 83 | + |
| 84 | + $this->prePersist($args); |
| 85 | + } |
| 86 | + |
| 87 | + function it_generates_a_string_on_a_property(Registry $registry, Reader $reader, LifecycleEventArgs $args, StringGenerator $generator) |
| 88 | + { |
| 89 | + $annotation = new GeneratorAnnotation([]); |
| 90 | + |
| 91 | + $reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]); |
| 92 | + |
| 93 | + $registry->get(Argument::any())->willReturn($generator); |
| 94 | + $generator->generate()->shouldBeCalled(); |
| 95 | + $generator->setLength(Argument::any())->shouldBeCalled(); |
| 96 | + |
| 97 | + $this->prePersist($args); |
42 | 98 | } |
43 | 99 | } |
44 | 100 |
|
45 | 101 | class Entity |
46 | 102 | { |
47 | | - public function getPrefix() |
| 103 | + private $name; |
| 104 | + |
| 105 | + public function createPrefix() |
48 | 106 | { |
| 107 | + return 'VIVA_'; |
| 108 | + } |
49 | 109 |
|
| 110 | + public function setName($name) |
| 111 | + { |
| 112 | + $this->name = $name; |
50 | 113 | } |
| 114 | + |
51 | 115 | } |
52 | 116 |
|
0 commit comments