Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated testing relation #94

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}