|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CodedMonkey\Dirigent\Tests\FunctionalTests\Doctrine\Entity; |
| 6 | + |
| 7 | +use CodedMonkey\Dirigent\Doctrine\Entity\Distribution; |
| 8 | +use CodedMonkey\Dirigent\Doctrine\Entity\Metadata; |
| 9 | +use CodedMonkey\Dirigent\Package\PackageDistributionResolver; |
| 10 | +use CodedMonkey\Dirigent\Tests\Helper\EntityManagerTestTrait; |
| 11 | +use CodedMonkey\Dirigent\Tests\Helper\KernelTestCaseTrait; |
| 12 | +use CodedMonkey\Dirigent\Tests\Helper\MockEntityFactoryTrait; |
| 13 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 14 | +use Symfony\Component\Filesystem\Filesystem; |
| 15 | + |
| 16 | +class DistributionDeletionTest extends KernelTestCase |
| 17 | +{ |
| 18 | + use EntityManagerTestTrait; |
| 19 | + use KernelTestCaseTrait; |
| 20 | + use MockEntityFactoryTrait; |
| 21 | + |
| 22 | + public function testDeletingDistributionDeletesDistributionFile(): void |
| 23 | + { |
| 24 | + [$package, $version, $metadata] = $this->createMockPackageWithMetadata(); |
| 25 | + [$distribution, $path] = $this->createDistributionFile($metadata); |
| 26 | + $this->persistEntities($package, $version, $metadata, $distribution); |
| 27 | + |
| 28 | + $this->removeEntities($distribution); |
| 29 | + |
| 30 | + self::assertFileDoesNotExist($path); |
| 31 | + } |
| 32 | + |
| 33 | + public function testDeletingMetadataDeletesDistributionFile(): void |
| 34 | + { |
| 35 | + [$package, $version, $metadata] = $this->createMockPackageWithMetadata(); |
| 36 | + [$distribution, $path] = $this->createDistributionFile($metadata); |
| 37 | + |
| 38 | + // Metadata can't be deleted if it's the current metadata of a version, so we replace it |
| 39 | + $currentMetadata = $this->createMockMetadata($version); |
| 40 | + $version->setCurrentMetadata($currentMetadata); |
| 41 | + |
| 42 | + $this->persistEntities($package, $version, $metadata, $currentMetadata, $distribution); |
| 43 | + |
| 44 | + $this->removeEntities($metadata); |
| 45 | + |
| 46 | + self::assertFileDoesNotExist($path); |
| 47 | + } |
| 48 | + |
| 49 | + public function testDeletingPackageDeletesDistributionFile(): void |
| 50 | + { |
| 51 | + [$package, $version, $metadata] = $this->createMockPackageWithMetadata(); |
| 52 | + [$distribution, $path] = $this->createDistributionFile($metadata); |
| 53 | + $this->persistEntities($package, $version, $metadata, $distribution); |
| 54 | + |
| 55 | + $this->removeEntities($package); |
| 56 | + |
| 57 | + self::assertFileDoesNotExist($path); |
| 58 | + } |
| 59 | + |
| 60 | + public function testDeletingVersionDeletesDistributionFile(): void |
| 61 | + { |
| 62 | + [$package, $version, $metadata] = $this->createMockPackageWithMetadata(); |
| 63 | + [$distribution, $path] = $this->createDistributionFile($metadata); |
| 64 | + $this->persistEntities($package, $version, $metadata, $distribution); |
| 65 | + |
| 66 | + $this->removeEntities($version); |
| 67 | + |
| 68 | + self::assertFileDoesNotExist($path); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return array{Distribution, string} |
| 73 | + */ |
| 74 | + private function createDistributionFile(Metadata $metadata): array |
| 75 | + { |
| 76 | + $metadata->setDistributionReference('reference'); |
| 77 | + |
| 78 | + $distribution = new Distribution($metadata, 'zip'); |
| 79 | + $distribution->setResolvedAt(); |
| 80 | + |
| 81 | + $path = $this->getService(PackageDistributionResolver::class)->path($metadata, $distribution->getType()); |
| 82 | + new Filesystem()->dumpFile($path, 'distribution'); |
| 83 | + |
| 84 | + return [$distribution, $path]; |
| 85 | + } |
| 86 | +} |
0 commit comments