Skip to content

Commit ef05646

Browse files
author
Robin Cawser
committed
Change nullOnly to override, Fix behaviour in specs
1 parent aa25c81 commit ef05646

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

spec/Vivait/StringGeneratorBundle/EventListener/GeneratorListenerSpec.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,70 @@
1717

1818
class GeneratorListenerSpec extends ObjectBehavior
1919
{
20+
public $mockEntity;
21+
2022
function it_is_initializable()
2123
{
2224
$this->shouldHaveType('Vivait\StringGeneratorBundle\EventListener\GeneratorListener');
2325
}
2426

25-
function let(Reader $reader, Registry $registry, Entity $mockEntity, EntityRepository $entityRepository, ClassMetadata $meta, EntityManagerInterface $entityManager, LifecycleEventArgs $args)
27+
function let(Reader $reader, Registry $registry, EntityRepository $entityRepository, ClassMetadata $meta, EntityManagerInterface $entityManager, LifecycleEventArgs $args)
2628
{
2729
$this->beConstructedWith($reader, $registry);
2830

2931
//Set up for EM etc.
30-
$args->getEntity()->willReturn($mockEntity);
32+
$this->mockEntity = new Entity();
33+
$args->getEntity()->willReturn($this->mockEntity);
3134
$args->getEntityManager()->willReturn($entityManager);
3235
$entityManager->getClassMetadata(Argument::any())->willReturn($meta);
3336
$entityManager->getRepository(Argument::any())->willReturn($entityRepository);
3437
$meta->getName()->willReturn('Entity');
3538

3639
}
3740

38-
function it_performs_callbacks_on_the_generator(StringGenerator $generator, Entity $mockEntity)
41+
function it_performs_callbacks_on_the_generator(StringGenerator $generator)
3942
{
4043
$annotation = new GeneratorAnnotation([]);
4144
$annotation->callbacks = ['setChars' => 'abcdef'];
42-
$this->shouldNotThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $mockEntity);
45+
$this->shouldNotThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $this->mockEntity);
4346

4447
$annotation->callbacks = ['noMethod' => 'something'];
45-
$this->shouldThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $mockEntity);
48+
$this->shouldThrow('\InvalidArgumentException')->duringPerformCallbacks($generator, $annotation, $this->mockEntity);
4649
}
4750

48-
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)
4952
{
5053
$annotation = new GeneratorAnnotation([]);
51-
$annotation->callbacks = ['setPrefix' => 'getPrefix'];
54+
$annotation->callbacks = ['setPrefix' => 'createPrefix'];
5255

53-
$mockEntity->getPrefix()->willReturn('VIVA_');
56+
$this->mockEntity->createPrefix();
5457
$generator->setPrefix('VIVA_')->shouldBeCalled();
55-
$this->performCallbacks($generator, $annotation, $mockEntity);
58+
$this->performCallbacks($generator, $annotation, $this->mockEntity);
5659
}
5760

58-
function it_sets_only_null_properties_if_specified(Reader $reader, LifecycleEventArgs $args, StringGenerator $generator, Entity $mockEntity)
61+
function it_sets_null_properties_if_override_set_to_true(Registry $registry, Reader $reader, LifecycleEventArgs $args, StringGenerator $generator)
5962
{
60-
$mockEntity->setName(null);
61-
6263
$annotation = new GeneratorAnnotation([]);
63-
$annotation->nullOnly = true;
64+
$annotation->override = false;
65+
$annotation->unique = false;
6466

6567
$reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]);
6668

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]);
6782
$generator->generate()->shouldNotBeCalled();
83+
6884
$this->prePersist($args);
6985
}
7086

@@ -75,11 +91,9 @@ function it_generates_a_string_on_a_property(Registry $registry, Reader $reader,
7591
$reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]);
7692

7793
$registry->get(Argument::any())->willReturn($generator);
78-
7994
$generator->generate()->shouldBeCalled();
8095
$generator->setLength(Argument::any())->shouldBeCalled();
8196

82-
8397
$this->prePersist($args);
8498
}
8599
}
@@ -88,14 +102,15 @@ class Entity
88102
{
89103
private $name;
90104

91-
public function getPrefix()
105+
public function createPrefix()
92106
{
93-
107+
return 'VIVA_';
94108
}
95109

96110
public function setName($name)
97111
{
98112
$this->name = $name;
99113
}
114+
100115
}
101116

src/Vivait/StringGeneratorBundle/Annotation/GeneratorAnnotation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ class GeneratorAnnotation extends Annotation
2828
*/
2929
public $unique = true;
3030

31-
public $nullOnly = false;
31+
public $override = true;
3232
}

src/Vivait/StringGeneratorBundle/EventListener/GeneratorListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function prePersist(LifecycleEventArgs $args)
5555
if ($annotation instanceof GeneratorAnnotation) {
5656

5757
$property->setAccessible(true);
58-
if ($annotation->nullOnly && !$property->getValue($object)) {
58+
if (!$annotation->override && $property->getValue($entity)) {
5959
break;
6060
}
6161

0 commit comments

Comments
 (0)