Underscores Theme Unit Testing – Catching Untitled Posts
Underscores Theme Unit Testing – Catching Untitled Posts
Underscores Theme Unit Testing – Catching Untitled Posts
Coverage in integration tests
In your test, you can create a post, start output buffering with ob_start(), output post content as usual, get output buffer into your variable with ob_end_clean(). Now your can process real post output string and compare it with the expected value.
If you’re on Windows, open wp-tests-config.php and change the ABSPATH with absolute path to your TEMP folder. For example: define( ‘ABSPATH’, ‘C:\Users\{User}\AppData\Local\Temp/wordpress/’ ); I changed this and phpunit worked! This code is on line 7 in %TEMP%/wordpress-tests-lib/wp-tests-config.php
You just need to define the MZMBO_UnitTestCase class as abstract: abstract class MZMBO_UnitTestCase extends WP_UnitTestCase { public function el($message){ file_put_contents(‘./log_’.date(“j.n.Y”).’.log’, $message, FILE_APPEND); } }
The best solution I’ve found is to make a symlink from the plugin directory to the actual location of the /wordpress-tests-lib/. At least in the case of VS Code, the symlink seems to help it “find” the files and incorporate them into autocomplete etc. I’m still having bugs but this is the best I could … Read more
One way could be to use classes and objects. It’s more efficient considering reusability, collective work and more. I don’t think it’s bad to use global variables if you know what you’re doing. So I would recommand you to use OOP (object oriented programming) especially with inheritance and polymorphism.
If I undertsand correctly, what you are searching for is a good library for creating stubs and mocks. If you use PHPUnit, it has a features for that. See https://phpunit.de/manual/current/en/test-doubles.html#test-doubles For example, let’s assume you have a class like this: namespace MyApp; class MyCustomUser { public function __construct(\WP_User $user) { $this->user = $user; } public … Read more
Take a look at the WordPress core setup with PHPUnit: https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/ Using this process rather than rolling your own allows you to leverage the boostrapping (and other customization) specific for WordPress. From the Codex: When phpunit is invoked, the test suite runs a script that sets up a default installation of WordPress, with a configuration … Read more
What you have discovered is that WordPress is loaded—and all of the actions are called—before the tests run. It is possible to hook into an action when WordPress is loaded, but it has to be done from your PHPUnit bootstrap file instead of from the testcase class. I don’t know what your bootstrap file looks … Read more