-
Notifications
You must be signed in to change notification settings - Fork 0
Add PHPUnit testing framework #181
base: trunk
Are you sure you want to change the base?
Conversation
* @return mixed The return value of the method. | ||
*/ | ||
|
||
public function invoke_method( &$object, $method_name, array $parameters = array() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would this work if we wanted to test a static method on a class that wasn't instantiated? Would there be a recommended other method to use, or a way to override this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still on that learning curve, and so far I haven't found a way since we need to pass an object to instantiate the ReflectionClass. Those static methods would ideally be public, at least if we want to test them.
This brings up a greater philosophical question - ie should we even be testing private or protected methods? From what I understand, the answer is generally no. If that's the case then we're going to have a difficult time unit testing the cli tool, and maybe should look into command level testing, though I feel that's not helpful for finding issues in the individual methods within a class/command.
Does this mean we should/could be using public methods instead? (Rhetorical, kind of, opinions welcomed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, if it's something we'd like tested, I'm 100% in favor of just making it public.
* | ||
* @return mixed The return value of the method. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably don't need this single blank line.
*/ | ||
public function test_get_request_url_adds_prefix_with_integer() { | ||
$expected = 'https://public-api.wordpress.com/rest/v1.1/sites/123'; | ||
$actual = $this->invoke_method( $this->wpcom_api_helper, 'get_request_url', array( 'sites/123' ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this differ in practice from just
$actual = $this->invoke_method( $this->wpcom_api_helper, 'get_request_url', array( 'sites/123' ) ); | |
$actual = $this->wpcom_api_helper->get_request_url( 'sites/123' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would give you an error since that method is private. The invoke method changes that with the $method->setAccessible( true );
in the ReflectionClass.
Almost all of the methods in our commands, and most of the helpers, are protected or private. To make testing of these easier I decided to add the invoke_method
as part of the extended TestCase to avoid having to re-write the method every time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! That makes sense.
Purpose of this PR:
Implement the PHPUnit testing framework on the Team 51 CLI tool to support development.
Approach
This implementation focuses on unit testing. While Symfony includes command level testing, a finer grain approach would produce more valuable outcomes for developers modifying existing and/or creating new commands.
Symfony includes a built-in implementation and integration with PHPUnit to scaffold in the required libraries which was used for this PR.
The intent is to keep testing optional, and as such the tests won't be part of any commit or merge level checks. The tests can be run manually as required.
Changes proposed in this Pull Request:
symfony/test-pack
package through composer.How to Test:
team51-cli
folder fetch updates and switch to this branch.composer install
to install the new packages.vendor/bin/phpunit --testsuite tests --testdox
.Adding new tests:
tests/commands/<command-class-name>
, and Helper tests intests/helpers/<helper-class-name>
.test-
to be run automatically via the testsuite.TestCase
or the extendedT51TestCase
if needed.tests/includes
folder.