Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
caa8f10
add phpunit
aaemnnosttv Jul 5, 2017
124b5a9
add RecursiveDataStructureTraverser + tests
aaemnnosttv Jul 5, 2017
195d100
add initial pluck + patch to meta commands
aaemnnosttv Jul 5, 2017
b71936b
remove concept of a delimiter, convert to array of keys
aaemnnosttv Jul 9, 2017
b4073ea
add assertion about return code for a failed pluck
aaemnnosttv Jul 9, 2017
2b011e9
rename a few scenarios
aaemnnosttv Jul 9, 2017
670a7a4
update out format options for pluck
aaemnnosttv Jul 9, 2017
2fcf1ed
pass key path to traverser as array in pluck cmd
aaemnnosttv Jul 9, 2017
3da5ecc
add another scenario for testing pluck failure
aaemnnosttv Jul 9, 2017
5acaf20
update patch command and scenarios
aaemnnosttv Jul 9, 2017
b178f7b
reword command descriptions
aaemnnosttv Jul 9, 2017
05ca479
replace patch mode flag with action arg
aaemnnosttv Jul 15, 2017
6a9229b
add support for patch delete
aaemnnosttv Jul 15, 2017
ce24ce0
add insert method to traverser
aaemnnosttv Jul 16, 2017
f2c2e3f
remove unnecessary casts
aaemnnosttv Jul 16, 2017
5ebce33
add patch insert
aaemnnosttv Jul 16, 2017
d293990
Revert "remove unnecessary casts"
aaemnnosttv Jul 16, 2017
0739704
add cast in delete for consistency
aaemnnosttv Jul 16, 2017
dc488c7
add initial support for pluck/patch with int keys
aaemnnosttv Jul 16, 2017
a7c6545
fix schema URL in phpunit config
aaemnnosttv Jul 31, 2017
67c7476
organize traverser classes within Entity namespace
aaemnnosttv Jul 31, 2017
03930d3
change base exception for NonExistentKeyException
aaemnnosttv Jul 31, 2017
96da024
update exception thrown in traverser’s create_key
aaemnnosttv Jul 31, 2017
868f0c3
docblocks and var name changes
aaemnnosttv Jul 31, 2017
057a930
remove vestigial delimiter property
aaemnnosttv Jul 31, 2017
de2d604
move key to constructor parameter
aaemnnosttv Jul 31, 2017
2ab55f4
improve exception message for a missing key
aaemnnosttv Jul 31, 2017
46a9e56
simplify traverse_to method
aaemnnosttv Jul 31, 2017
4b89ef9
update quote used in behat test after change
aaemnnosttv Jul 31, 2017
5691612
break long line for readability
aaemnnosttv Jul 31, 2017
10f232e
update patch with real stdin detection!
aaemnnosttv Jul 31, 2017
7e6f0b2
remove entity utils import
aaemnnosttv Aug 1, 2017
ecbc037
Merge branch 'master' into pr/pluck-patch-subcommands
danielbachhuber Aug 3, 2017
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"require": {},
"require-dev": {
"behat/behat": "~2.5",
"wp-cli/wp-cli": "*"
"wp-cli/wp-cli": "*",
"phpunit/phpunit": "^4.8"
},
"extra": {
"branch-alias": {
Expand Down
270 changes: 270 additions & 0 deletions features/post-meta.feature
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,273 @@ Feature: Manage post custom fields
"""
My\New\Meta
"""

@pluck
Scenario: Nested values can be retrieved.
Given a WP install
And an input.json file:
"""
{
"foo": "bar"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I run `wp post meta pluck 1 meta-key foo`
Then STDOUT should be:
"""
bar
"""

@pluck @pluck-deep
Scenario: A nested value can be retrieved at any depth.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": {
"baz": "some value"
}
},
"foo.com": {
"visitors": 999
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I run `wp post meta pluck 1 meta-key foo bar baz`
Then STDOUT should be:
"""
some value
"""

When I run `wp post meta pluck 1 meta-key foo.com visitors`
Then STDOUT should be:
"""
999
"""

@pluck @pluck-fail
Scenario: Attempting to pluck a non-existent nested value fails.
Given a WP install
And I run `wp post meta set 1 meta-key '{ "key": "value" }' --format=json`

When I run `wp post meta pluck 1 meta-key key`
Then STDOUT should be:
"""
value
"""

When I try `wp post meta pluck 1 meta-key foo`
Then STDOUT should be empty
Copy link
Member

Choose a reason for hiding this comment

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

We should set an explicit check for return code here too:

And the return code should be 1

And the return code should be 1

@pluck @pluck-fail
Scenario: Attempting to pluck from a primitive value fails.
Given a WP install
And I run `wp post meta set 1 meta-key simple-value`

When I try `wp post meta pluck 1 meta-key foo`
Then STDOUT should be empty
And the return code should be 1

@pluck @pluck-numeric
Scenario: A nested value can be retrieved from an integer key.
Given a WP install
And I run `wp post meta set 1 meta-key '[ "foo", "bar" ]' --format=json`

When I run `wp post meta pluck 1 meta-key 0`
Then STDOUT should be:
"""
foo
"""

@patch @patch-update @patch-arg
Scenario: Nested values can be changed.
Given a WP install
And an input.json file:
"""
{
"foo": "bar"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I run `wp post meta patch update 1 meta-key foo baz`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""

When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": "baz"
}
"""

@patch @patch-update @patch-stdin
Scenario: Nested values can be set with a value from STDIN.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz"
},
"bar": "bad"
}
"""
And a patch file:
"""
new value
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I run `wp post meta patch update 1 meta-key foo bar < patch`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""

When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": {
"bar": "new value"
},
"bar": "bad"
}
"""

@patch @patch-update @patch-fail
Scenario: Attempting to update a nested value fails if a parent's key does not exist.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz"
},
"bar": "bad"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I try `wp post meta patch update 1 meta-key foo not-a-key new-value`
Then STDOUT should be empty
And STDERR should contain:
"""
No data exists for key "not-a-key"
"""
And the return code should be 1

@patch @patch-delete
Scenario: A key can be deleted from a nested value.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz",
"abe": "lincoln"
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I run `wp post meta patch delete 1 meta-key foo bar`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""

When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": {
"abe": "lincoln"
}
}
"""

@patch @patch-fail @patch-delete @patch-delete-fail
Scenario: A key cannot be deleted from a nested value from a non-existent key.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz"
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`

When I try `wp post meta patch delete 1 meta-key foo not-a-key`
Then STDOUT should be empty
And STDERR should contain:
"""
No data exists for key "not-a-key"
"""
And the return code should be 1

@patch @patch-insert
Scenario: A new key can be inserted into a nested value.
Given a WP install
And I run `wp post meta set 1 meta-key '{}' --format=json`

When I run `wp post meta patch insert 1 meta-key foo bar`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""

When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": "bar"
}
"""

@patch @patch-fail @patch-insert @patch-insert-fail
Scenario: A new key cannot be inserted into a non-nested value.
Given a WP install
And I run `wp post meta set 1 meta-key 'a simple value'`

When I try `wp post meta patch insert 1 meta-key foo bar`
Then STDOUT should be empty
And STDERR should contain:
"""
Cannot create key "foo"
"""
And the return code should be 1

When I run `wp post meta get 1 meta-key`
Then STDOUT should be:
"""
a simple value
"""

@patch @patch-numeric
Scenario: A nested value can be updated using an integer key.
Given a WP install
And I run `wp post meta set 1 meta-key '[ "foo", "bar" ]' --format=json`

When I run `wp post meta patch update 1 meta-key 0 new`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""

When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
[ "new", "bar" ]
"""
Copy link
Member

Choose a reason for hiding this comment

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

I think there's three use/test cases missing:

  1. What happens when you patch insert an already existing value? (Is there a warning? Does it fail?)
  2. What happens when you patch update a non-existing key? (This should produce an error.)
  3. What happens when you patch update an existing key to the same value? (You seem to have an adapted success message in the code below.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What happens when you patch insert an already existing value?

Will add.

What happens when you patch update a non-existing key? (This should produce an error.)

I believe this is covered already. See the @patch-fail tags.

What happens when you patch update an existing key to the same value? (You seem to have an adapted success message in the code below.)

Will add. (Should be a notice that value did not change.)

21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
Loading