From ad4ab30f47051d3fdaa086ceeffdb5f48acf9377 Mon Sep 17 00:00:00 2001 From: Michael Morozov Date: Fri, 15 Apr 2022 19:03:41 +0300 Subject: [PATCH] Fixed model deserialization issues after release. (#23) Fixed invalid types in deserialization for models. Added basic tests from test fixtures. --- README.md | 5 ++ phpstan.neon | 1 + src/HelpScoutDocs/Models/ArticleRef.php | 2 +- src/HelpScoutDocs/Models/Category.php | 2 +- src/HelpScoutDocs/Models/Redirect.php | 30 +++++---- tests/HelpScoutDocs/FixtureLoader.php | 25 ++++++++ .../Models/ModelDeserializationTest.php | 61 +++++++++++++++++++ .../Models/fixtures/article.json | 25 ++++++++ .../Models/fixtures/article_ref.json | 17 ++++++ .../Models/fixtures/article_revision.json | 11 ++++ .../Models/fixtures/article_revision_ref.json | 10 +++ .../Models/fixtures/article_search.json | 15 +++++ .../Models/fixtures/category.json | 18 ++++++ .../Models/fixtures/collection.json | 17 ++++++ .../HelpScoutDocs/Models/fixtures/person.json | 5 ++ .../Models/fixtures/redirect.json | 14 +++++ tests/HelpScoutDocs/Models/fixtures/site.json | 27 ++++++++ 17 files changed, 271 insertions(+), 14 deletions(-) create mode 100644 tests/HelpScoutDocs/FixtureLoader.php create mode 100644 tests/HelpScoutDocs/Models/ModelDeserializationTest.php create mode 100644 tests/HelpScoutDocs/Models/fixtures/article.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/article_ref.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/article_revision.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/article_revision_ref.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/article_search.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/category.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/collection.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/person.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/redirect.json create mode 100644 tests/HelpScoutDocs/Models/fixtures/site.json diff --git a/README.md b/README.md index f347719..8a79c87 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/phpstan.neon b/phpstan.neon index 5613bea..544ca90 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,3 +4,4 @@ parameters: - src - tests checkGenericClassInNonGenericObjectType: false + checkMissingIterableValueType: false diff --git a/src/HelpScoutDocs/Models/ArticleRef.php b/src/HelpScoutDocs/Models/ArticleRef.php index 263bf6e..9b6683a 100644 --- a/src/HelpScoutDocs/Models/ArticleRef.php +++ b/src/HelpScoutDocs/Models/ArticleRef.php @@ -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; } } diff --git a/src/HelpScoutDocs/Models/Category.php b/src/HelpScoutDocs/Models/Category.php index 993520d..d711bac 100644 --- a/src/HelpScoutDocs/Models/Category.php +++ b/src/HelpScoutDocs/Models/Category.php @@ -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; } } diff --git a/src/HelpScoutDocs/Models/Redirect.php b/src/HelpScoutDocs/Models/Redirect.php index 1f17e37..4024875 100644 --- a/src/HelpScoutDocs/Models/Redirect.php +++ b/src/HelpScoutDocs/Models/Redirect.php @@ -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; } } diff --git a/tests/HelpScoutDocs/FixtureLoader.php b/tests/HelpScoutDocs/FixtureLoader.php new file mode 100644 index 0000000..b2ffae0 --- /dev/null +++ b/tests/HelpScoutDocs/FixtureLoader.php @@ -0,0 +1,25 @@ + [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'); + } +} diff --git a/tests/HelpScoutDocs/Models/fixtures/article.json b/tests/HelpScoutDocs/Models/fixtures/article.json new file mode 100644 index 0000000..3dbfa82 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/article.json @@ -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" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/article_ref.json b/tests/HelpScoutDocs/Models/fixtures/article_ref.json new file mode 100644 index 0000000..1c452f5 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/article_ref.json @@ -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" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/article_revision.json b/tests/HelpScoutDocs/Models/fixtures/article_revision.json new file mode 100644 index 0000000..04b7b09 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/article_revision.json @@ -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" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/article_revision_ref.json b/tests/HelpScoutDocs/Models/fixtures/article_revision_ref.json new file mode 100644 index 0000000..f61c7bf --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/article_revision_ref.json @@ -0,0 +1,10 @@ +{ + "id": "5230deaa456636123f6363d0", + "articleId": "522f918d1ef00a6bca026ca0", + "createdBy": { + "id": 73423, + "firstName": "John", + "lastName": "Appleseed" + }, + "createdAt": "2013-09-11T21:20:42Z" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/article_search.json b/tests/HelpScoutDocs/Models/fixtures/article_search.json new file mode 100644 index 0000000..29845d5 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/article_search.json @@ -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" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/category.json b/tests/HelpScoutDocs/Models/fixtures/category.json new file mode 100644 index 0000000..d1d593d --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/category.json @@ -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 +} diff --git a/tests/HelpScoutDocs/Models/fixtures/collection.json b/tests/HelpScoutDocs/Models/fixtures/collection.json new file mode 100644 index 0000000..9f2af45 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/collection.json @@ -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" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/person.json b/tests/HelpScoutDocs/Models/fixtures/person.json new file mode 100644 index 0000000..07a0315 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/person.json @@ -0,0 +1,5 @@ +{ + "id": 23478, + "firstName": "John", + "lastName": "Appleseed" +} diff --git a/tests/HelpScoutDocs/Models/fixtures/redirect.json b/tests/HelpScoutDocs/Models/fixtures/redirect.json new file mode 100644 index 0000000..35a61f6 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/redirect.json @@ -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" + } +} diff --git a/tests/HelpScoutDocs/Models/fixtures/site.json b/tests/HelpScoutDocs/Models/fixtures/site.json new file mode 100644 index 0000000..e1ff542 --- /dev/null +++ b/tests/HelpScoutDocs/Models/fixtures/site.json @@ -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" +}