-
-
Notifications
You must be signed in to change notification settings - Fork 52
Cover violation file with tests #631
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
Open
AlessandroMinoccheri
wants to merge
1
commit into
main
Choose a base branch
from
feature/cover-violation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−0
Open
Changes from all commits
Commits
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 hidden or 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,95 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Arkitect\Tests\Unit\Rules; | ||
|
|
||
| use Arkitect\Expression\Description; | ||
| use Arkitect\Rules\Violation; | ||
| use Arkitect\Rules\ViolationMessage; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class ViolationTest extends TestCase | ||
| { | ||
| public function test_create_sets_fqcn_error_and_file_path_with_no_line(): void | ||
| { | ||
| $message = ViolationMessage::selfExplanatory(new Description('should be final', '')); | ||
| $violation = Violation::create('App\Domain\Order', $message, 'src/Domain/Order.php'); | ||
|
|
||
| self::assertEquals('App\Domain\Order', $violation->getFqcn()); | ||
| self::assertEquals('should be final', $violation->getError()); | ||
| self::assertNull($violation->getLine()); | ||
| self::assertEquals('src/Domain/Order.php', $violation->getFilePath()); | ||
| } | ||
|
|
||
| public function test_create_with_error_line_sets_all_fields(): void | ||
| { | ||
| $message = ViolationMessage::selfExplanatory(new Description('should be abstract', 'design reasons')); | ||
| $violation = Violation::createWithErrorLine('App\Service\Foo', $message, 42, 'src/Service/Foo.php'); | ||
|
|
||
| self::assertEquals('App\Service\Foo', $violation->getFqcn()); | ||
| self::assertEquals('should be abstract because design reasons', $violation->getError()); | ||
| self::assertEquals(42, $violation->getLine()); | ||
| self::assertEquals('src/Service/Foo.php', $violation->getFilePath()); | ||
| } | ||
|
|
||
| public function test_json_serialize_returns_all_properties(): void | ||
| { | ||
| $violation = new Violation('App\Foo', 'some error', 10, 'src/Foo.php'); | ||
| $data = $violation->jsonSerialize(); | ||
|
|
||
| self::assertArrayHasKey('fqcn', $data); | ||
| self::assertArrayHasKey('error', $data); | ||
| self::assertArrayHasKey('line', $data); | ||
| self::assertArrayHasKey('filePath', $data); | ||
| self::assertEquals('App\Foo', $data['fqcn']); | ||
| self::assertEquals('some error', $data['error']); | ||
| self::assertEquals(10, $data['line']); | ||
| self::assertEquals('src/Foo.php', $data['filePath']); | ||
| } | ||
|
|
||
| public function test_from_json_round_trips_with_file_path(): void | ||
| { | ||
| $original = new Violation('App\Bar', 'must implement interface', 7, 'src/Bar.php'); | ||
| $restored = Violation::fromJson($original->jsonSerialize()); | ||
|
|
||
| self::assertEquals($original->getFqcn(), $restored->getFqcn()); | ||
| self::assertEquals($original->getError(), $restored->getError()); | ||
| self::assertEquals($original->getLine(), $restored->getLine()); | ||
| self::assertEquals($original->getFilePath(), $restored->getFilePath()); | ||
| } | ||
|
|
||
| public function test_from_json_round_trips_without_file_path(): void | ||
| { | ||
| $data = [ | ||
| 'fqcn' => 'App\Baz', | ||
| 'error' => 'should extend BaseClass', | ||
| 'line' => null, | ||
| ]; | ||
| $violation = Violation::fromJson($data); | ||
|
|
||
| self::assertEquals('App\Baz', $violation->getFqcn()); | ||
| self::assertEquals('should extend BaseClass', $violation->getError()); | ||
| self::assertNull($violation->getLine()); | ||
| self::assertNull($violation->getFilePath()); | ||
| } | ||
|
|
||
| public function test_without_line_number_returns_copy_with_null_line(): void | ||
| { | ||
| $violation = new Violation('App\Qux', 'must be final', 99, 'src/Qux.php'); | ||
| $stripped = $violation->withoutLineNumber(); | ||
|
|
||
| self::assertNull($stripped->getLine()); | ||
| self::assertEquals('App\Qux', $stripped->getFqcn()); | ||
| self::assertEquals('must be final', $stripped->getError()); | ||
| self::assertEquals('src/Qux.php', $stripped->getFilePath()); | ||
| } | ||
|
|
||
| public function test_without_line_number_does_not_mutate_original(): void | ||
| { | ||
| $violation = new Violation('App\Qux', 'must be final', 99, 'src/Qux.php'); | ||
| $violation->withoutLineNumber(); | ||
|
|
||
| self::assertEquals(99, $violation->getLine()); | ||
| } | ||
| } | ||
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.
here you wanted to do as above with tojson and then fromjson?