Skip to content

Commit

Permalink
Merge pull request #194 from dotkernel/issue-193
Browse files Browse the repository at this point in the history
Changed annotations into attributes
  • Loading branch information
arhimede committed Oct 31, 2023
2 parents f284f21 + 98a0300 commit 898439a
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/Admin/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Frontend\Admin;

use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Dot\AnnotatedServices\Factory\AnnotatedServiceFactory;
use Frontend\Admin\Adapter\AuthenticationAdapter;
use Frontend\Admin\Controller\AdminController;
Expand Down Expand Up @@ -87,7 +87,7 @@ public function getDoctrineConfig(): array
],
],
'AdminEntities' => [
'class' => AnnotationDriver::class,
'class' => AttributeDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/Entity'],
],
Expand Down
43 changes: 19 additions & 24 deletions src/Admin/src/Entity/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Frontend\Admin\Repository\AdminRepository;
use Frontend\App\Entity\AbstractEntity;

use function array_map;

/**
* @ORM\Entity(repositoryClass="Frontend\Admin\Repository\AdminRepository")
* @ORM\Table(name="admin")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: AdminRepository::class)]
#[ORM\Table(name: "admin")]
#[ORM\HasLifecycleCallbacks]
class Admin extends AbstractEntity implements AdminInterface
{
public const STATUS_ACTIVE = 'active';
Expand All @@ -25,29 +24,31 @@ class Admin extends AbstractEntity implements AdminInterface
self::STATUS_INACTIVE,
];

/** @ORM\Column(name="identity", type="string", length=100, nullable=false, unique=true) */
#[ORM\Column(name: "identity", type: "string", length: 100, unique: true, nullable: false)]
protected ?string $identity = null;

/** @ORM\Column(name="firstName", type="string", length=255) */
#[ORM\Column(name: "firstName", type: "string", length: 255)]
protected ?string $firstName = null;

/** @ORM\Column(name="lastName", type="string", length=255) */
#[ORM\Column(name: "lastName", type: "string", length: 255)]
protected ?string $lastName = null;

/** @ORM\Column(name="password", type="string", length=100, nullable=false) */
#[ORM\Column(name: "password", type: "string", length: 100, nullable: false)]
protected ?string $password = null;

/** @ORM\Column(name="status", type="string", length=20, columnDefinition="ENUM('pending', 'active')") */
#[ORM\Column(
name: "status",
type: "string",
length: 20,
nullable: false,
columnDefinition: "ENUM('pending', 'active')"
)]
protected string $status = self::STATUS_ACTIVE;

/**
* @ORM\ManyToMany(targetEntity="Frontend\Admin\Entity\AdminRole", fetch="EAGER")
* @ORM\JoinTable(
* name="admin_roles",
* joinColumns={@ORM\JoinColumn(name="userUuid", referencedColumnName="uuid")},
* inverseJoinColumns={@ORM\JoinColumn(name="roleUuid", referencedColumnName="uuid")}
* )
*/
#[ORM\ManyToMany(targetEntity: AdminRole::class, fetch: "EAGER")]
#[ORM\JoinTable(name: "admin_roles")]
#[ORM\JoinColumn(name: "userUuid", referencedColumnName: "uuid")]
#[ORM\InverseJoinColumn(name: "roleUuid", referencedColumnName: "uuid")]
protected Collection $roles;

public function __construct()
Expand All @@ -57,9 +58,6 @@ public function __construct()
$this->roles = new ArrayCollection();
}

/**
* @return array
*/
public function getArrayCopy(): array
{
return [
Expand Down Expand Up @@ -136,9 +134,6 @@ public function setStatus(string $status): self
return $this;
}

/**
* @return AdminRole[]
*/
public function getRoles(): array
{
return $this->roles->toArray();
Expand Down
48 changes: 26 additions & 22 deletions src/Admin/src/Entity/AdminLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,73 @@
namespace Frontend\Admin\Entity;

use Doctrine\ORM\Mapping as ORM;
use Frontend\Admin\Repository\AdminLoginRepository;
use Frontend\App\Entity\AbstractEntity;

/**
* @ORM\Entity(repositoryClass="Frontend\Admin\Repository\AdminLoginRepository")
* @ORM\Table(name="admin_login")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: AdminLoginRepository::class)]
#[ORM\Table(name: 'admin_login')]
#[ORM\HasLifecycleCallbacks]
class AdminLogin extends AbstractEntity
{
public const IS_MOBILE_YES = 'yes';
public const IS_MOBILE_NO = 'no';
public const LOGIN_SUCCESS = 'success';
public const LOGIN_FAIL = 'fail';

/** @ORM\Column(name="adminIp", type="string", length=50, nullable=true) */
#[ORM\Column(name: "adminIp", type: "string", length: 50, nullable: true)]
protected ?string $adminIp = null;

/** @ORM\Column(name="country", type="string", length=50, nullable=true) */
#[ORM\Column(name: "country", type: "string", length: 50, nullable: true)]
protected ?string $country = null;

/** @ORM\Column(name="continent", type="string", length=50, nullable=true) */
#[ORM\Column(name: "continent", type: "string", length: 50, nullable: true)]
protected ?string $continent = null;

/** @ORM\Column(name="organization", type="string", length=50, nullable=true) */
#[ORM\Column(name: "organization", type: "string", length: 50, nullable: true)]
protected ?string $organization = null;

/** @ORM\Column(name="deviceType", type="string", length=20, nullable=true) */
#[ORM\Column(name: "deviceType", type: "string", length: 20, nullable: true)]
protected ?string $deviceType = null;

/** @ORM\Column(name="deviceBrand", type="string", length=20, nullable=true) */
#[ORM\Column(name: "deviceBrand", type: "string", length: 20, nullable: true)]
protected ?string $deviceBrand = null;

/** @ORM\Column(name="deviceModel", type="string", length=40, nullable=true) */
#[ORM\Column(name: "deviceModel", type: "string", length: 40, nullable: true)]
protected ?string $deviceModel = null;

/** @ORM\Column(name="isMobile", type="string", columnDefinition="ENUM('yes', 'no')") */
#[ORM\Column(
name: "isMobile",
type: "string",
nullable: true,
columnDefinition: "ENUM('yes', 'no')"
)]
protected ?string $isMobile = null;

/** @ORM\Column(name="osName", type="string", length=20, nullable=true) */
#[ORM\Column(name: "osName", type: "string", length: 20, nullable: true)]
protected ?string $osName = null;

/** @ORM\Column(name="osVersion", type="string", length=20, nullable=true) */
#[ORM\Column(name: "osVersion", type: "string", length: 20, nullable: true)]
protected ?string $osVersion = null;

/** @ORM\Column(name="osPlatform", type="string", length=20, nullable=true) */
#[ORM\Column(name: "osPlatform", type: "string", length: 20, nullable: true)]
protected ?string $osPlatform = null;

/** @ORM\Column(name="clientType", type="string", length=20, nullable=true) */
#[ORM\Column(name: "clientType", type: "string", length: 20, nullable: true)]
protected ?string $clientType = null;

/** @ORM\Column(name="clientName", type="string", length=40, nullable=true) */
#[ORM\Column(name: "clientName", type: "string", length: 40, nullable: true)]
protected ?string $clientName = null;

/** @ORM\Column(name="clientEngine", type="string", length=20, nullable=true) */
#[ORM\Column(name: "clientEngine", type: "string", length: 20, nullable: true)]
protected ?string $clientEngine = null;

/** @ORM\Column(name="clientVersion", type="string", length=20, nullable=true) */
#[ORM\Column(name: "clientVersion", type: "string", length: 20, nullable: true)]
protected ?string $clientVersion = null;

/** @ORM\Column(name="loginStatus", type="string", columnDefinition="ENUM('success', 'fail')") */
#[ORM\Column(name: "loginStatus", type: "string", nullable: true, columnDefinition: "ENUM('success', 'fail')")]
protected ?string $loginStatus = null;

/** @ORM\Column(name="identity", type="string", length=100) */
#[ORM\Column(name: "identity", type: "string", length: 100, nullable: true)]
protected ?string $identity = null;

public function getAdminIp(): ?string
Expand Down
11 changes: 5 additions & 6 deletions src/Admin/src/Entity/AdminRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Frontend\Admin\Entity;

use Doctrine\ORM\Mapping as ORM;
use Frontend\Admin\Repository\AdminRoleRepository;
use Frontend\App\Entity\AbstractEntity;

/**
* @ORM\Entity(repositoryClass="Frontend\Admin\Repository\AdminRoleRepository")
* @ORM\Table(name="admin_role")
* @ORM\HasLifecycleCallbacks()
*/
#[ORM\Entity(repositoryClass: AdminRoleRepository::class)]
#[ORM\Table(name: 'admin_role')]
#[ORM\HasLifecycleCallbacks]
class AdminRole extends AbstractEntity
{
public const ROLE_ADMIN = 'admin';
Expand All @@ -21,7 +20,7 @@ class AdminRole extends AbstractEntity
self::ROLE_SUPERUSER,
];

/** @ORM\Column(name="name", type="string", length=30, nullable=false, unique=true) */
#[ORM\Column(name: "name", type: "string", length: 30, unique: true, nullable: false)]
protected ?string $name = null;

public function getName(): ?string
Expand Down
10 changes: 4 additions & 6 deletions src/App/src/Common/TimestampAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ trait TimestampAwareTrait
{
private string $dateFormat = 'Y-m-d H:i:s';

/** @ORM\Column(name="created", type="datetime_immutable") */
#[ORM\Column(name: "created", type: "datetime_immutable")]
protected ?DateTimeImmutable $created = null;

/** @ORM\Column(name="updated", type="datetime_immutable", nullable=true) */
#[ORM\Column(name: "updated", type: "datetime_immutable", nullable: true)]
protected ?DateTimeImmutable $updated = null;

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->touch();
Expand Down
11 changes: 5 additions & 6 deletions src/App/src/Common/UuidAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace Frontend\App\Common;

use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator as RamseyUuidOrderedTimeGenerator;
use Ramsey\Uuid\UuidInterface;

trait UuidAwareTrait
{
/**
* @ORM\Id()
* @ORM\Column(name="uuid", type="uuid_binary_ordered_time", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
*/
#[ORM\Id]
#[ORM\Column(name: 'uuid', type: "uuid_binary_ordered_time", unique: true)]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(RamseyUuidOrderedTimeGenerator::class)]
protected ?UuidInterface $uuid = null;

public function getUuid(): ?UuidInterface
Expand Down
34 changes: 26 additions & 8 deletions test/Unit/Admin/Entity/AdminLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@
namespace FrontendTest\Unit\Admin\Entity;

use DateTimeInterface;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Table;
use Frontend\Admin\Entity\AdminLogin;
use Frontend\Admin\Repository\AdminLoginRepository;
use FrontendTest\Unit\UnitTest;
use Ramsey\Uuid\Rfc4122\UuidInterface;
use ReflectionAttribute;
use ReflectionClass;

class AdminLoginTest extends UnitTest
{
public function testAnnotations(): void
{
$reflection = new ReflectionClass(AdminLogin::class);
$docComment = $reflection->getDocComment();
$this->assertStringContainsString(
'@ORM\Entity(repositoryClass="Frontend\Admin\Repository\AdminLoginRepository")',
$docComment
);
$this->assertStringContainsString('@ORM\Table(name="admin_login")', $docComment);
$this->assertStringContainsString('@ORM\HasLifecycleCallbacks()', $docComment);
$reflection = new ReflectionClass(AdminLogin::class);
$entity = $reflection->getAttributes(Entity::class);
$table = $reflection->getAttributes(Table::class);
$hasLifecycleCallbacks = $reflection->getAttributes(HasLifecycleCallbacks::class);

$this->assertNotEmpty($entity[0]);
$this->assertNotEmpty($table[0]);
$this->assertNotEmpty($hasLifecycleCallbacks[0]);
$this->assertInstanceOf(ReflectionAttribute::class, $entity[0]);
$this->assertInstanceOf(ReflectionAttribute::class, $table[0]);
$this->assertInstanceOf(ReflectionAttribute::class, $hasLifecycleCallbacks[0]);

$entityArguments = $entity[0]->getArguments();
$tableArguments = $table[0]->getArguments();

$this->assertIsArray($entityArguments);
$this->assertIsArray($tableArguments);
$this->assertArrayHasKey('repositoryClass', $entityArguments);
$this->assertArrayHasKey('name', $tableArguments);
$this->assertSame(AdminLoginRepository::class, $entityArguments['repositoryClass']);
$this->assertSame('admin_login', $tableArguments['name']);
}

public function testAccessors(): void
Expand Down
34 changes: 26 additions & 8 deletions test/Unit/Admin/Entity/AdminRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@
namespace FrontendTest\Unit\Admin\Entity;

use DateTimeInterface;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Table;
use Frontend\Admin\Entity\AdminRole;
use Frontend\Admin\Repository\AdminRoleRepository;
use FrontendTest\Unit\UnitTest;
use Ramsey\Uuid\Rfc4122\UuidInterface;
use ReflectionAttribute;
use ReflectionClass;

class AdminRoleTest extends UnitTest
{
public function testAnnotations(): void
{
$reflection = new ReflectionClass(AdminRole::class);
$docComment = $reflection->getDocComment();
$this->assertStringContainsString(
'@ORM\Entity(repositoryClass="Frontend\Admin\Repository\AdminRoleRepository")',
$docComment
);
$this->assertStringContainsString('@ORM\Table(name="admin_role")', $docComment);
$this->assertStringContainsString('@ORM\HasLifecycleCallbacks()', $docComment);
$reflection = new ReflectionClass(AdminRole::class);
$entity = $reflection->getAttributes(Entity::class);
$table = $reflection->getAttributes(Table::class);
$hasLifecycleCallbacks = $reflection->getAttributes(HasLifecycleCallbacks::class);

$this->assertNotEmpty($entity[0]);
$this->assertNotEmpty($table[0]);
$this->assertNotEmpty($hasLifecycleCallbacks[0]);
$this->assertInstanceOf(ReflectionAttribute::class, $entity[0]);
$this->assertInstanceOf(ReflectionAttribute::class, $table[0]);
$this->assertInstanceOf(ReflectionAttribute::class, $hasLifecycleCallbacks[0]);

$entityArguments = $entity[0]->getArguments();
$tableArguments = $table[0]->getArguments();

$this->assertIsArray($entityArguments);
$this->assertIsArray($tableArguments);
$this->assertArrayHasKey('repositoryClass', $entityArguments);
$this->assertArrayHasKey('name', $tableArguments);
$this->assertSame(AdminRoleRepository::class, $entityArguments['repositoryClass']);
$this->assertSame('admin_role', $tableArguments['name']);
}

public function testAccessors(): void
Expand Down
Loading

0 comments on commit 898439a

Please sign in to comment.