From beb049ca4f417d0e15d5a4abf6ab9ae4d7339f3c Mon Sep 17 00:00:00 2001 From: Abdellah Ramadan Date: Thu, 24 Apr 2025 13:25:44 +0100 Subject: [PATCH] Test Entity Ip Tagged --- .github/workflows/ci.yaml | 8 +-- .../IpTagged/IpTaggedEntityTest.php | 48 +++++++++++++++ .../TimeStamped/TimeStampedEntityTest.php | 2 - tests/Functional/Uuid/UuidEntityTest.php | 3 - tests/Util/App/BaseKernel.php | 9 +++ tests/Util/Entity/IpTagged/Product.php | 58 +++++++++++++++++++ 6 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 tests/Functional/IpTagged/IpTaggedEntityTest.php create mode 100644 tests/Util/Entity/IpTagged/Product.php diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1e26987..4b6ba8b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -28,9 +28,9 @@ jobs: run: | composer install --prefer-dist --no-progress --no-suggest -# - name: Run tests -# run: | -# ./vendor/bin/phpunit + - name: Run tests + run: | + ./vendor/bin/phpunit - name: Run PHPStan - run: vendor/bin/phpstan analyse src + run: ./vendor/bin/phpstan analyse src diff --git a/tests/Functional/IpTagged/IpTaggedEntityTest.php b/tests/Functional/IpTagged/IpTaggedEntityTest.php new file mode 100644 index 0000000..fb25b4a --- /dev/null +++ b/tests/Functional/IpTagged/IpTaggedEntityTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code. + */ + +namespace Rami\EntityKitBundle\Tests\Functional\IpTagged; + +use Rami\EntityKitBundle\Tests\Util\App\BaseKernel; +use Rami\EntityKitBundle\Tests\Util\Entity\IpTagged\Product; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; + +class IpTaggedEntityTest extends BaseKernel +{ + public function testIpTaggedEntity(): void + { + $request = new Request(); + $request->server->set('REMOTE_ADDR', '123.123.123.123'); + + $requestStack = $this->container->get(RequestStack::class); + $requestStack->push($request); + + $product = new Product(); + $product->setTitle('Awesome Product'); + $this->entityManager->persist($product); + $this->entityManager->flush(); + + $this->assertInstanceOf(Product::class, $product); + $this->assertNotNull($product->getCreatedFromIp()); + + $request->server->set('REMOTE_ADDR', '123.123.123.10'); + + $requestStack = $this->container->get(RequestStack::class); + $requestStack->push($request); + + $product->setTitle('new title'); + + $this->entityManager->flush(); + + $this->assertEquals('123.123.123.10', $product->getUpdatedFromIp()); + } +} \ No newline at end of file diff --git a/tests/Functional/TimeStamped/TimeStampedEntityTest.php b/tests/Functional/TimeStamped/TimeStampedEntityTest.php index ccd3b09..761316c 100644 --- a/tests/Functional/TimeStamped/TimeStampedEntityTest.php +++ b/tests/Functional/TimeStamped/TimeStampedEntityTest.php @@ -42,7 +42,5 @@ public function testTimeStampedEntity(): void $this->assertInstanceOf(\DateTimeImmutable::class, $blog->getCreatedAt()); $this->assertInstanceOf(\DateTimeImmutable::class, $blog->getUpdatedAt()); - $filesystem = new Filesystem(); - $filesystem->remove('var/database.db3'); } } \ No newline at end of file diff --git a/tests/Functional/Uuid/UuidEntityTest.php b/tests/Functional/Uuid/UuidEntityTest.php index 2308d3e..42982b7 100644 --- a/tests/Functional/Uuid/UuidEntityTest.php +++ b/tests/Functional/Uuid/UuidEntityTest.php @@ -36,9 +36,6 @@ public function testUuid() : void $this->assertInstanceOf(User::class, $user); $this->assertInstanceOf(Uuid::class, $user->getUuid()); - - $filesystem = new Filesystem(); - $filesystem->remove('var/database.db3'); } } \ No newline at end of file diff --git a/tests/Util/App/BaseKernel.php b/tests/Util/App/BaseKernel.php index 369e137..e2c0ad3 100644 --- a/tests/Util/App/BaseKernel.php +++ b/tests/Util/App/BaseKernel.php @@ -17,6 +17,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\Filesystem\Filesystem; class BaseKernel extends KernelTestCase { @@ -33,4 +34,12 @@ public function setUp(): void $schemaTool = new SchemaTool($this->entityManager); $schemaTool->updateSchema($metaData); } + + public function tearDown(): void + { + parent::tearDown(); + + $filesystem = new Filesystem(); + $filesystem->remove('var/database.db3'); + } } \ No newline at end of file diff --git a/tests/Util/Entity/IpTagged/Product.php b/tests/Util/Entity/IpTagged/Product.php new file mode 100644 index 0000000..8913cc0 --- /dev/null +++ b/tests/Util/Entity/IpTagged/Product.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code. + */ + +namespace Rami\EntityKitBundle\Tests\Util\Entity\IpTagged; + +use Doctrine\ORM\Mapping as ORM; +use Rami\EntityKitBundle\Common\Interfaces\IpTagged\IpTaggedInterface; +use Rami\EntityKitBundle\Entity\Traits\IpTaggedTrait; +use Rami\EntityKitBundle\Entity\Traits\MappedIpTaggedTrait; + +#[ORM\Entity] +class Product implements IpTaggedInterface +{ + use MappedIpTaggedTrait; + + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] + public ?int $id = null; + + #[ORM\Column(type: 'string', length: 255)] + public ?string $title = null; + + /** + * @return int|null + */ + public function getId(): ?int + { + return $this->id; + } + + /** + * @return string|null + */ + public function getTitle(): ?string + { + return $this->title; + } + + /** + * @param string|null $title + * @return Product + */ + public function setTitle(?string $title): Product + { + $this->title = $title; + return $this; + } + +} \ No newline at end of file