diff --git a/README.md b/README.md index 1db109c..05b6205 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ ddev exec bin/console doctrine:migration:migrate ddev exec bin/console doctrine:fixtures:load ddev exec bin/console make:entity + ddev exec bin/console make:factory + ddev exec php bin/phpunit + ddev exec php bin/phpunit --filter=testPostToCreateNewUserPost # Installation: diff --git a/migrations/Version20240321094704.php b/migrations/Version20240321094704.php deleted file mode 100644 index 3259b23..0000000 --- a/migrations/Version20240321094704.php +++ /dev/null @@ -1,35 +0,0 @@ -addSql('CREATE TABLE document_object (id INT AUTO_INCREMENT NOT NULL, partner_id INT DEFAULT NULL, product_id INT DEFAULT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_16CF1A8A9393F8FE (partner_id), INDEX IDX_16CF1A8A4584665A (product_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('ALTER TABLE document_object ADD CONSTRAINT FK_16CF1A8A9393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); - $this->addSql('ALTER TABLE document_object ADD CONSTRAINT FK_16CF1A8A4584665A FOREIGN KEY (product_id) REFERENCES product (id)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('ALTER TABLE document_object DROP FOREIGN KEY FK_16CF1A8A9393F8FE'); - $this->addSql('ALTER TABLE document_object DROP FOREIGN KEY FK_16CF1A8A4584665A'); - $this->addSql('DROP TABLE document_object'); - } -} diff --git a/migrations/Version20240322113223.php b/migrations/Version20240322113223.php deleted file mode 100644 index d9fc5e5..0000000 --- a/migrations/Version20240322113223.php +++ /dev/null @@ -1,35 +0,0 @@ -addSql('CREATE TABLE partner_follow (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, partner_id INT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_55FFED0BA76ED395 (user_id), INDEX IDX_55FFED0B9393F8FE (partner_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); - $this->addSql('ALTER TABLE partner_follow ADD CONSTRAINT FK_55FFED0BA76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id) ON DELETE CASCADE'); - $this->addSql('ALTER TABLE partner_follow ADD CONSTRAINT FK_55FFED0B9393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('ALTER TABLE partner_follow DROP FOREIGN KEY FK_55FFED0BA76ED395'); - $this->addSql('ALTER TABLE partner_follow DROP FOREIGN KEY FK_55FFED0B9393F8FE'); - $this->addSql('DROP TABLE partner_follow'); - } -} diff --git a/migrations/Version20240322155533.php b/migrations/Version20240322155533.php deleted file mode 100644 index 690fe97..0000000 --- a/migrations/Version20240322155533.php +++ /dev/null @@ -1,35 +0,0 @@ -addSql('ALTER TABLE document_object ADD created_by_id INT NOT NULL, ADD name VARCHAR(255) NOT NULL, ADD description LONGTEXT DEFAULT NULL'); - $this->addSql('ALTER TABLE document_object ADD CONSTRAINT FK_16CF1A8AB03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); - $this->addSql('CREATE INDEX IDX_16CF1A8AB03A8386 ON document_object (created_by_id)'); - } - - public function down(Schema $schema): void - { - // this down() migration is auto-generated, please modify it to your needs - $this->addSql('ALTER TABLE document_object DROP FOREIGN KEY FK_16CF1A8AB03A8386'); - $this->addSql('DROP INDEX IDX_16CF1A8AB03A8386 ON document_object'); - $this->addSql('ALTER TABLE document_object DROP created_by_id, DROP name, DROP description'); - } -} diff --git a/migrations/Version20240320115901.php b/migrations/Version20240325145137.php similarity index 78% rename from migrations/Version20240320115901.php rename to migrations/Version20240325145137.php index c62289e..b63d216 100644 --- a/migrations/Version20240320115901.php +++ b/migrations/Version20240325145137.php @@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration; /** * Auto-generated Migration: Please modify to your needs! */ -final class Version20240320115901 extends AbstractMigration +final class Version20240325145137 extends AbstractMigration { public function getDescription(): string { @@ -22,8 +22,11 @@ final class Version20240320115901 extends AbstractMigration // this up() migration is auto-generated, please modify it to your needs $this->addSql('CREATE TABLE comment (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, posting_id INT NOT NULL, message LONGTEXT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_9474526C7E3C61F9 (owner_id), INDEX IDX_9474526C9AE985F6 (posting_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE contact (id INT AUTO_INCREMENT NOT NULL, partner_id INT NOT NULL, image_id INT DEFAULT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, birthday DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', position VARCHAR(255) DEFAULT NULL, phone VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_4C62E6389393F8FE (partner_id), INDEX IDX_4C62E6383DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE document (id INT AUTO_INCREMENT NOT NULL, created_by_id INT NOT NULL, partner_id INT DEFAULT NULL, product_id INT DEFAULT NULL, document_object_id INT NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D8698A76B03A8386 (created_by_id), INDEX IDX_D8698A769393F8FE (partner_id), INDEX IDX_D8698A764584665A (product_id), INDEX IDX_D8698A7631157E8A (document_object_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE document_object (id INT AUTO_INCREMENT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE media_object (id INT AUTO_INCREMENT NOT NULL, file_path VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE partner (id INT AUTO_INCREMENT NOT NULL, logo_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, street VARCHAR(255) DEFAULT NULL, street_no VARCHAR(255) DEFAULT NULL, zip VARCHAR(255) DEFAULT NULL, city VARCHAR(255) DEFAULT NULL, country VARCHAR(255) DEFAULT NULL, website VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_312B3E16F98F144A (logo_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('CREATE TABLE partner_follow (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, partner_id INT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_55FFED0BA76ED395 (user_id), INDEX IDX_55FFED0B9393F8FE (partner_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE posting (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, partner_id INT NOT NULL, contact_id INT DEFAULT NULL, sale_id INT DEFAULT NULL, headline VARCHAR(255) NOT NULL, message LONGTEXT NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_BD275D737E3C61F9 (owner_id), INDEX IDX_BD275D739393F8FE (partner_id), INDEX IDX_BD275D73E7A1254A (contact_id), INDEX IDX_BD275D734A7E4868 (sale_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE product (id INT AUTO_INCREMENT NOT NULL, image_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D34A04AD3DA5256D (image_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); $this->addSql('CREATE TABLE sale (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, partner_id INT NOT NULL, product_id INT NOT NULL, turnover INT NOT NULL, profit INT DEFAULT NULL, comment LONGTEXT DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_E54BC0057E3C61F9 (owner_id), INDEX IDX_E54BC0059393F8FE (partner_id), INDEX IDX_E54BC0054584665A (product_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); @@ -34,7 +37,13 @@ final class Version20240320115901 extends AbstractMigration $this->addSql('ALTER TABLE comment ADD CONSTRAINT FK_9474526C9AE985F6 FOREIGN KEY (posting_id) REFERENCES posting (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6389393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE contact ADD CONSTRAINT FK_4C62E6383DA5256D FOREIGN KEY (image_id) REFERENCES media_object (id) ON DELETE SET NULL'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A76B03A8386 FOREIGN KEY (created_by_id) REFERENCES `user` (id)'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A769393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id)'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A764584665A FOREIGN KEY (product_id) REFERENCES product (id)'); + $this->addSql('ALTER TABLE document ADD CONSTRAINT FK_D8698A7631157E8A FOREIGN KEY (document_object_id) REFERENCES document_object (id)'); $this->addSql('ALTER TABLE partner ADD CONSTRAINT FK_312B3E16F98F144A FOREIGN KEY (logo_id) REFERENCES media_object (id) ON DELETE SET NULL'); + $this->addSql('ALTER TABLE partner_follow ADD CONSTRAINT FK_55FFED0BA76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE partner_follow ADD CONSTRAINT FK_55FFED0B9393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D737E3C61F9 FOREIGN KEY (owner_id) REFERENCES `user` (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D739393F8FE FOREIGN KEY (partner_id) REFERENCES partner (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE posting ADD CONSTRAINT FK_BD275D73E7A1254A FOREIGN KEY (contact_id) REFERENCES contact (id) ON DELETE CASCADE'); @@ -59,7 +68,13 @@ final class Version20240320115901 extends AbstractMigration $this->addSql('ALTER TABLE comment DROP FOREIGN KEY FK_9474526C9AE985F6'); $this->addSql('ALTER TABLE contact DROP FOREIGN KEY FK_4C62E6389393F8FE'); $this->addSql('ALTER TABLE contact DROP FOREIGN KEY FK_4C62E6383DA5256D'); + $this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A76B03A8386'); + $this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A769393F8FE'); + $this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A764584665A'); + $this->addSql('ALTER TABLE document DROP FOREIGN KEY FK_D8698A7631157E8A'); $this->addSql('ALTER TABLE partner DROP FOREIGN KEY FK_312B3E16F98F144A'); + $this->addSql('ALTER TABLE partner_follow DROP FOREIGN KEY FK_55FFED0BA76ED395'); + $this->addSql('ALTER TABLE partner_follow DROP FOREIGN KEY FK_55FFED0B9393F8FE'); $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D737E3C61F9'); $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D739393F8FE'); $this->addSql('ALTER TABLE posting DROP FOREIGN KEY FK_BD275D73E7A1254A'); @@ -77,8 +92,11 @@ final class Version20240320115901 extends AbstractMigration $this->addSql('ALTER TABLE `user` DROP FOREIGN KEY FK_8D93D6493DA5256D'); $this->addSql('DROP TABLE comment'); $this->addSql('DROP TABLE contact'); + $this->addSql('DROP TABLE document'); + $this->addSql('DROP TABLE document_object'); $this->addSql('DROP TABLE media_object'); $this->addSql('DROP TABLE partner'); + $this->addSql('DROP TABLE partner_follow'); $this->addSql('DROP TABLE posting'); $this->addSql('DROP TABLE product'); $this->addSql('DROP TABLE sale'); diff --git a/src/ApiResource/DocumentApi.php b/src/ApiResource/DocumentApi.php new file mode 100644 index 0000000..0ae334c --- /dev/null +++ b/src/ApiResource/DocumentApi.php @@ -0,0 +1,87 @@ + + * @date 12.12.23 + */ + + +namespace App\ApiResource; + +use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; +use ApiPlatform\Doctrine\Orm\State\Options; +use ApiPlatform\Metadata\ApiFilter; +use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\ApiResource; +use App\Entity\Comment; +use App\Entity\Document; +use App\Entity\DocumentObject; +use App\Entity\Partner; +use App\Interface\OwnerInterface; +use App\State\EntityClassDtoStateProcessor; +use App\State\EntityToDtoStateProvider; +use ApiPlatform\Metadata\Delete; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Patch; +use ApiPlatform\Metadata\Post; +use App\Validator\IsValidOwner; +use Symfony\Component\Serializer\Attribute\Groups; +use Symfony\Component\Validator\Constraints\NotBlank; + +#[ApiResource( + shortName: 'Document', + operations: [ + new Get( + security: 'is_granted("ROLE_USER")' + ), + new GetCollection( + security: 'is_granted("ROLE_USER")', + ), + new Post( + security: 'is_granted("ROLE_USER")', + ), + new Patch( + security: 'is_granted("ROLE_USER")', + ), + new Delete( + security: 'is_granted("ROLE_ADMIN")', + ) + ], + security: 'is_granted("ROLE_USER")', + provider: EntityToDtoStateProvider::class, + processor: EntityClassDtoStateProcessor::class, + stateOptions: new Options(entityClass: Document::class), +)] +#[ApiFilter(SearchFilter::class, properties: ['owner' => 'exact', 'partner' => 'exact', 'product' => 'exact'])] +class DocumentApi +{ + #[ApiProperty(readable: false, writable: false, identifier: true)] + public ?int $id = null; + + #[ApiProperty(writable: false)] + public ?UserApi $createdBy = null; + + #[ApiProperty(writable: false)] + public ?string $createdByName = null; + + #[NotBlank] + public ?string $name = null; + + public ?string $description = null; + + public ?PartnerApi $partner = null; + + #[ApiProperty(writable: false)] + public ?string $partnerName = null; + + public ?ProductApi $product = null; + + #[ApiProperty(writable: false)] + public ?string $productName = null; + + #[NotBlank] + public ?DocumentObject $documentObject = null; + + #[ApiProperty(writable: false)] + public ?\DateTimeImmutable $createdAt = null; +} \ No newline at end of file diff --git a/src/Controller/CreateDocumentObjectAction.php b/src/Controller/CreateDocumentObjectAction.php index cb556f8..2fedef9 100644 --- a/src/Controller/CreateDocumentObjectAction.php +++ b/src/Controller/CreateDocumentObjectAction.php @@ -8,52 +8,25 @@ namespace App\Controller; -use ApiPlatform\Api\IriConverterInterface; use App\Entity\DocumentObject; -use App\Entity\Partner; -use App\Entity\Product; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; -use Symfonycasts\MicroMapper\MicroMapperInterface; #[AsController] final class CreateDocumentObjectAction extends AbstractController { - public function __invoke( - Request $request, - IriConverterInterface $iriConverter, - MicroMapperInterface $microMapper - ): DocumentObject { + public function __invoke(Request $request): DocumentObject + { $uploadedFile = $request->files->get('file'); if (!$uploadedFile) { throw new BadRequestHttpException('"file" is required'); } - $documentObject = new DocumentObject(); - $documentObject->file = $uploadedFile; + $mediaObject = new DocumentObject(); + $mediaObject->file = $uploadedFile; - $partnerIri = $request->request->get('partner'); - if ($partnerIri !== null) { - try { - $partnerApi = $iriConverter->getResourceFromIri($partnerIri); - } catch (\Exception $exception) { - throw new BadRequestHttpException('invalid partner iri'); - } - $documentObject->setPartner($microMapper->map($partnerApi, Partner::class)); - } - - $productIri = $request->request->get('product'); - if ($productIri !== null) { - try { - $productApi = $iriConverter->getResourceFromIri($productIri); - } catch (\Exception $exception) { - throw new BadRequestHttpException('invalid product iri'); - } - $documentObject->setProduct($microMapper->map($productApi, Product::class)); - } - - return $documentObject; + return $mediaObject; } } \ No newline at end of file diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 8a6864a..190dd2b 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -2,8 +2,10 @@ namespace App\DataFixtures; +use App\Entity\Document; use App\Factory\CommentFactory; use App\Factory\ContactFactory; +use App\Factory\DocumentFactory; use App\Factory\DocumentObjectFactory; use App\Factory\MediaObjectLogoFactory; use App\Factory\MediaObjectProductFactory; @@ -75,7 +77,8 @@ class AppFixtures extends Fixture MediaObjectProductFactory::createMany(50); TaskFactory::createMany(50); TaskNoteFactory::createMany(100); - DocumentObjectFactory::createMany(50); PartnerFollowFactory::createMany(100); + DocumentObjectFactory::createMany(50); + DocumentFactory::createMany(50); } } diff --git a/src/Entity/Document.php b/src/Entity/Document.php new file mode 100644 index 0000000..e41a6e0 --- /dev/null +++ b/src/Entity/Document.php @@ -0,0 +1,119 @@ + + * @date 25.01.24 + */ + +namespace App\Entity; + +use ApiPlatform\Metadata\ApiProperty; +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Mapping as ORM; + +#[ORM\Entity] +class Document +{ + #[ORM\Id, ORM\Column, ORM\GeneratedValue] + private ?int $id = null; + + #[ORM\ManyToOne(inversedBy: 'documents')] + #[ORM\JoinColumn(nullable: false)] + private ?User $createdBy = null; + + #[ORM\Column(length: 255)] + private ?string $name = null; + + #[ORM\Column(type: Types::TEXT, nullable: true)] + private ?string $description = null; + + #[ORM\ManyToOne(inversedBy: 'documents')] + private ?Partner $partner = null; + + #[ORM\ManyToOne(inversedBy: 'documents')] + private ?Product $product = null; + + #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: false)] + private ?DocumentObject $documentObject = null; + + #[ApiProperty(writable: false)] + #[ORM\Column] + private ?\DateTimeImmutable $createdAt = null; + + public function __construct(User $createdBy, DocumentObject $documentObject) + { + $this->createdBy = $createdBy; + $this->documentObject = $documentObject; + $this->createdAt = new \DateTimeImmutable(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getCreatedBy(): ?User + { + return $this->createdBy; + } + + public function setCreatedBy(?User $createdBy): void + { + $this->createdBy = $createdBy; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(?string $name): void + { + $this->name = $name; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): void + { + $this->description = $description; + } + + public function getPartner(): ?Partner + { + return $this->partner; + } + + public function setPartner(?Partner $partner): void + { + $this->partner = $partner; + } + + public function getProduct(): ?Product + { + return $this->product; + } + + public function setProduct(?Product $product): void + { + $this->product = $product; + } + + public function getDocumentObject(): ?DocumentObject + { + return $this->documentObject; + } + + public function setDocumentObject(?DocumentObject $documentObject): void + { + $this->documentObject = $documentObject; + } + + public function getCreatedAt(): ?\DateTimeImmutable + { + return $this->createdAt; + } +} \ No newline at end of file diff --git a/src/Entity/DocumentObject.php b/src/Entity/DocumentObject.php index 25c7b84..48edfb7 100644 --- a/src/Entity/DocumentObject.php +++ b/src/Entity/DocumentObject.php @@ -4,10 +4,9 @@ * @date 25.01.24 */ + namespace App\Entity; -use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; -use ApiPlatform\Metadata\ApiFilter; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\Delete; @@ -16,7 +15,6 @@ use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\Post; use ApiPlatform\OpenApi\Model; use App\Controller\CreateDocumentObjectAction; -use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\Serializer\Annotation\Groups; @@ -26,7 +24,7 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich; #[Vich\Uploadable] #[ORM\Entity] #[ApiResource( - shortName: 'Document', + shortName: 'DocumentObject', types: ['https://schema.org/MediaObject'], operations: [ new Get(), @@ -40,72 +38,29 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich; 'schema' => [ 'type' => 'object', 'properties' => [ - 'name' => [ - 'type' => 'string', - 'required' => true, - ], - 'description' => [ - 'type' => 'text', - 'required' => true, - ], 'file' => [ 'type' => 'string', 'format' => 'binary' - ], - 'partner' => [ - 'type' => 'string', - 'format' => 'iri_reference', - 'example' => 'https://example.com/' - ], - 'product' => [ - 'type' => 'string', - 'format' => 'iri_reference', - 'example' => 'https://example.com/' ] - ], - ], - ], + ] + ] + ] ]) ) ), validationContext: ['groups' => ['Default', 'document_object:create']], deserialize: false ), - new Delete( - // controller: DeleteMediaObjectAction::class - ), + new Delete(), ], normalizationContext: ['groups' => ['document_object:read']], security: 'is_granted("ROLE_USER")', )] -#[ApiFilter(SearchFilter::class, properties: ['partner' => 'exact', 'product' => 'exact'])] class DocumentObject { #[ORM\Id, ORM\Column, ORM\GeneratedValue] private ?int $id = null; - #[Groups(['document_object:read', 'document_object:create'])] - #[ORM\ManyToOne(inversedBy: 'documentObjects')] - #[ORM\JoinColumn(nullable: false)] - private ?User $createdBy = null; - - #[Groups(['document_object:read', 'document_object:create'])] - #[Assert\NotNull(groups: ['document_object:create'])] - #[ORM\Column(length: 255)] - private ?string $name = null; - - #[Groups(['document_object:read', 'document_object:create'])] - #[ORM\Column(type: Types::TEXT, nullable: true)] - private ?string $description = null; - - #[Groups(['document_object:read', 'document_object:create'])] - #[ORM\ManyToOne(inversedBy: 'documentObjects')] - private ?Partner $partner = null; - - #[Groups(['document_object:read', 'document_object:create'])] - #[ORM\ManyToOne(inversedBy: 'documentObjects')] - private ?Product $product = null; - #[ApiProperty(types: ['https://schema.org/contentUrl'])] #[Groups(['document_object:read'])] public ?string $contentUrl = null; @@ -117,8 +72,6 @@ class DocumentObject #[ORM\Column(nullable: true)] public ?string $filePath = null; - #[ApiProperty(writable: false)] - #[Groups(['document_object:read'])] #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; @@ -152,64 +105,4 @@ class DocumentObject return $this->createdAt; } - public function getPartner(): ?Partner - { - return $this->partner; - } - - public function setPartner(?Partner $partner): static - { - $this->partner = $partner; - - return $this; - } - - public function getProduct(): ?Product - { - return $this->product; - } - - public function setProduct(?Product $product): static - { - $this->product = $product; - - return $this; - } - - public function getCreatedBy(): ?User - { - return $this->createdBy; - } - - public function setCreatedBy(?User $createdBy): static - { - $this->createdBy = $createdBy; - - return $this; - } - - public function getName(): ?string - { - return $this->name; - } - - public function setName(string $name): static - { - $this->name = $name; - - return $this; - } - - public function getDescription(): ?string - { - return $this->description; - } - - public function setDescription(?string $description): static - { - $this->description = $description; - - return $this; - } - } \ No newline at end of file diff --git a/src/Entity/MediaObject.php b/src/Entity/MediaObject.php index 4c4d12b..9a357e4 100644 --- a/src/Entity/MediaObject.php +++ b/src/Entity/MediaObject.php @@ -24,7 +24,7 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich; #[Vich\Uploadable] #[ORM\Entity] #[ApiResource( - shortName: 'Media', + shortName: 'MediaObject', types: ['https://schema.org/MediaObject'], operations: [ new Get(), @@ -51,9 +51,7 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich; validationContext: ['groups' => ['Default', 'media_object:create']], deserialize: false ), - new Delete( - // controller: DeleteMediaObjectAction::class - ), + new Delete(), ], normalizationContext: ['groups' => ['media_object:read']], security: 'is_granted("ROLE_USER")', diff --git a/src/Entity/Partner.php b/src/Entity/Partner.php index f51c381..49bf82e 100644 --- a/src/Entity/Partner.php +++ b/src/Entity/Partner.php @@ -57,7 +57,7 @@ class Partner #[ORM\OneToMany(mappedBy: 'partner', targetEntity: Sale::class)] private Collection $sales; - #[ORM\OneToMany(mappedBy: 'partner', targetEntity: DocumentObject::class)] + #[ORM\OneToMany(mappedBy: 'partner', targetEntity: Document::class)] private Collection $documentObjects; #[ORM\OneToMany(mappedBy: 'partner', targetEntity: PartnerFollow::class)] @@ -249,14 +249,14 @@ class Partner } /** - * @return Collection + * @return Collection */ public function getDocumentObjects(): Collection { return $this->documentObjects; } - public function addDocumentObject(DocumentObject $documentObject): static + public function addDocumentObject(Document $documentObject): static { if (!$this->documentObjects->contains($documentObject)) { $this->documentObjects->add($documentObject); @@ -266,7 +266,7 @@ class Partner return $this; } - public function removeDocumentObject(DocumentObject $documentObject): static + public function removeDocumentObject(Document $documentObject): static { if ($this->documentObjects->removeElement($documentObject)) { // set the owning side to null (unless already changed) diff --git a/src/Entity/Product.php b/src/Entity/Product.php index 81ee37a..2fe0f96 100644 --- a/src/Entity/Product.php +++ b/src/Entity/Product.php @@ -32,7 +32,7 @@ class Product #[ORM\OneToMany(mappedBy: 'product', targetEntity: Sale::class)] private Collection $sales; - #[ORM\OneToMany(mappedBy: 'product', targetEntity: DocumentObject::class)] + #[ORM\OneToMany(mappedBy: 'product', targetEntity: Document::class)] private Collection $documentObjects; public function __construct() @@ -119,14 +119,14 @@ class Product } /** - * @return Collection + * @return Collection */ public function getDocumentObjects(): Collection { return $this->documentObjects; } - public function addDocumentObject(DocumentObject $documentObject): static + public function addDocumentObject(Document $documentObject): static { if (!$this->documentObjects->contains($documentObject)) { $this->documentObjects->add($documentObject); @@ -136,7 +136,7 @@ class Product return $this; } - public function removeDocumentObject(DocumentObject $documentObject): static + public function removeDocumentObject(Document $documentObject): static { if ($this->documentObjects->removeElement($documentObject)) { // set the owning side to null (unless already changed) diff --git a/src/Entity/User.php b/src/Entity/User.php index 6b8e938..e17fdab 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -58,7 +58,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\OneToMany(mappedBy: 'user', targetEntity: PartnerFollow::class)] private Collection $partnerFollows; - #[ORM\OneToMany(mappedBy: 'createdBy', targetEntity: DocumentObject::class)] + #[ORM\OneToMany(mappedBy: 'createdBy', targetEntity: Document::class)] private Collection $documentObjects; @@ -320,14 +320,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface } /** - * @return Collection + * @return Collection */ public function getDocumentObjects(): Collection { return $this->documentObjects; } - public function addDocumentObject(DocumentObject $documentObject): static + public function addDocumentObject(Document $documentObject): static { if (!$this->documentObjects->contains($documentObject)) { $this->documentObjects->add($documentObject); @@ -337,7 +337,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } - public function removeDocumentObject(DocumentObject $documentObject): static + public function removeDocumentObject(Document $documentObject): static { if ($this->documentObjects->removeElement($documentObject)) { // set the owning side to null (unless already changed) diff --git a/src/Factory/DocumentFactory.php b/src/Factory/DocumentFactory.php new file mode 100644 index 0000000..bcf3961 --- /dev/null +++ b/src/Factory/DocumentFactory.php @@ -0,0 +1,75 @@ + + * + * @method Document|Proxy create(array|callable $attributes = []) + * @method static Document|Proxy createOne(array $attributes = []) + * @method static Document|Proxy find(object|array|mixed $criteria) + * @method static Document|Proxy findOrCreate(array $attributes) + * @method static Document|Proxy first(string $sortedField = 'id') + * @method static Document|Proxy last(string $sortedField = 'id') + * @method static Document|Proxy random(array $attributes = []) + * @method static Document|Proxy randomOrCreate(array $attributes = []) + * @method static EntityRepository|RepositoryProxy repository() + * @method static Document[]|Proxy[] all() + * @method static Document[]|Proxy[] createMany(int $number, array|callable $attributes = []) + * @method static Document[]|Proxy[] createSequence(iterable|callable $sequence) + * @method static Document[]|Proxy[] findBy(array $attributes) + * @method static Document[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) + * @method static Document[]|Proxy[] randomSet(int $number, array $attributes = []) + */ +final class DocumentFactory extends ModelFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() + { + parent::__construct(); + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + protected function getDefaults(): array + { + $randBool = (bool)random_int(0, 1); + return [ + 'createdBy' => UserFactory::random(), + 'documentObject' => DocumentObjectFactory::random(), + 'name' => self::faker()->randomElement(FakeValues::BUSINESS_FILE_NAMES), + 'description' => self::faker()->text(), + 'partner' => $randBool ? PartnerFactory::random() : null, + 'product' => !$randBool ? ProductFactory::random() : null, + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + protected function initialize(): self + { + return $this + // ->afterInstantiate(function(Document $document): void {}) + ; + } + + protected static function getClass(): string + { + return Document::class; + } +} diff --git a/src/Factory/DocumentObjectFactory.php b/src/Factory/DocumentObjectFactory.php index 09844c1..23e2978 100644 --- a/src/Factory/DocumentObjectFactory.php +++ b/src/Factory/DocumentObjectFactory.php @@ -2,7 +2,6 @@ namespace App\Factory; -use App\DataFixtures\FakeValues; use App\Entity\DocumentObject; use Doctrine\ORM\EntityRepository; use Symfony\Component\HttpKernel\KernelInterface; @@ -63,11 +62,6 @@ final class DocumentObjectFactory extends ModelFactory $randBool = (bool)random_int(0, 1); return [ 'file' => new ReplacingFile($randomFile), - 'createdBy' => UserFactory::random(), - 'name' => self::faker()->randomElement(FakeValues::BUSINESS_FILE_NAMES), - 'description' => self::faker()->text(), - 'partner' => $randBool ? PartnerFactory::random() : null, - 'product' => !$randBool ? ProductFactory::random() : null, ]; } diff --git a/src/Factory/UserFactory.php b/src/Factory/UserFactory.php index cc85b0f..fe8b75b 100644 --- a/src/Factory/UserFactory.php +++ b/src/Factory/UserFactory.php @@ -52,7 +52,7 @@ final class UserFactory extends ModelFactory 'email' => self::faker()->email(), 'firstName' => self::faker()->firstName(), 'lastName' => self::faker()->lastName(), - 'image' => MediaObjectUserFactory::createOne(), + 'image' => MediaObjectUserFactory::random(), 'password' => "test", 'roles' => [], ]; diff --git a/src/Mapper/DocumentApiToEntityMapper.php b/src/Mapper/DocumentApiToEntityMapper.php new file mode 100644 index 0000000..fc5e865 --- /dev/null +++ b/src/Mapper/DocumentApiToEntityMapper.php @@ -0,0 +1,79 @@ +id) { + $entity = $this->repository->find($dto->id); + } else { + $user = $this->security->getUser(); + assert($user instanceof User); + if ($dto->documentObject === null) { + throw new \Exception('Document object missing'); + } + $entity = new Document($user, $dto->documentObject); + } + + if (!$entity) { + throw new \Exception('Document not found'); + } + + return $entity; + } + + public function populate(object $from, object $to, array $context): object + { + $dto = $from; + assert($dto instanceof DocumentApi); + $entity = $to; + assert($entity instanceof Document); + + $entity->setName($dto->name); + $entity->setDescription($dto->description); + if ($dto->partner !== null) { + $entity->setPartner( + $this->microMapper->map($dto->partner, Partner::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]) + ); + } + if ($dto->product !== null) { + $entity->setProduct( + $this->microMapper->map($dto->product, Product::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]) + ); + } + if ($dto->documentObject?->getId() !== $entity->getDocumentObject()?->getId()) { + $entity->setDocumentObject($dto->documentObject); + } + return $entity; + } +} diff --git a/src/Mapper/DocumentEntityToApiMapper.php b/src/Mapper/DocumentEntityToApiMapper.php new file mode 100644 index 0000000..6b4e0a5 --- /dev/null +++ b/src/Mapper/DocumentEntityToApiMapper.php @@ -0,0 +1,70 @@ +id = $entity->getId(); + + return $dto; + } + + public function populate(object $from, object $to, array $context): object + { + $entity = $from; + $dto = $to; + assert($entity instanceof Document); + assert($dto instanceof DocumentApi); + + $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->createdByName = $entity->getCreatedBy()?->getFirstName() . ' ' . $entity->getCreatedBy()?->getLastName(); + $dto->name = $entity->getName(); + $dto->description = $entity->getDescription(); + + if ($entity->getPartner()) { + $dto->partner = $this->microMapper->map($entity->getPartner(), PartnerApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->partnerName = $entity->getPartner()->getName(); + } + + if ($entity->getProduct()) { + $dto->product = $this->microMapper->map($entity->getProduct(), ProductApi::class, [ + MicroMapperInterface::MAX_DEPTH => 1, + ]); + $dto->productName = $entity->getProduct()->getName(); + } + + $dto->documentObject = $entity->getDocumentObject(); + $dto->createdAt = $entity->getCreatedAt(); + + return $dto; + } +} diff --git a/src/Repository/DocumentRepository.php b/src/Repository/DocumentRepository.php new file mode 100644 index 0000000..2c0db1b --- /dev/null +++ b/src/Repository/DocumentRepository.php @@ -0,0 +1,48 @@ + + * + * @method Document|null find($id, $lockMode = null, $lockVersion = null) + * @method Document|null findOneBy(array $criteria, array $orderBy = null) + * @method Document[] findAll() + * @method Document[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class DocumentRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Document::class); + } + +// /** +// * @return Contact[] Returns an array of Contact objects +// */ +// public function findByExampleField($value): array +// { +// return $this->createQueryBuilder('p') +// ->andWhere('p.exampleField = :val') +// ->setParameter('val', $value) +// ->orderBy('p.id', 'ASC') +// ->setMaxResults(10) +// ->getQuery() +// ->getResult() +// ; +// } + +// public function findOneBySomeField($value): ?Contact +// { +// return $this->createQueryBuilder('p') +// ->andWhere('p.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; +// } +} diff --git a/src/Serializer/MediaObjectNormalizer.php b/src/Serializer/MediaObjectNormalizer.php index b3692e8..4352bd8 100644 --- a/src/Serializer/MediaObjectNormalizer.php +++ b/src/Serializer/MediaObjectNormalizer.php @@ -8,6 +8,7 @@ namespace App\Serializer; +use App\Entity\Document; use App\Entity\DocumentObject; use App\Entity\MediaObject; use Symfony\Component\DependencyInjection\Attribute\AsDecorator; diff --git a/tests/Functional/CommentResourceTest.php b/tests/Functional/CommentResourceTest.php index 82c64e6..7356ef5 100644 --- a/tests/Functional/CommentResourceTest.php +++ b/tests/Functional/CommentResourceTest.php @@ -11,6 +11,7 @@ use App\Enum\PartnerType; use App\Factory\CommentFactory; use App\Factory\MediaObjectLogoFactory; use App\Factory\MediaObjectContactFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\PostingFactory; use App\Factory\ProductFactory; @@ -38,6 +39,7 @@ class CommentResourceTest extends KernelTestCase public function testPostComment(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', diff --git a/tests/Functional/ContactResourceTest.php b/tests/Functional/ContactResourceTest.php index dddbbca..78fe28a 100644 --- a/tests/Functional/ContactResourceTest.php +++ b/tests/Functional/ContactResourceTest.php @@ -55,7 +55,7 @@ class ContactResourceTest extends KernelTestCase 'lastName' => 'Test', 'partner' => '/api/partners/' . $partner->getId(), 'birthday' => '1984-02-10T00:00:00.000Z', - 'image' => '/api/medias/' . $mediaObject->getId(), + 'image' => '/api/media_objects/' . $mediaObject->getId(), 'position' => 'CEO', 'phone' => '123456789', 'email' => 'peter@test2.de', @@ -79,7 +79,7 @@ class ContactResourceTest extends KernelTestCase ; $this->browser() - ->delete('/api/medias/' . $mediaObject->getId(), [ + ->delete('/api/media_objects/' . $mediaObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], diff --git a/tests/Functional/DocumentObjectResourceTest.php b/tests/Functional/DocumentObjectResourceTest.php index 5d4a764..a4c9581 100644 --- a/tests/Functional/DocumentObjectResourceTest.php +++ b/tests/Functional/DocumentObjectResourceTest.php @@ -7,9 +7,10 @@ namespace App\Tests\Functional; +use App\Factory\DocumentFactory; use App\Factory\DocumentObjectFactory; use App\Factory\MediaObjectLogoFactory; -use App\Factory\MediaObjectProductFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\ProductFactory; use App\Factory\UserFactory; @@ -45,6 +46,7 @@ class DocumentObjectResourceTest extends KernelTestCase $file = new UploadedFile($dstFile, 'image.png'); + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', @@ -53,14 +55,16 @@ class DocumentObjectResourceTest extends KernelTestCase 'email' => 'peter@test.de', ] ); - MediaObjectProductFactory::createOne(); - $partner = PartnerFactory::createOne(); - $product = ProductFactory::createOne(); + + $documentObject = DocumentObjectFactory::createOne(); + PartnerFactory::createOne(); + ProductFactory::createOne(); + $document = DocumentFactory::createOne(); $token = $this->JWTManager->create($user->object()); $this->browser() - ->get('/api/documents', [ + ->get('/api/document_objects', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], @@ -69,7 +73,7 @@ class DocumentObjectResourceTest extends KernelTestCase ; $this->browser() - ->post('/api/documents', [ + ->post('/api/document_objects', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'multipart/form-data' @@ -77,29 +81,38 @@ class DocumentObjectResourceTest extends KernelTestCase 'files' => [ 'file' => $file, ], - 'partner' => '/api/partners/' . $partner->getId(), - 'product' => '/api/products/' . $product->getId(), ]) ->assertSuccessful() ; $this->browser() - ->delete('/api/documents/1', + ->patch('/api/documents/' . $document->getId(), [ + 'json' => [ + 'documentObject' => '/api/document_objects/' . $documentObject->getId() + ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'application/merge-patch+json' ], ]) ->assertSuccessful() ; + + $this->browser() + ->delete('/api/document_objects/' . $documentObject->getId(), + [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]) + ->assertStatus(500) + ; } public function testDeleteDocumentObject() { - MediaObjectProductFactory::createOne(); - $partner = PartnerFactory::createOne(); - $product = ProductFactory::createOne(); - $documentsObject = DocumentObjectFactory::createOne(); + $mediaObject = MediaObjectLogoFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', @@ -109,8 +122,9 @@ class DocumentObjectResourceTest extends KernelTestCase ] ); $token = $this->JWTManager->create($user->object()); + $documentObject = DocumentObjectFactory::createOne(); $this->browser() - ->delete('/api/documents/' . $documentsObject->getId(), [ + ->delete('/api/document_objects/' . $documentObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], diff --git a/tests/Functional/DocumentResourceTest.php b/tests/Functional/DocumentResourceTest.php new file mode 100644 index 0000000..0ebb82a --- /dev/null +++ b/tests/Functional/DocumentResourceTest.php @@ -0,0 +1,114 @@ + + * @date 01.03.24 + */ + + +namespace App\Tests\Functional; + +use App\Entity\DocumentObject; +use App\Factory\DocumentFactory; +use App\Factory\DocumentObjectFactory; +use App\Factory\MediaObjectLogoFactory; +use App\Factory\MediaObjectProductFactory; +use App\Factory\MediaObjectUserFactory; +use App\Factory\PartnerFactory; +use App\Factory\ProductFactory; +use App\Factory\UserFactory; +use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\HttpFoundation\File\UploadedFile; +use Zenstruck\Browser\Test\HasBrowser; +use Zenstruck\Foundry\Test\Factories; +use Zenstruck\Foundry\Test\ResetDatabase; + +class DocumentResourceTest extends KernelTestCase +{ + use HasBrowser; + use ResetDatabase; + use Factories; + + private JWTTokenManagerInterface $JWTManager; + private string $projectDir; + + protected function setUp(): void + { + parent::setUp(); + $this->JWTManager = self::getContainer()->get('lexik_jwt_authentication.jwt_manager'); + $this->projectDir = self::getContainer()->get('kernel')->getProjectDir(); + } + + public function testCreateDocument(): void + { + MediaObjectUserFactory::createOne(); + $user = UserFactory::createOne( + [ + 'firstName' => 'Peter', + 'lastName' => 'Test', + 'password' => 'test', + 'email' => 'peter@test.de', + ] + ); + MediaObjectLogoFactory::createOne(); + MediaObjectProductFactory::createOne(); + $partner = PartnerFactory::createOne(); + $product = ProductFactory::createOne(); + $documentObject = DocumentObjectFactory::createOne(); + + $token = $this->JWTManager->create($user->object()); + + $this->browser() + ->get('/api/documents', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]) + ->assertSuccessful() + ; + + $this->browser() + ->post('/api/documents', [ + 'json' => [ + 'name' => 'my business file', + 'description' => 'test', + 'partner' => '/api/partners/' . $partner->getId(), + 'product' => '/api/products/' . $product->getId(), + 'documentObject' => '/api/document_objects/' . $documentObject->getId() + ], + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ] + ]) + ->assertSuccessful() + ; + } + + public function testDeleteDocument() + { + MediaObjectUserFactory::createOne(); + $user = UserFactory::createOne( + [ + 'firstName' => 'Peter', + 'lastName' => 'Test', + 'password' => 'test', + 'email' => 'peter@test.de', + 'roles' => ["ROLE_ADMIN"] + ] + ); + $token = $this->JWTManager->create($user->object()); + DocumentObjectFactory::createOne(); + PartnerFactory::createOne(); + ProductFactory::createOne(); + $document = DocumentFactory::createOne(); + + $this->browser() + ->delete('/api/documents/' . $document->getId(), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]) + ->assertSuccessful() + ; + } +} diff --git a/tests/Functional/MediaObjectResourceTest.php b/tests/Functional/MediaObjectResourceTest.php index 882f669..490b6c9 100644 --- a/tests/Functional/MediaObjectResourceTest.php +++ b/tests/Functional/MediaObjectResourceTest.php @@ -8,6 +8,7 @@ namespace App\Tests\Functional; use App\Factory\MediaObjectLogoFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\UserFactory; use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; @@ -42,6 +43,7 @@ class MediaObjectResourceTest extends KernelTestCase $file = new UploadedFile($dstFile, 'image.png'); + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', @@ -53,7 +55,7 @@ class MediaObjectResourceTest extends KernelTestCase $token = $this->JWTManager->create($user->object()); $this->browser() - ->get('/api/medias', [ + ->get('/api/media_objects', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], @@ -62,7 +64,7 @@ class MediaObjectResourceTest extends KernelTestCase ; $this->browser() - ->post('/api/medias', [ + ->post('/api/media_objects', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'multipart/form-data' @@ -79,7 +81,7 @@ class MediaObjectResourceTest extends KernelTestCase ->patch('/api/partners/' . $partner->getId(), [ 'json' => [ - 'logo' => '/api/medias/1' + 'logo' => '/api/media_objects/1' ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, @@ -90,7 +92,7 @@ class MediaObjectResourceTest extends KernelTestCase ; $this->browser() - ->delete('/api/medias/1', + ->delete('/api/media_objects/1', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, @@ -102,6 +104,7 @@ class MediaObjectResourceTest extends KernelTestCase public function testDeleteMediaObject() { + MediaObjectUserFactory::createOne(); $mediaObject = MediaObjectLogoFactory::createOne(); $user = UserFactory::createOne( [ @@ -113,7 +116,7 @@ class MediaObjectResourceTest extends KernelTestCase ); $token = $this->JWTManager->create($user->object()); $this->browser() - ->delete('/api/medias/' . $mediaObject->getId(), [ + ->delete('/api/media_objects/' . $mediaObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], diff --git a/tests/Functional/PartnerFollowResourceTest.php b/tests/Functional/PartnerFollowResourceTest.php index 3b7c6c9..6c1e28c 100644 --- a/tests/Functional/PartnerFollowResourceTest.php +++ b/tests/Functional/PartnerFollowResourceTest.php @@ -8,6 +8,7 @@ namespace App\Tests\Functional; use App\Factory\MediaObjectLogoFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\PartnerFollowFactory; use App\Factory\UserFactory; @@ -33,6 +34,7 @@ class PartnerFollowResourceTest extends KernelTestCase public function testPostPartnerFollow(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', diff --git a/tests/Functional/PartnerResourceTest.php b/tests/Functional/PartnerResourceTest.php index 380345f..52bd544 100644 --- a/tests/Functional/PartnerResourceTest.php +++ b/tests/Functional/PartnerResourceTest.php @@ -9,6 +9,7 @@ namespace App\Tests\Functional; use App\Enum\PartnerType; use App\Factory\MediaObjectLogoFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\ProductFactory; use App\Factory\SaleFactory; @@ -35,6 +36,7 @@ class PartnerResourceTest extends KernelTestCase public function testPostPartner(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'email' => 'peter@test.de', @@ -62,7 +64,7 @@ class PartnerResourceTest extends KernelTestCase 'city' => 'test city', 'country' => 'test country', 'website' => 'wwe.test.de', - 'logo' => '/api/medias/' . $mediaObject->getId(), + 'logo' => '/api/media_objects/' . $mediaObject->getId(), ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, @@ -86,7 +88,7 @@ class PartnerResourceTest extends KernelTestCase ; $this->browser() - ->delete('/api/medias/' . $mediaObject->getId(), [ + ->delete('/api/media_objects/' . $mediaObject->getId(), [ 'headers' => [ 'Authorization' => 'Bearer ' . $token, ], diff --git a/tests/Functional/PostingResourceTest.php b/tests/Functional/PostingResourceTest.php index c845da6..c04c1a5 100644 --- a/tests/Functional/PostingResourceTest.php +++ b/tests/Functional/PostingResourceTest.php @@ -10,6 +10,7 @@ namespace App\Tests\Functional; use App\Factory\ContactFactory; use App\Factory\MediaObjectLogoFactory; use App\Factory\MediaObjectContactFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\PostingFactory; use App\Factory\ProductFactory; @@ -37,6 +38,7 @@ class PostingResourceTest extends KernelTestCase public function testPostPosting(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'email' => 'peter@test.de', diff --git a/tests/Functional/ProductResourceTest.php b/tests/Functional/ProductResourceTest.php index 45a1b6b..dd0a994 100644 --- a/tests/Functional/ProductResourceTest.php +++ b/tests/Functional/ProductResourceTest.php @@ -7,13 +7,8 @@ namespace App\Tests\Functional; -use App\Factory\ContactFactory; -use App\Factory\MediaObjectLogoFactory; -use App\Factory\MediaObjectContactFactory; use App\Factory\MediaObjectProductFactory; use App\Factory\MediaObjectUserFactory; -use App\Factory\PartnerFactory; -use App\Factory\PostingFactory; use App\Factory\UserFactory; use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -37,6 +32,7 @@ class ProductResourceTest extends KernelTestCase public function testPostProduct(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'email' => 'peter@test.de', @@ -54,7 +50,7 @@ class ProductResourceTest extends KernelTestCase 'json' => [ 'name' => 'Test', 'description' => 'more test', - 'image' => '/api/medias/' . $mediaObject->getId(), + 'image' => '/api/media_objects/' . $mediaObject->getId(), ], 'headers' => [ 'Authorization' => 'Bearer ' . $token, diff --git a/tests/Functional/TaskNoteResourceTest.php b/tests/Functional/TaskNoteResourceTest.php index 6cf812b..891a419 100644 --- a/tests/Functional/TaskNoteResourceTest.php +++ b/tests/Functional/TaskNoteResourceTest.php @@ -11,6 +11,7 @@ use App\Enum\PartnerType; use App\Factory\CommentFactory; use App\Factory\MediaObjectLogoFactory; use App\Factory\MediaObjectContactFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\PostingFactory; use App\Factory\TaskFactory; @@ -38,6 +39,7 @@ class TaskNoteResourceTest extends KernelTestCase public function testPostTaskNote(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', diff --git a/tests/Functional/TaskResourceTest.php b/tests/Functional/TaskResourceTest.php index 3e69391..9c9c38f 100644 --- a/tests/Functional/TaskResourceTest.php +++ b/tests/Functional/TaskResourceTest.php @@ -12,6 +12,7 @@ use App\Factory\CommentFactory; use App\Factory\ContactFactory; use App\Factory\MediaObjectLogoFactory; use App\Factory\MediaObjectContactFactory; +use App\Factory\MediaObjectUserFactory; use App\Factory\PartnerFactory; use App\Factory\PostingFactory; use App\Factory\UserFactory; @@ -37,6 +38,7 @@ class TaskResourceTest extends KernelTestCase public function testPostTask(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'firstName' => 'Peter', diff --git a/tests/Functional/UserResourceTest.php b/tests/Functional/UserResourceTest.php index f28288d..2f256e4 100644 --- a/tests/Functional/UserResourceTest.php +++ b/tests/Functional/UserResourceTest.php @@ -7,6 +7,7 @@ namespace App\Tests\Functional; +use App\Factory\MediaObjectUserFactory; use App\Factory\UserFactory; use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -31,6 +32,7 @@ class UserResourceTest extends KernelTestCase public function testPostUser(): void { + MediaObjectUserFactory::createOne(); UserFactory::createOne( [ 'email' => 'peter@test.de', @@ -88,6 +90,7 @@ class UserResourceTest extends KernelTestCase public function testPostUserNoAdmin(): void { + MediaObjectUserFactory::createOne(); $user = UserFactory::createOne( [ 'email' => 'peter@test.de',