Skip to content
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

Type pattern "@...@" is not supported by TextMatcher. #432

Closed
tentacode opened this issue Jan 3, 2023 · 4 comments
Closed

Type pattern "@...@" is not supported by TextMatcher. #432

tentacode opened this issue Jan 3, 2023 · 4 comments

Comments

@tentacode
Copy link

Looks like PHPMatcher uses TextMatcher instead of JsonMatcher, and I can't use any of the cool JSON matcher…

$expectedJsonMatcher = <<<JSON
    {
        "aws": {
            "key": "laclédepassepartout",
            "secret": "lesecretdupèrefouras",
            "@...@"
        }
    }
    JSON;


$matcher = new PHPMatcher;
$hasMatched = $matcher->match($actualJson, $expectedJsonMatcher);

Type pattern "@...@" is not supported by TextMatcher.

Is there a way to force JSON matcher ?

@norberttech
Copy link
Member

Hey, your example does not have a valid JSON, because of that PHPMatcher thinks this is just a regular text.
The following line is not correct from a json point of view.

{
        "aws": {
            "key": "laclédepassepartout",
            "secret": "lesecretdupèrefouras",
-            "@...@"
        }
    }

@tentacode
Copy link
Author

Hey, it's not the Json, but the Json Pattern, the Json is ok in the variable $actualJson.

It should work when I read the documentation the @...@ should be supported.

https://github.com/coduo/php-matcher#json-matching-with-unbounded-arrays-and-objects

Json matching with unbounded arrays and objects

<?php

use Coduo\PHPMatcher\PHPMatcher;

$matcher = new PHPMatcher();

$matcher->match(
  '{
    "users":[
      {
        "firstName": "Norbert",
        "lastName": "Orzechowicz",
        "created": "2014-01-01",
        "roles":["ROLE_USER", "ROLE_DEVELOPER"],
        "attributes": {
          "isAdmin": false,
          "dateOfBirth": null,
          "hasEmailVerified": true
        },
        "avatar": {
          "url": "http://avatar-image.com/avatar.png"
        }
      },
      {
        "firstName": "Michał",
        "lastName": "Dąbrowski",
        "created": "2014-01-01",
        "roles":["ROLE_USER", "ROLE_DEVELOPER", "ROLE_ADMIN"],
        "attributes": {
          "isAdmin": true,
          "dateOfBirth": null,
          "hasEmailVerified": true
        },
        "avatar": null
      }
    ]
  }',
  '{
    "users":[
      {
        "firstName": "@string@",
        "lastName": "@string@",
        "created": "@string@.isDateTime()",
        "roles": [
            "ROLE_USER",
            "@...@"
        ],
        "attributes": {
          "isAdmin": @boolean@,
          "@*@": "@*@"
        },
        "avatar": "@json@.match({\"url\":\"@string@.isUrl()\"})"
      }
      ,
      @...@
    ]
  }'
);

@domis86
Copy link
Contributor

domis86 commented Jan 4, 2023

@tentacode can you try like this? :

$expectedJsonMatcher = <<<JSON
    {
        "aws": {
            "key": "laclédepassepartout",
            "secret": "lesecretdupèrefouras",
            "@*@": "@*@"
        }
    }
    JSON;

@norberttech
Copy link
Member

@domis86 is right here, you should use "@*@" if you just want wildcard, @...@ was made for arrays, not properties.

Example

There is also no such thing as "Json Pattern", in order to match value against pattern, json matcher must turn both of them into arrays, it's impossible to achieve that when pattern is not even a JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants