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

2.3 - Add repeat expander feature #109

Merged
merged 3 commits into from
Oct 15, 2017
Merged

Conversation

raskyer
Copy link

@raskyer raskyer commented Sep 26, 2017

Add "repeat" requested feature in:
#35

And also in:
#21

Feel free to comment, give advice.
I want to merge on 2.3 because I want to stick with PHP 5.6 not 7.

Copy link
Contributor

@eliecharra eliecharra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 For PHP 5.x compat, but this is a new feature and should also be in latest master version for php 7.x

README.md Outdated
@@ -84,6 +84,7 @@ $matcher->getError(); // returns null or error message
* ``oneOf(...$expanders)`` - example usage ``"@string@.oneOf(contains('foo'), contains('bar'), contains('baz'))"``
* ``matchRegex($regex)`` - example usage ``"@string@.matchRegex('/^lorem.+/')"``
* ``optional()`` - work's only with ``ArrayMatcher``, ``JsonMatcher`` and ``XmlMatcher``
* ``repeat($pattern, $isStrict = true)`` - example usage ``'@array@.repeat({"name" => "foe"})'`` or ``"@array@.repeat('@string@')"``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{"name" => "foe"}

I think we should either ["name" => "foe"] or {"name": "foe"} but not a mix of both there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, a little mistake here. Of course, it's JSON. I will fix that ASAP

{
$jsonPattern = '{"name": "@string@", "activated": "@boolean@"}';

$jsonTest = array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use "new" PHP short array syntax [] which is less verbose

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes probably, but the whole project seems to use the older array() syntax. In order to stick with the project's coding convention I'd rather not change that for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On master branch we are using [] - 2.3 branch was only created for 5.6 support however I'm not planning to add more new features here without adding them to the master branch first

* @param Matcher $matcher
* @return bool
*/
private function matchJson(array $values, Matcher $matcher)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe allow this method to be recursive to match nested objects ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
"offers": "@array@.repeat({\"subscription\": \"@array@.repeat(\"date\": \"@string@.isDateTime()\")\"})"
}

This case will work with actual implementation ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case should work with the nested array.repeat syntax 'cause I just make a call to php-matcher factory. But you're right, maybe allowing nested objects would be more clever.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not yet supported, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'm sorry, didn't have the time to do it

public function __construct($pattern, $isStrict = true)
{
if (!is_string($pattern)) {
throw new \InvalidArgumentException("Repeat pattern must be a string.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception is not covered by tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix that ASAP.

private function matchScalar(array $values, Matcher $matcher)
{
foreach ($values as $index => $value) {
$match = $matcher->match($value, $this->pattern);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only match value, is following pattern failing ?

@array@.repeat({"foo": "@string@"})

[
  {
    "foo": "bar" 
  },
  {
    "test": "bar"
  }
]
``

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand what you're talking about here ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just ignore this comment

@norberttech
Copy link
Member

I want to merge on 2.3 because I want to stick with PHP 5.6 not 7.

Sure, but first I would like to see that feature on master branch and then into 2.3 or even 2.4

@eliecharra
Copy link
Contributor

Sure, but first I would like to see that feature on master branch and then into 2.3 or even 2.4

Totally agreed with that

@raskyer
Copy link
Author

raskyer commented Sep 28, 2017

Fix readme and add cover for a new exception in test.

Also create #110 for 3.0 repeat.

@norberttech norberttech merged commit 7b2cb77 into coduo:2.3 Oct 15, 2017
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

Successfully merging this pull request may close these issues.

4 participants