diff --git a/data/doctrine/migrations/Version20240611131923.php b/data/doctrine/migrations/Version20240613153602.php similarity index 86% rename from data/doctrine/migrations/Version20240611131923.php rename to data/doctrine/migrations/Version20240613153602.php index 7206b2e..5674afe 100644 --- a/data/doctrine/migrations/Version20240611131923.php +++ b/data/doctrine/migrations/Version20240613153602.php @@ -10,7 +10,7 @@ /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240611131923 extends AbstractMigration +final class Version20240613153602 extends AbstractMigration { public function getDescription(): string { @@ -20,9 +20,9 @@ public function getDescription(): string public function up(Schema $schema): void { // this up() migration is auto-generated, please modify it to your needs - $this->addSql('CREATE TABLE admin (identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(191) NOT NULL, lastName VARCHAR(191) NOT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE admin_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_1614D53DD73087E9 (userUuid), INDEX IDX_1614D53D88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4'); - $this->addSql('CREATE TABLE admin_role (name VARCHAR(30) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7770088A5E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name VARCHAR(30) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7770088A5E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE oauth_access_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, user_id VARCHAR(25) DEFAULT NULL, token VARCHAR(100) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, client_id INT UNSIGNED DEFAULT NULL, INDEX IDX_CA42527C19EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE oauth_access_token_scopes (access_token_id INT UNSIGNED NOT NULL, scope_id INT UNSIGNED NOT NULL, INDEX IDX_9FDF62E92CCB2688 (access_token_id), INDEX IDX_9FDF62E9682B5931 (scope_id), PRIMARY KEY(access_token_id, scope_id)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE oauth_auth_codes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expiresDatetime DATETIME DEFAULT NULL, client_id INT UNSIGNED DEFAULT NULL, INDEX IDX_BB493F8319EB6921 (client_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4'); @@ -30,12 +30,12 @@ public function up(Schema $schema): void $this->addSql('CREATE TABLE oauth_clients (id INT UNSIGNED AUTO_INCREMENT NOT NULL, name VARCHAR(40) NOT NULL, secret VARCHAR(100) DEFAULT NULL, redirect VARCHAR(191) NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, isConfidential TINYINT(1) DEFAULT 0 NOT NULL, user_id BINARY(16) DEFAULT NULL, INDEX IDX_13CE8101A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE oauth_refresh_tokens (id INT UNSIGNED AUTO_INCREMENT NOT NULL, revoked TINYINT(1) DEFAULT 0 NOT NULL, expires_at DATETIME NOT NULL, access_token_id INT UNSIGNED DEFAULT NULL, INDEX IDX_5AB6872CCB2688 (access_token_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE oauth_scopes (id INT UNSIGNED AUTO_INCREMENT NOT NULL, scope VARCHAR(191) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4'); - $this->addSql('CREATE TABLE user (identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status VARCHAR(20) NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE user (uuid BINARY(16) NOT NULL, identity VARCHAR(191) NOT NULL, password VARCHAR(191) NOT NULL, status VARCHAR(20) NOT NULL, isDeleted TINYINT(1) NOT NULL, hash VARCHAR(64) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_8D93D6496A95E9C4 (identity), UNIQUE INDEX UNIQ_8D93D649D1B862B8 (hash), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('CREATE TABLE user_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_54FCD59FD73087E9 (userUuid), INDEX IDX_54FCD59F88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4'); - $this->addSql('CREATE TABLE user_avatar (name VARCHAR(191) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); - $this->addSql('CREATE TABLE user_detail (firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); - $this->addSql('CREATE TABLE user_reset_password (expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status VARCHAR(20) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); - $this->addSql('CREATE TABLE user_role (name VARCHAR(20) NOT NULL, uuid BINARY(16) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_2DE8C6A35E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE user_avatar (uuid BINARY(16) NOT NULL, name VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_73256912D73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE user_detail (uuid BINARY(16) NOT NULL, firstName VARCHAR(191) DEFAULT NULL, lastName VARCHAR(191) DEFAULT NULL, email VARCHAR(191) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_4B5464AED73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE user_reset_password (uuid BINARY(16) NOT NULL, expires DATETIME NOT NULL, hash VARCHAR(64) NOT NULL, status VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, userUuid BINARY(16) DEFAULT NULL, UNIQUE INDEX UNIQ_D21DE3BCD1B862B8 (hash), INDEX IDX_D21DE3BCD73087E9 (userUuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); + $this->addSql('CREATE TABLE user_role (uuid BINARY(16) NOT NULL, name VARCHAR(20) NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_2DE8C6A35E237E06 (name), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4'); $this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53DD73087E9 FOREIGN KEY (userUuid) REFERENCES admin (uuid)'); $this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53D88446210 FOREIGN KEY (roleUuid) REFERENCES admin_role (uuid)'); $this->addSql('ALTER TABLE oauth_access_tokens ADD CONSTRAINT FK_CA42527C19EB6921 FOREIGN KEY (client_id) REFERENCES oauth_clients (id)'); diff --git a/src/Admin/src/Entity/Admin.php b/src/Admin/src/Entity/Admin.php index d28f573..09b833d 100644 --- a/src/Admin/src/Entity/Admin.php +++ b/src/Admin/src/Entity/Admin.php @@ -8,6 +8,7 @@ use Api\App\Entity\AbstractEntity; use Api\App\Entity\PasswordTrait; use Api\App\Entity\RoleInterface; +use Api\App\Entity\TimestampsTrait; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -19,6 +20,7 @@ class Admin extends AbstractEntity implements UserEntityInterface { use PasswordTrait; + use TimestampsTrait; public const STATUS_ACTIVE = 'active'; public const STATUS_INACTIVE = 'inactive'; diff --git a/src/Admin/src/Entity/AdminRole.php b/src/Admin/src/Entity/AdminRole.php index 614d51d..103c073 100644 --- a/src/Admin/src/Entity/AdminRole.php +++ b/src/Admin/src/Entity/AdminRole.php @@ -7,6 +7,7 @@ use Api\Admin\Repository\AdminRoleRepository; use Api\App\Entity\AbstractEntity; use Api\App\Entity\RoleInterface; +use Api\App\Entity\TimestampsTrait; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: AdminRoleRepository::class)] @@ -14,6 +15,8 @@ #[ORM\HasLifecycleCallbacks] class AdminRole extends AbstractEntity implements RoleInterface { + use TimestampsTrait; + public const ROLE_ADMIN = 'admin'; public const ROLE_SUPERUSER = 'superuser'; public const ROLES = [ diff --git a/src/App/src/Entity/AbstractEntity.php b/src/App/src/Entity/AbstractEntity.php index e842724..d38d053 100644 --- a/src/App/src/Entity/AbstractEntity.php +++ b/src/App/src/Entity/AbstractEntity.php @@ -4,7 +4,6 @@ namespace Api\App\Entity; -use DateTimeImmutable; use Doctrine\ORM\Mapping as ORM; use Laminas\Stdlib\ArraySerializableInterface; use Ramsey\Uuid\Uuid; @@ -14,22 +13,16 @@ use function method_exists; use function ucfirst; +#[ORM\MappedSuperclass] abstract class AbstractEntity implements ArraySerializableInterface { #[ORM\Id] #[ORM\Column(name: 'uuid', type: "uuid_binary", unique: true)] protected UuidInterface $uuid; - #[ORM\Column(name: "created", type: "datetime_immutable")] - protected DateTimeImmutable $created; - - #[ORM\Column(name: "updated", type: "datetime_immutable", nullable: true)] - protected ?DateTimeImmutable $updated = null; - public function __construct() { - $this->uuid = Uuid::uuid4(); - $this->created = new DateTimeImmutable(); + $this->uuid = Uuid::uuid4(); } public function getUuid(): UuidInterface @@ -37,37 +30,6 @@ public function getUuid(): UuidInterface return $this->uuid; } - public function getCreated(): ?DateTimeImmutable - { - return $this->created; - } - - public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string - { - return $this->created->format($dateFormat); - } - - public function getUpdated(): ?DateTimeImmutable - { - return $this->updated; - } - - public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string - { - if ($this->updated instanceof DateTimeImmutable) { - return $this->updated->format($dateFormat); - } - - return null; - } - - #[ORM\PrePersist] - #[ORM\PreUpdate] - public function touch(): void - { - $this->updated = new DateTimeImmutable(); - } - public function exchangeArray(array $array): void { foreach ($array as $property => $values) { diff --git a/src/App/src/Entity/TimestampsTrait.php b/src/App/src/Entity/TimestampsTrait.php new file mode 100644 index 0000000..52f9f18 --- /dev/null +++ b/src/App/src/Entity/TimestampsTrait.php @@ -0,0 +1,55 @@ +created; + } + + public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string + { + return $this->created->format($dateFormat); + } + + public function getUpdated(): ?DateTimeImmutable + { + return $this->updated; + } + + public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string + { + if ($this->updated instanceof DateTimeImmutable) { + return $this->updated->format($dateFormat); + } + + return null; + } + + #[ORM\PrePersist] + public function created(): void + { + $this->created = new DateTimeImmutable(); + $this->updated = new DateTimeImmutable(); + } + + #[ORM\PreUpdate] + public function touch(): void + { + $this->updated = new DateTimeImmutable(); + } +} diff --git a/src/User/src/Entity/User.php b/src/User/src/Entity/User.php index 8a2871b..9bb33dd 100644 --- a/src/User/src/Entity/User.php +++ b/src/User/src/Entity/User.php @@ -7,6 +7,7 @@ use Api\App\Entity\AbstractEntity; use Api\App\Entity\PasswordTrait; use Api\App\Entity\RoleInterface; +use Api\App\Entity\TimestampsTrait; use Api\User\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -23,6 +24,7 @@ class User extends AbstractEntity implements UserEntityInterface { use PasswordTrait; + use TimestampsTrait; public const STATUS_PENDING = 'pending'; public const STATUS_ACTIVE = 'active'; diff --git a/src/User/src/Entity/UserAvatar.php b/src/User/src/Entity/UserAvatar.php index 165fa77..abc76fc 100644 --- a/src/User/src/Entity/UserAvatar.php +++ b/src/User/src/Entity/UserAvatar.php @@ -5,6 +5,7 @@ namespace Api\User\Entity; use Api\App\Entity\AbstractEntity; +use Api\App\Entity\TimestampsTrait; use Api\User\EventListener\UserAvatarEventListener; use Api\User\Repository\UserAvatarRepository; use Doctrine\ORM\Mapping as ORM; @@ -15,6 +16,8 @@ #[ORM\EntityListeners([UserAvatarEventListener::class])] class UserAvatar extends AbstractEntity { + use TimestampsTrait; + #[ORM\OneToOne(inversedBy: "avatar", targetEntity: User::class)] #[ORM\JoinColumn(name: "userUuid", referencedColumnName: "uuid")] protected User $user; diff --git a/src/User/src/Entity/UserDetail.php b/src/User/src/Entity/UserDetail.php index e3633c9..a1711a5 100644 --- a/src/User/src/Entity/UserDetail.php +++ b/src/User/src/Entity/UserDetail.php @@ -5,6 +5,7 @@ namespace Api\User\Entity; use Api\App\Entity\AbstractEntity; +use Api\App\Entity\TimestampsTrait; use Api\User\Repository\UserDetailRepository; use Doctrine\ORM\Mapping as ORM; @@ -13,6 +14,8 @@ #[ORM\HasLifecycleCallbacks] class UserDetail extends AbstractEntity { + use TimestampsTrait; + #[ORM\OneToOne(inversedBy: "detail", targetEntity: User::class)] #[ORM\JoinColumn(name: "userUuid", referencedColumnName: "uuid")] protected User $user; diff --git a/src/User/src/Entity/UserResetPassword.php b/src/User/src/Entity/UserResetPassword.php index a5d916a..11014bf 100644 --- a/src/User/src/Entity/UserResetPassword.php +++ b/src/User/src/Entity/UserResetPassword.php @@ -5,6 +5,7 @@ namespace Api\User\Entity; use Api\App\Entity\AbstractEntity; +use Api\App\Entity\TimestampsTrait; use Api\User\Repository\UserResetPasswordRepository; use DateInterval; use DateTime; @@ -17,6 +18,8 @@ #[ORM\HasLifecycleCallbacks] class UserResetPassword extends AbstractEntity { + use TimestampsTrait; + public const STATUS_COMPLETED = 'completed'; public const STATUS_REQUESTED = 'requested'; public const STATUSES = [ diff --git a/src/User/src/Entity/UserRole.php b/src/User/src/Entity/UserRole.php index e8df50f..cc6ff4e 100644 --- a/src/User/src/Entity/UserRole.php +++ b/src/User/src/Entity/UserRole.php @@ -6,6 +6,7 @@ use Api\App\Entity\AbstractEntity; use Api\App\Entity\RoleInterface; +use Api\App\Entity\TimestampsTrait; use Api\User\Repository\UserRoleRepository; use Doctrine\ORM\Mapping as ORM; @@ -14,6 +15,8 @@ #[ORM\HasLifecycleCallbacks] class UserRole extends AbstractEntity implements RoleInterface { + use TimestampsTrait; + public const ROLE_GUEST = 'guest'; public const ROLE_USER = 'user'; public const ROLES = [ diff --git a/test/Functional/Traits/DatabaseTrait.php b/test/Functional/Traits/DatabaseTrait.php index 8e6053f..4990f63 100644 --- a/test/Functional/Traits/DatabaseTrait.php +++ b/test/Functional/Traits/DatabaseTrait.php @@ -42,6 +42,9 @@ public function runSeeders(): void $loader->loadFromDirectory($path); $fixtures = $loader->getFixtures(); - $executor->execute($fixtures, true); + + foreach ($fixtures as $fixture) { + $executor->load($entityManager, $fixture); + } } }