Skip to content

Commit

Permalink
Fixed getting property name in annotation drivers
Browse files Browse the repository at this point in the history
Fixed using setterMethod for SoftDeleteable annotations
Added Unit tests for the setter methods
Amended Code Style
  • Loading branch information
fwolfsjaeger committed Jan 8, 2024
1 parent 5f8a2bf commit 0f4649b
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Blameable/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function readExtendedMetadata($meta, array &$config)
];
}
// add the setter method for the field
$this->setSetterMethod($field, $blameable->setterMethod, $config);
$this->setSetterMethod($property->getName(), $blameable->setterMethod, $config);
// properties are unique and mapper checks that, no risk here
$config[$blameable->on][] = $field;
}
Expand Down
8 changes: 4 additions & 4 deletions src/IpTraceable/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function readExtendedMetadata($meta, array &$config)
// property annotations
foreach ($class->getProperties() as $property) {
if (
isset($meta->associationMappings[$property->name]['inherited']) ||
($meta->isMappedSuperclass && !$property->isPrivate()) ||
$meta->isInheritedField($property->name)
isset($meta->associationMappings[$property->name]['inherited'])
|| ($meta->isMappedSuperclass && !$property->isPrivate())
|| $meta->isInheritedField($property->name)
) {
continue;
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public function readExtendedMetadata($meta, array &$config)
];
}
// add the setter method for the field
$this->setSetterMethod($field, $ipTraceable->setterMethod, $config);
$this->setSetterMethod($property->getName(), $ipTraceable->setterMethod, $config);
// properties are unique and mapper checks that, no risk here
$config[$ipTraceable->on][] = $field;
}
Expand Down
14 changes: 13 additions & 1 deletion src/SoftDeleteable/SoftDeleteableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\MappedEventSubscriber;
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;
Expand Down Expand Up @@ -96,7 +97,18 @@ public function onFlush(EventArgs $args)
);
}

$reflProp->setValue($object, $date);
if (!empty($config['setterMethod'][$config['fieldName']])) {
$reflectionClass = $meta->getReflectionClass();
$setterName = $config['setterMethod'][$config['fieldName']];

if (!$reflectionClass->hasMethod($setterName)) {
throw new InvalidMappingException("Setter method - [{$setterName}] does not exist in class - {$meta->getName()}");
}

$reflectionClass->getMethod($setterName)->invoke($object, $date);
} else {
$reflProp->setValue($object, $date);
}

$om->persist($object);
$uow->propertyChanged($object, $config['fieldName'], $oldValue, $date);
Expand Down
8 changes: 4 additions & 4 deletions src/Timestampable/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function readExtendedMetadata($meta, array &$config)
// property annotations
foreach ($class->getProperties() as $property) {
if (
isset($meta->associationMappings[$property->name]['inherited']) ||
($meta->isMappedSuperclass && !$property->isPrivate()) ||
$meta->isInheritedField($property->name)
isset($meta->associationMappings[$property->name]['inherited'])
|| ($meta->isMappedSuperclass && !$property->isPrivate())
|| $meta->isInheritedField($property->name)
) {
continue;
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public function readExtendedMetadata($meta, array &$config)
];
}
// add the setter method for the field
$this->setSetterMethod($field, $timestampable->setterMethod, $config);
$this->setSetterMethod($property->getName(), $timestampable->setterMethod, $config);
// properties are unique and mapper checks that, no risk here
$config[$timestampable->on][] = $field;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Gedmo/Blameable/BlameableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function testBlameable(): void
static::assertNull($sport->getPublished());

$sportComment = $this->em->getRepository(self::COMMENT)->findOneBy(['message' => 'hello']);
static::assertSame('testuser', $sportComment->getCreated());
static::assertSame('testuser', $sportComment->getModified());
static::assertNull($sportComment->getClosed());

Expand Down
19 changes: 19 additions & 0 deletions tests/Gedmo/Blameable/Fixture/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class Comment implements Blameable
#[ORM\Column(type: Types::INTEGER)]
private ?int $status = null;

/**
* @Gedmo\Blameable(on="create")
*
* @ORM\Column(name="created", type="string")
*/
#[ORM\Column(name: 'created', type: Types::STRING)]
#[Gedmo\Blameable(on: 'create', setterMethod: 'setCreated')]
private ?string $created = null;

/**
* @var string|null
*
Expand Down Expand Up @@ -104,6 +113,16 @@ public function getMessage(): ?string
return $this->message;
}

public function getCreated(): ?string
{
return $this->created;
}

public function setCreated(?string $created): void
{
$this->created = $created;
}

public function getModified(): ?string
{
return $this->modified;
Expand Down
7 changes: 6 additions & 1 deletion tests/Gedmo/IpTraceable/Fixture/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Comment implements IpTraceable
* @Gedmo\IpTraceable(on="update")
*/
#[ORM\Column(name: 'modified', type: Types::STRING, length: 45)]
#[Gedmo\IpTraceable(on: 'update')]
#[Gedmo\IpTraceable(on: 'update', setterMethod: 'setModified')]
private $modified;

public function setArticle(?Article $article): void
Expand Down Expand Up @@ -109,6 +109,11 @@ public function getModified(): ?string
return $this->modified;
}

public function setModified(?string $modified): void
{
$this->modified = $modified;
}

public function getClosed(): ?string
{
return $this->closed;
Expand Down
2 changes: 1 addition & 1 deletion tests/Gedmo/SoftDeleteable/Fixture/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
#[ORM\Entity]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', setterMethod: 'setDeletedAt')]
class Comment
{
/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,17 @@ public function testSoftDeleteableWithDateTimeInterface(): void
static::assertInstanceOf('DateTimeInterface', $foundArt->getDeletedAt());
static::assertIsObject($foundComment);
static::assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);

$commentField = 'comment';
$commentValue = 'Comment 1';
$commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
$comment = $commentRepo->findOneBy([$commentField => $commentValue]);

$this->em->remove($comment);
$this->em->flush();

static::assertIsObject($comment->getDeletedAt());
static::assertInstanceOf('DateTimeInterface', $comment->getDeletedAt());
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Gedmo/Timestampable/Fixture/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ class Comment implements Timestampable
#[Gedmo\Timestampable(on: 'change', field: 'status', value: 1)]
private $closed;

/**
* @var \DateTime|null
*
* @ORM\Column(name="modified", type="time")
*
* @Gedmo\Timestampable(on="update")
*/
#[ORM\Column(name: 'created', type: Types::TIME_MUTABLE)]
#[Gedmo\Timestampable(on: 'create', setterMethod: 'setCreated')]
private $created;

/**
* @var \DateTime|null
*
Expand Down Expand Up @@ -109,6 +120,16 @@ public function getMessage(): ?string
return $this->message;
}

public function getCreated(): ?\DateTime
{
return $this->created;
}

public function setCreated(?\DateTime $created): void
{
$this->created = $created;
}

public function getModified(): ?\DateTime
{
return $this->modified;
Expand Down
1 change: 1 addition & 0 deletions tests/Gedmo/Timestampable/TimestampableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function testShouldHandleStandardBehavior(): void
$sport->setAuthor($author);

$sportComment = $this->em->getRepository(self::COMMENT)->findOneBy(['message' => 'hello']);
static::assertInstanceOf(\DateTime::class, $sportComment->getCreated());
static::assertNotNull($sportComment->getModified());
static::assertNull($sportComment->getClosed());

Expand Down

0 comments on commit 0f4649b

Please sign in to comment.