Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
use ::class constants in tests
  • Loading branch information
Art4 committed Jul 9, 2024
commit e616e2a1ee4d5fc08e7871f0b13565fa3864d7a7
43 changes: 25 additions & 18 deletions tests/Functional/ErrorParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

use Art4\JsonApiClient\Helper\Parser;
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
use Art4\JsonApiClient\V1\Document;
use Art4\JsonApiClient\V1\Error;
use Art4\JsonApiClient\V1\ErrorCollection;
use Art4\JsonApiClient\V1\ErrorLink;
use Art4\JsonApiClient\V1\ErrorSource;
use Art4\JsonApiClient\V1\Link;
use Art4\JsonApiClient\V1\Meta;
use PHPUnit\Framework\TestCase;

class ErrorParsingTest extends TestCase
Expand All @@ -21,7 +28,7 @@ public function testParseErrors(): void
$string = $this->getJsonString('09_errors.json');
$document = Parser::parseResponseString($string);

$this->assertInstanceOf('Art4\JsonApiClient\V1\Document', $document);
$this->assertInstanceOf(Document::class, $document);
$this->assertTrue($document->has('errors'));
$this->assertFalse($document->has('meta'));
$this->assertTrue($document->has('jsonapi'));
Expand All @@ -30,37 +37,37 @@ public function testParseErrors(): void
$this->assertFalse($document->has('data'));

$errors = $document->get('errors');
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorCollection', $errors);
$this->assertInstanceOf(ErrorCollection::class, $errors);
$this->assertCount(3, $errors->getKeys());

$this->assertTrue($errors->has('0'));
$error0 = $errors->get('0');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Error', $error0);
$this->assertInstanceOf(Error::class, $error0);
$this->assertCount(5, $error0->getKeys());
$this->assertTrue($error0->has('code'));
$this->assertSame('123', $error0->get('code'));
$this->assertTrue($error0->has('source'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorSource', $error0->get('source'));
$this->assertInstanceOf(ErrorSource::class, $error0->get('source'));
$this->assertTrue($error0->has('source.pointer'));
$this->assertSame('/data/attributes/first-name', $error0->get('source.pointer'));
$this->assertTrue($error0->has('title'));
$this->assertSame('Value is too short', $error0->get('title'));
$this->assertTrue($error0->has('detail'));
$this->assertSame('First name must contain at least three characters.', $error0->get('detail'));
$this->assertTrue($error0->has('meta'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\Meta', $error0->get('meta'));
$this->assertInstanceOf(Meta::class, $error0->get('meta'));
$this->assertSame('bar', $error0->get('meta.foo'));

$this->assertTrue($errors->has('1'));
$error1 = $errors->get('1');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Error', $error1);
$this->assertInstanceOf(Error::class, $error1);
$this->assertCount(4, $error1->getKeys());
$this->assertTrue($error1->has('code'));
$this->assertSame('225', $error1->get('code'));
$this->assertTrue($error1->has('source'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorSource', $error1->get('source'));
$this->assertInstanceOf(ErrorSource::class, $error1->get('source'));
$this->assertTrue($error1->has('source.pointer'));
$this->assertSame('/data/attributes/password', $error1->get('source.pointer'));
$this->assertTrue($error1->has('title'));
Expand All @@ -71,12 +78,12 @@ public function testParseErrors(): void
$this->assertTrue($errors->has('2'));
$error2 = $errors->get('2');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Error', $error2);
$this->assertInstanceOf(Error::class, $error2);
$this->assertCount(3, $error2->getKeys());
$this->assertTrue($error2->has('code'));
$this->assertSame('226', $error2->get('code'));
$this->assertTrue($error2->has('source'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorSource', $error2->get('source'));
$this->assertInstanceOf(ErrorSource::class, $error2->get('source'));
$this->assertTrue($error2->has('source.pointer'));
$this->assertSame('/data/attributes/password', $error2->get('source.pointer'));
$this->assertTrue($error2->has('title'));
Expand All @@ -91,7 +98,7 @@ public function testParseErrorWithLinks(): void
$string = $this->getJsonString('10_error_with_links.json');
$document = Parser::parseResponseString($string);

$this->assertInstanceOf('Art4\JsonApiClient\V1\Document', $document);
$this->assertInstanceOf(Document::class, $document);
$this->assertTrue($document->has('errors'));
$this->assertFalse($document->has('meta'));
$this->assertTrue($document->has('jsonapi'));
Expand All @@ -100,42 +107,42 @@ public function testParseErrorWithLinks(): void
$this->assertFalse($document->has('data'));

$errors = $document->get('errors');
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorCollection', $errors);
$this->assertInstanceOf(ErrorCollection::class, $errors);
$this->assertCount(2, $errors->getKeys());

$this->assertTrue($errors->has('0'));
$error0 = $errors->get('0');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Error', $error0);
$this->assertInstanceOf(Error::class, $error0);
$this->assertCount(4, $error0->getKeys());
$this->assertTrue($error0->has('code'));
$this->assertSame('123', $error0->get('code'));
$this->assertTrue($error0->has('source'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorSource', $error0->get('source'));
$this->assertInstanceOf(ErrorSource::class, $error0->get('source'));
$this->assertTrue($error0->has('source.pointer'));
$this->assertSame('/data/attributes/first-name', $error0->get('source.pointer'));
$this->assertTrue($error0->has('title'));
$this->assertSame('Value is too short', $error0->get('title'));
$this->assertTrue($error0->has('links'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorLink', $error0->get('links'));
$this->assertInstanceOf(ErrorLink::class, $error0->get('links'));
$this->assertTrue($error0->has('links.about'));
$this->assertSame('http://example.org/errors/123', $error0->get('links.about'));

$this->assertTrue($errors->has('1'));
$error1 = $errors->get('1');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Error', $error1);
$this->assertInstanceOf(Error::class, $error1);
$this->assertCount(2, $error1->getKeys());
$this->assertTrue($error1->has('code'));
$this->assertSame('124', $error1->get('code'));
$this->assertTrue($error1->has('links'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\ErrorLink', $error1->get('links'));
$this->assertInstanceOf(ErrorLink::class, $error1->get('links'));
$this->assertTrue($error1->has('links.about'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\Link', $error1->get('links.about'));
$this->assertInstanceOf(Link::class, $error1->get('links.about'));
$this->assertTrue($error1->has('links.about.href'));
$this->assertSame('http://example.org/errors/124', $error1->get('links.about.href'));
$this->assertTrue($error1->has('links.meta'));
$this->assertInstanceOf('Art4\JsonApiClient\V1\Link', $error1->get('links.meta'));
$this->assertInstanceOf(Link::class, $error1->get('links.meta'));
$this->assertTrue($error1->has('links.meta.href'));
$this->assertSame('http://example.org/meta', $error1->get('links.meta.href'));

Expand Down
Loading