We can probably all agree that automated tests are great, but sometimes we put off writing them because the initial first step of getting everything working correctly can be really difficult.
Good news, Sugar developers! Writing PHPUnit tests for your customizations just got easier in Sugar Fall '18! Our engineering team has created a test runner specifically for your custom PHPUnit tests.
If you do not have access to the SugarCRM provided unit test suite, requesting access is easy, all you need to do is send an e-mail to email@example.com with your request. For more information, see this post.
The first thing you'll need to do is add the SugarCRM provided unit test suite to your Sugar directory. Be sure to select the branch in the unit test suite that matches the version of your Sugar instance (needs to be 8.2 or higher). Follow the instructions in the branch's readme to add the tests to your Sugar directory, install the necessary dependencies, and update the file permissions.
Now you're ready to write your tests! Add your new PHPUnit test classes to the custom/tests/unit-php directory. You may need to create it yourself if it doesn't already exist.
For example, create a new file named CustomLogicHookTest.php and place it in the custom/tests/unit-php directory. Paste the following in to the new file:
* @coversDefaultClass \CustomLogicHook
class CustomLogicHookTest extends TestCase
* @covers ::aFunctionInsideTheCustomLogicHook
public function testCustomHook()
// Wow! This is a horrible test.
// You would never do something like this just to increase your test
// coverage, right?
In a shell, navigate to the Sugar provided unit tests directory by doing something like the following:
Then you can run the tests by executing
php ../../vendor/bin/phpunit --testsuite custom
or by using gulp (gulp installation instructions are available here)
gulp test:unit:php --suite custom
The above test is extremely simple and doesn't test anything. If you'd like to see an example of a larger test suite that tests Sugar customizations, check out Professor M's School for Gifted Coders. We've recently updated our PHPUnit test suite so we can use the new test runner. Check out the pull request associated with this change for the details.