Skip to content

Commit

Permalink
Merge pull request #94 from DATA-DOG/automated-testing-relation
Browse files Browse the repository at this point in the history
Automated testing relation
  • Loading branch information
natewiebe13 authored Mar 10, 2022
2 parents 33720ec + 20e5022 commit b5d2082
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 9 deletions.
89 changes: 89 additions & 0 deletions tests/Entity/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

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;

/**
* @ORM\Entity()
* @ORM\Table()
*/
#[ORM\Entity]
#[ORM\Table]
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private int $id;

/**
* @ORM\Column(type="string", length=255)
*/
#[ORM\Column(type: Types::STRING, length: 255)]
private string $title;

/**
* @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
*/
#[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'posts')]
private Collection $tags;

public function __construct()
{
$this->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;
}
}
52 changes: 47 additions & 5 deletions tests/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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;
}
}
93 changes: 89 additions & 4 deletions tests/EventSubscriber/AuditSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Expand All @@ -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());
}
}

0 comments on commit b5d2082

Please sign in to comment.