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 tests to Domain\Preference\Preference.php #89

Merged
Changes from 1 commit
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
277 changes: 277 additions & 0 deletions tests/Unit/Domain/Preference/PreferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
<?php
declare(strict_types = 1);

namespace PackageHealth\PHP\Test\Unit\Domain\Preference;

use DateTimeImmutable;
use PackageHealth\PHP\Domain\Preference\{Preference, PreferenceTypeEnum};
use PHPUnit\Framework\TestCase;

final class PreferenceTest extends TestCase {
private array $preferenceAttributes;
private Preference $preference;
private DateTimeImmutable $createdAt;

public function setUp(): void {
$this->preferenceAttributes = [
'id' => null,
'category' => 'Category',
'property' => 'Property',
'value' => 'Value'
];

$this->preference = new Preference(...$this->preferenceAttributes);

$this->createdAt = $this->preference->getCreatedAt();
}

public function testIdNotGiven(): void {
$this->assertNull($this->preference->getId());
$this->assertSame(null, $this->preference->getId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pq assertNull e dps assertSame? (o segundo tá errado hehe)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errado não está, mas também não está certo hahaha

}

public function testIdGiven(): void {
$id = 1;

$preferenceAttributes = array_merge(
$this->preferenceAttributes,
[
'id' => $id
]
);

$preference = new Preference(...$preferenceAttributes);

$this->assertSame($id, $preference->getId());
}

public function testCategory(): void {
$this->assertSame('Category', $this->preference->getCategory());
}

public function testProperty(): void {
$this->assertSame('Property', $this->preference->getProperty());
}

public function testType(): void {
$this->assertSame(PreferenceTypeEnum::isString, $this->preference->getType());
}

public function testCreatedAt(): void {
$this->assertSame($this->createdAt, $this->preference->getCreatedAt());
}

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

/**
* @depends testObjectIsInItsInitialState
*/
public function testValueAsString(): array {
$preference = $this->preference->withStringValue('Value');

$this->assertSame('Value', $preference->getValueAsString());

return [$preference, $this->createdAt];
}

/**
* @depends testObjectIsInItsInitialState
*/
public function testValueAsInteger(): array {
$preference = $this->preference->withIntegerValue(1);

$this->assertSame(1, $preference->getValueAsInteger());

return [$preference, $this->createdAt];
}

/**
* @depends testObjectIsInItsInitialState
*/
public function testValueAsFloat(): array {
$preference = $this->preference->withFloatValue(1.0);

$this->assertSame(1.0, $preference->getValueAsFloat());

return [$preference, $this->createdAt];
}

/**
* @depends testObjectIsInItsInitialState
*/
public function testValueAsBool(): array {
$preference = $this->preference->withBoolValue(true);

$this->assertSame(true, $preference->getValueAsBool());

return [$preference, $this->createdAt];
}

/**
* @depends testValueAsString
* @depends testValueAsInteger
* @depends testValueAsFloat
* @depends testValueAsBool
*/
public function testAsString(array $testData): void {
[$preference, $createdAt] = $testData;

$this->assertIsString($preference->getValueAsString());
}

/**
* @depends testValueAsString
* @depends testValueAsInteger
* @depends testValueAsFloat
* @depends testValueAsBool
*/
public function testAsInteger(array $testData): void {
[$preference, $createdAt] = $testData;

$this->assertIsInt($preference->getValueAsInteger());
}

/**
* @depends testValueAsString
* @depends testValueAsInteger
* @depends testValueAsFloat
* @depends testValueAsBool
*/
public function testAsFloat(array $testData): void {
[$preference, $createdAt] = $testData;

$this->assertIsFloat($preference->getValueAsFloat());
}

/**
* @depends testValueAsString
* @depends testValueAsInteger
* @depends testValueAsFloat
* @depends testValueAsBool
*/
public function testAsBool(array $test): void {
[$preference, $createdAt] = $test;

$this->assertIsBool($preference->getValueAsBool());
}

/**
* @depends testValueAsString
* @depends testValueAsInteger
* @depends testValueAsFloat
* @depends testValueAsBool
*/
public function testObjectIsDirty(array $testData): void {
[$preference, $createdAt] = $testData;

$this->assertTrue($preference->isDirty());
$this->assertSame($createdAt, $preference->getCreatedAt());
$this->assertInstanceOf(DateTimeImmutable::class, $preference->getUpdatedAt());
}


public function testJsonSerializeWhenPreferenceWasNotUpdated(): array {
$preferenceAttributes = array_merge(
$this->preferenceAttributes,
[
'type' => PreferenceTypeEnum::isString->value,
'createdAt' => $this->createdAt,
'updatedAt' => null
]
);

$this->assertNull($this->preference->getUpdatedAt());

return [$this->preference, $preferenceAttributes];
}

public function testJsonSerializeWhenPreferenceTypeWasUpdatedToString(): array {
$preference = $this->preference->withStringValue('false');

$preferenceAttributes = array_merge(
$this->preferenceAttributes,
[
'value' => 'false',
'type' => PreferenceTypeEnum::isString->value,
'createdAt' => $this->createdAt,
'updatedAt' => $preference->getUpdatedAt()
]
);

$this->assertInstanceOf(DateTimeImmutable::class, $preference->getUpdatedAt());

return [$preference, $preferenceAttributes];
}

public function testJsonSerializeWhenPreferenceTypeWasUpdatedToInteger(): array {
$preference = $this->preference->withIntegerValue(0);

$preferenceAttributes = array_merge(
$this->preferenceAttributes,
[
'value' => '0',
'type' => PreferenceTypeEnum::isInteger->value,
'createdAt' => $this->createdAt,
'updatedAt' => $preference->getUpdatedAt()
]
);

$this->assertInstanceOf(DateTimeImmutable::class, $preference->getUpdatedAt());

return [$preference, $preferenceAttributes];
}

public function testJsonSerializeWhenPreferenceTypeWasUpdatedToFloat(): array {
$preference = $this->preference->withFloatValue(1.0);

$preferenceAttributes = array_merge(
$this->preferenceAttributes,
[
'value' => '1',
'type' => PreferenceTypeEnum::isFloat->value,
'createdAt' => $this->createdAt,
'updatedAt' => $preference->getUpdatedAt()
]
);

$this->assertInstanceOf(DateTimeImmutable::class, $preference->getUpdatedAt());

return [$preference, $preferenceAttributes];
}

public function testJsonSerializeWhenPreferenceTypeWasUpdatedToBool(): array {
$preference = $this->preference->withBoolValue(false);

$preferenceAttributes = array_merge(
$this->preferenceAttributes,
[
'value' => '',
'type' => PreferenceTypeEnum::isBool->value,
'createdAt' => $this->createdAt,
'updatedAt' => $preference->getUpdatedAt()
]
);

$this->assertInstanceOf(DateTimeImmutable::class, $preference->getUpdatedAt());

return [$preference, $preferenceAttributes];
}

/**
* @depends testJsonSerializeWhenPreferenceWasNotUpdated
* @depends testJsonSerializeWhenPreferenceTypeWasUpdatedToString
* @depends testJsonSerializeWhenPreferenceTypeWasUpdatedToInteger
* @depends testJsonSerializeWhenPreferenceTypeWasUpdatedToFloat
* @depends testJsonSerializeWhenPreferenceTypeWasUpdatedToBool
*/
public function testObjectSerializedValuesMatch(array $testData): void {
[$preference, $preferenceAttributes] = $testData;

$this->assertIsArray($preference->jsonSerialize());
$this->assertSame($preferenceAttributes, $preference->jsonSerialize());
}
}