diff --git a/tests/Entity/Post.php b/tests/Entity/Post.php new file mode 100644 index 0000000..80933d6 --- /dev/null +++ b/tests/Entity/Post.php @@ -0,0 +1,89 @@ +tags = new ArrayCollection(); + } + + public function getId(): int + { + return $this->id; + } + + public function getTitle(): string + { + return $this->title; + } + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + /** + * @return Tag[] + */ + public function getTags(): Collection + { + return $this->tags; + } + + public function addTag(Tag $tag): self + { + if (!$this->tags->contains($tag)) { + $this->tags[] = $tag; + } + + return $this; + } + + public function removeTag(Tag $tag): self + { + if ($this->tags->contains($tag)) { + $this->tags->removeElement($tag); + } + + return $this; + } +} diff --git a/tests/Entity/Tag.php b/tests/Entity/Tag.php index c90501a..689f264 100644 --- a/tests/Entity/Tag.php +++ b/tests/Entity/Tag.php @@ -4,6 +4,9 @@ namespace DataDog\AuditBundle\Tests\Entity; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; /** @@ -15,21 +18,32 @@ class Tag { /** - * @ORM\Column(type="integer") * @ORM\Id() - * @ORM\GeneratedValue(strategy="AUTO") + * @ORM\GeneratedValue() + * @ORM\Column(type="integer") */ - #[ORM\Column] #[ORM\Id] #[ORM\GeneratedValue] + #[ORM\Column(type: Types::INTEGER)] private int $id; /** - * @ORM\Column(type="string") + * @ORM\Column(type="string", length=255) */ - #[ORM\Column(nullable: true)] + #[ORM\Column(type: Types::STRING, length: 255)] private string $name; + /** + * @ORM\ManyToMany(targetEntity=Post::class, mappedBy="tags") + */ + #[ORM\ManyToMany(targetEntity: Post::class, mappedBy: 'tags')] + private Collection $posts; + + public function __construct() + { + $this->posts = new ArrayCollection(); + } + public function getId(): int { return $this->id; @@ -46,4 +60,32 @@ public function setName(string $name): self return $this; } + + /** + * @return Post[] + */ + public function getPosts(): Collection + { + return $this->posts; + } + + public function addPost(Post $post): self + { + if (!$this->posts->contains($post)) { + $this->posts[] = $post; + $post->addTag($this); + } + + return $this; + } + + public function removePost(Post $post): self + { + if ($this->posts->contains($post)) { + $this->posts->removeElement($post); + $post->removeTag($this); + } + + return $this; + } } diff --git a/tests/EventSubscriber/AuditSubscriberTest.php b/tests/EventSubscriber/AuditSubscriberTest.php index 3ae353e..061579b 100644 --- a/tests/EventSubscriber/AuditSubscriberTest.php +++ b/tests/EventSubscriber/AuditSubscriberTest.php @@ -5,6 +5,7 @@ namespace DataDog\AuditBundle\Tests\EventSubscriber; use DataDog\AuditBundle\Entity\AuditLog; +use DataDog\AuditBundle\Tests\Entity\Post; use DataDog\AuditBundle\Tests\Entity\Tag; use DataDog\AuditBundle\Tests\OrmTestCase; @@ -17,7 +18,81 @@ protected function setUp(): void $this->loadFixtures(); } - public function testCorrectNumberOfAuditLogs(): void + public function testSingleEntityCreation(): void + { + $this->resetDatabase(); + + $em = $this->getDoctrine()->getManager(); + + $tag = new Tag(); + $tag->setName('Books'); + + $em->persist($tag); + $em->flush(); + + $this->assertCount(1, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); + } + + public function testSingleEntityUpdate(): void + { + $this->resetDatabase(); + + $em = $this->getDoctrine()->getManager(); + + $tag = new Tag(); + $tag->setName('Books'); + + $em->persist($tag); + $em->flush(); + + $tag->setName('Movies'); + + $em->flush(); + + $this->assertCount(2, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); + } + + public function testSingleEntityDelete(): void + { + $this->resetDatabase(); + + $em = $this->getDoctrine()->getManager(); + + $tag = new Tag(); + $tag->setName('Books'); + + $em->persist($tag); + $em->flush(); + + $em->remove($tag); + + $em->flush(); + + $this->assertCount(2, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); + } + + public function testEntityRelationCreate(): void + { + $this->resetDatabase(); + + $em = $this->getDoctrine()->getManager(); + + $tag = new Tag(); + $tag->setName('Books'); + + $post = new Post(); + $post->setTitle('Top 10 Books You Should Read'); + + $post->addTag($tag); + + $em->persist($tag); + $em->persist($post); + $em->flush(); + + $this->assertCount(3, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); + } + + public function testEntityRelationUpdate(): void { $this->resetDatabase(); @@ -26,13 +101,23 @@ public function testCorrectNumberOfAuditLogs(): void $tag1 = new Tag(); $tag1->setName('Books'); + $tag2 = new Tag(); + $tag2->setName('Lists'); + + $post = new Post(); + $post->setTitle('Top 10 Books You Should Read'); + + $post->addTag($tag1); + $em->persist($tag1); + $em->persist($tag2); + $em->persist($post); $em->flush(); - $tag1->setName('Movies'); - + $post->removeTag($tag1); + $post->addTag($tag2); $em->flush(); - $this->assertCount(2, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); + $this->assertCount(6, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); } }