Skip to content

Commit

Permalink
Fixed model deserialization issues after release. (#23)
Browse files Browse the repository at this point in the history
Fixed invalid types in deserialization for models.
Added basic tests from test fixtures.
  • Loading branch information
m1x0n authored Apr 15, 2022
1 parent 682081e commit ad4ab30
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ Contributions
Contributions are highly appreciated.

Feel free to file an issue, send a PR, make a suggestion etc.

TODO
---------------------

Use jms/serializer for deserialization
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ parameters:
- src
- tests
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
2 changes: 1 addition & 1 deletion src/HelpScoutDocs/Models/ArticleRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(stdClass $data = null)
$this->createdBy = $data->createdBy ?? null;
$this->updatedBy = $data->updatedBy ?? null;
$this->createdAt = $data->createdAt ?? null;
$this->createdBy = $data->updatedAt ?? null;
$this->updatedAt = $data->updatedAt ?? null;
$this->lastPublishedAt = $data->lastPublishedAt ?? null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HelpScoutDocs/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(stdClass $data = null)
$this->createdBy = $data->createdBy ?? null;
$this->updatedBy = $data->updatedBy ?? null;
$this->createdAt = $data->createdAt ?? null;
$this->createdBy = $data->updatedAt ?? null;
$this->updatedAt = $data->updatedAt ?? null;
}
}

Expand Down
30 changes: 18 additions & 12 deletions src/HelpScoutDocs/Models/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@ class Redirect extends DocsModel

public function __construct(stdClass $data = null)
{
if ($data) {
$this->id = $data->id ?? null;
$this->siteId = $data->siteId ?? null;
$this->urlMapping = $data->urlMapping ?? null;
$this->type = $data->type ?? null;
$this->documentId = $data->documentId ?? null;
$this->anchor = $data->anchor ?? null;
$this->redirect = $data->redirect ?? null;
$this->createdBy = $data->createdBy ?? null;
$this->updatedBy = $data->updatedBy ?? null;
$this->createdAt = $data->createdAt ?? null;
$this->createdBy = $data->updatedAt ?? null;
$redirect = null;

if ($data && property_exists($data, 'redirect')) {
$redirect = $data->redirect;
}

if ($redirect) {
$this->id = $redirect->id ?? null;
$this->siteId = $redirect->siteId ?? null;
$this->urlMapping = $redirect->urlMapping ?? null;
$this->type = $redirect->type ?? null;
$this->documentId = $redirect->documentId ?? null;
$this->anchor = $redirect->anchor ?? null;
$this->redirect = $redirect->redirect ?? null;
$this->createdBy = $redirect->createdBy ?? null;
$this->updatedBy = $redirect->updatedBy ?? null;
$this->createdAt = $redirect->createdAt ?? null;
$this->updatedAt = $redirect->updatedAt ?? null;
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/HelpScoutDocs/FixtureLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace HelpScoutDocs\Tests;

use JsonException;
use RuntimeException;
use stdClass;

trait FixtureLoader
{
/**
* @throws JsonException
* @throws RuntimeException
*/
public function loadFixtureAsStdClass(string $path): stdClass
{
$data = file_get_contents($path);
if ($data === false) {
throw new RuntimeException("Failed to read fixture context of $path");
}
return json_decode($data, false, 512, JSON_THROW_ON_ERROR);
}
}
61 changes: 61 additions & 0 deletions tests/HelpScoutDocs/Models/ModelDeserializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace HelpScoutDocs\Tests\Models;

use HelpScoutDocs\Models\Article;
use HelpScoutDocs\Models\ArticleRef;
use HelpScoutDocs\Models\ArticleRevision;
use HelpScoutDocs\Models\ArticleRevisionRef;
use HelpScoutDocs\Models\ArticleSearch;
use HelpScoutDocs\Models\Category;
use HelpScoutDocs\Models\Collection;
use HelpScoutDocs\Models\Person;
use HelpScoutDocs\Models\Redirect;
use HelpScoutDocs\Models\Site;
use HelpScoutDocs\Tests\FixtureLoader;
use PHPUnit\Framework\TestCase;
use stdClass;

class ModelDeserializationTest extends TestCase
{
use FixtureLoader;

private const FIXTURE_PATH = __DIR__ . '/fixtures/';

/**
* @param string $className
* @param stdClass $fixture
* @return void
* @dataProvider dataProvider
*/
public function testShouldDeserializeModels(string $className, stdClass $fixture): void
{
new $className($fixture);
}

/**
* @return array[]
*/
public function dataProvider(): array
{
return [
'Article' => [Article::class, $this->loadFixture('article')],
'ArticleRef' => [ArticleRef::class, $this->loadFixture('article_ref')],
'ArticleRevision' => [ArticleRevision::class, $this->loadFixture('article_revision')],
'ArticleRevisionRef' => [ArticleRevisionRef::class, $this->loadFixture('article_revision_ref')],
'ArticleSearch' => [ArticleSearch::class, $this->loadFixture('article_search')],
'Category' => [Category::class, $this->loadFixture('category')],
'Collection' => [Collection::class, $this->loadFixture('collection')],
'Person' => [Person::class, $this->loadFixture('person')],
'Redirect' => [Redirect::class, $this->loadFixture('redirect')],
'Site' => [Site::class, $this->loadFixture('site')],
];
}

private function loadFixture(string $name): stdClass
{
return $this->loadFixtureAsStdClass(self::FIXTURE_PATH . $name . '.json');
}
}
25 changes: 25 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/article.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "521632244566c845e582652d",
"number": 125,
"collectionId": "5214c77c45667acd25394b51",
"slug": "my-article",
"status": "published",
"hasDraft": false,
"name": "My Article",
"text": "This is the text of the article.",
"categories": [
"5214c77d45667acd25394b52"
],
"related": [
"521632244566c845e582652b",
"521632244566c845e582652c"
],
"publicUrl": "https://docs.helpscout.net/article/100-my-article",
"popularity": 4.3,
"viewCount": 237,
"createdBy": 73423,
"updatedBy": 73423,
"createdAt": "2013-08-22T15:45:40Z",
"updatedAt": "2013-08-22T21:40:56Z",
"lastPublishedAt": "2013-08-22T21:40:56Z"
}
17 changes: 17 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/article_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "521632244566c845e582652d",
"number": 125,
"collectionId": "522f91311ef00a6bca026c9d",
"slug": "my-article",
"status": "published",
"hasDraft": false,
"name": "My Article",
"publicUrl": "https://docs.helpscout.net/article/100-my-article",
"popularity": 4.3,
"viewCount": 237,
"createdBy": 73423,
"updatedBy": 73423,
"createdAt": "2013-08-22T15:45:40Z",
"updatedAt": "2013-08-22T21:40:56Z",
"lastPublishedAt": "2013-08-22T21:40:56Z"
}
11 changes: 11 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/article_revision.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "5230deaa456636123f6363d0",
"articleId": "522f918d1ef00a6bca026ca0",
"text": "This is the text of the article.",
"createdBy": {
"id": 73423,
"firstName": "John",
"lastName": "Appleseed"
},
"createdAt": "2013-09-11T21:20:42Z"
}
10 changes: 10 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/article_revision_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "5230deaa456636123f6363d0",
"articleId": "522f918d1ef00a6bca026ca0",
"createdBy": {
"id": 73423,
"firstName": "John",
"lastName": "Appleseed"
},
"createdAt": "2013-09-11T21:20:42Z"
}
15 changes: 15 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/article_search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "5215163545667acd25394b5c",
"collectionId": "5214c77c45667acd25394b51",
"categoryIds": [
"5214c77d45667acd25394b52"
],
"slug": "my-article",
"name": "My Article",
"preview": "This is my article.",
"url": "https://docs.helpscout.net/article/14-my-article",
"docsUrl": "/docs/5214c77c45667acd25394b51/article/5215163545667acd25394b5c",
"number": 14,
"status": "published",
"visibility": "public"
}
18 changes: 18 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": "5214c83d45667acd25394b54",
"number": 133,
"slug": "my-category",
"visibility": "public",
"collectionId": "5214c83d45667acd25394b53",
"order": 1,
"defaultSort": "custom",
"name": "My Category",
"description": "",
"articleCount": 21,
"publishedArticleCount": 19,
"publicUrl": "https://mysite.helpscoutdocs.com/category/133-my-category",
"createdBy": 73423,
"updatedBy": null,
"createdAt": "2013-08-21T14:01:33Z",
"updatedAt": null
}
17 changes: 17 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "5214c83d45667acd25394b53",
"siteId": "52404efc4566740003092640",
"number": 33,
"slug": "my-collection",
"visibility": "public",
"order": 1,
"name": "My Collection",
"description": "Description of my collection",
"publicUrl": "https://my-docs.helpscoutdocs.com/collection/1-test",
"articleCount": 3,
"publishedArticleCount": 1,
"createdBy": 73423,
"updatedBy": 73423,
"createdAt": "2013-08-21T14:01:33Z",
"updatedAt": "2013-08-21T14:01:33Z"
}
5 changes: 5 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/person.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 23478,
"firstName": "John",
"lastName": "Appleseed"
}
14 changes: 14 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/redirect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"redirect": {
"id": "573df74195ac0e2ad08ab695",
"siteId": "52444ccf3e3e9bd67a3dc68c",
"urlMapping": "/previous/url/1234",
"documentId": "525217bae4b0cb13e565bb9d",
"type": "article",
"redirect": "https://docs.helpscout.net/article/488-transfer-account",
"createdBy": 4577,
"modifiedBy": 4577,
"createdAt": "2016-05-19T17:26:25Z",
"modifiedAt": "2016-05-19T17:26:25Z"
}
}
27 changes: 27 additions & 0 deletions tests/HelpScoutDocs/Models/fixtures/site.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"id": 23478,
"status": "active",
"subDomain": "helpscout",
"cname": "docs.helpscout.net",
"hasPublicSite": true,
"companyName": "Help Scout",
"title": "Help Scout Support",
"logoUrl": "http://helpscout.net/logo.png",
"logoWidth": 174,
"logoHeight": 40,
"favIconUrl": "http://helpscout.net/favicon.png",
"touchIconUrl": "http://helpscout.net/touchicon.png",
"homeUrl": "http://helpscout.net",
"homeLinkText": "Home",
"bgColor": "#444444",
"description": "Frequently asked questions and support documentation for Help Scout",
"hasContactForm": true,
"mailboxId": 968,
"contactEmail": null,
"styleSheetUrl": "http://helpscout.net/docs-style.css",
"headerCode": null,
"createdBy": 29456,
"updatedBy": 29456,
"createdAt": "2013-12-10T15:09:52Z",
"updatedAt": "2013-12-10T15:09:52Z"
}

0 comments on commit ad4ab30

Please sign in to comment.