Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests to Domain\Package\Package.php #87

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 78 additions & 7 deletions tests/unit/Domain/Package/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
use PHPUnit\Framework\TestCase;

final class PackageTest extends TestCase {
private array $packageAttributes;
private Package $package;
private DateTimeImmutable $createdAt;

public function setUp(): void {
$this->package = new Package(
name: 'vendor/project',
description: 'An awesome package description',
latestVersion: '1.0',
url: 'http://'
);
$this->packageAttributes = [
'name' => 'vendor/project',
'description' => 'An awesome package description',
'latestVersion' => '1.0',
'url' => 'http://'
];

$this->package = new Package(...$this->packageAttributes);

$this->createdAt = $this->package->getCreatedAt();
}
Expand Down Expand Up @@ -51,11 +54,24 @@ public function testDescriptionHasBeenUpdated(): void {
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getUpdatedAt());
}

public function testDescriptionDoesNotUpdateWhenItsTheSame(): void {
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getCreatedAt());
$this->assertFalse($this->package->isDirty());
$this->assertNull($this->package->getUpdatedAt());

$this->package = $this->package->withDescription('An awesome package description');

$this->assertFalse($this->package->isDirty());
$this->assertSame('An awesome package description', $this->package->getDescription());
$this->assertSame($this->createdAt, $this->package->getCreatedAt());
$this->assertNull($this->package->getUpdatedAt());
}

public function testLatestVersion(): void {
$this->assertSame('1.0', $this->package->getLatestVersion());
}

public function testLatestversionHasBeenUpdated(): void {
public function testLatestVersionHasBeenUpdated(): void {
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getCreatedAt());
$this->assertFalse($this->package->isDirty());
$this->assertNull($this->package->getUpdatedAt());
Expand All @@ -68,6 +84,19 @@ public function testLatestversionHasBeenUpdated(): void {
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getUpdatedAt());
}

public function testLatestVersionDoesNotUpdateWhenItsTheSame(): void {
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getCreatedAt());
$this->assertFalse($this->package->isDirty());
$this->assertNull($this->package->getUpdatedAt());

$this->package = $this->package->withLatestVersion('1.0');

$this->assertFalse($this->package->isDirty());
$this->assertSame('1.0', $this->package->getLatestVersion());
$this->assertSame($this->createdAt, $this->package->getCreatedAt());
$this->assertNull($this->package->getUpdatedAt());
}

public function testUrl(): void {
$this->assertSame('http://', $this->package->getUrl());
}
Expand All @@ -84,4 +113,46 @@ public function testUrlHasBeenUpdated(): void {
$this->assertSame($this->createdAt, $this->package->getCreatedAt());
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getUpdatedAt());
}

public function testUrlDoesNotUpdateWhenItsTheSame(): void {
$this->assertInstanceOf(DateTimeImmutable::class, $this->package->getCreatedAt());
$this->assertFalse($this->package->isDirty());
$this->assertNull($this->package->getUpdatedAt());

$this->package = $this->package->withUrl('http://');

$this->assertFalse($this->package->isDirty());
$this->assertSame('http://', $this->package->getUrl());
$this->assertSame($this->createdAt, $this->package->getCreatedAt());
$this->assertNull($this->package->getUpdatedAt());
}

public function testJsonSerializeWhenPackageWasNotUpdated() {
$packageWasNotUpdated = array_merge(
$this->packageAttributes,
[
'createdAt' => $this->createdAt,
'updatedAt' => null
]
);

$this->assertIsArray($this->package->jsonSerialize());
$this->assertSame($packageWasNotUpdated, $this->package->jsonSerialize());
}

public function testJsonSerializeWhenPackageWasUpdated(): void {
$this->package = $this->package->withLatestVersion('1.0.1');

$packageWasUpdated = array_merge(
$this->packageAttributes,
[
'latestVersion' => '1.0.1',
'createdAt' => $this->createdAt,
'updatedAt' => $this->package->getUpdatedAt()
]
);

$this->assertIsArray($this->package->jsonSerialize());
$this->assertSame($packageWasUpdated, $this->package->jsonSerialize());
}
}