-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
flavioheleno
merged 2 commits into
package-health:develop
from
marcelo-lipienski:add-tests-to-preference-class
Jul 13, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pq
assertNull
e dpsassertSame
? (o segundo tá errado hehe)There was a problem hiding this comment.
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