Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Add command for running project-specitic PHPUnit tests #290

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions docs/extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ The critical parts of the example are:

Everything else (class names, function names) is flexible, and each public function inside of the class will show up as an available `dktl` command.

## Custom Tests
- Add a `phpunit.xml` file to `src/modules`, then add the paths of your tests to the Custom Test Suite directory section.
- Run the tests with `dktl custom:test-phpunit --testsuite "Custom Test Suite"`

_phpunit.xml_ sample

```php
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
bootstrap="/var/www/vendor/weitzman/drupal-test-traits/src/bootstrap.php"
colors="true"
stopOnFailure="false"
stopOnError="false"
verbose="true">

<testsuites>
<testsuite name="Custom Test Suite">
<directory>my_module/tests/src/ExistingSite/TestName.php</directory>
</testsuite>
</testsuites>

<php>
<!-- These variables may alternatively be set as environment variables. -->
<!-- E.g., `DRUPAL_VERSION=V8 ./vendor/bin/phpunit` -->
<env name="DRUPAL_VERSION" value="V8"/>
<env name="DTT_BASE_URL" value="http://web"/>
<env name="SIMPLETEST_BASE_URL" value="http://web"/>
<env name="SIMPLETEST_DB" value="mysql://drupal:123@db/drupal"/>
</php>
</phpunit>
```

## Advanced configuration

Expand Down
68 changes: 68 additions & 0 deletions src/Command/CustomCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace DkanTools\Command;

use DkanTools\Util\Util;

/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class CustomCommands extends \Robo\Tasks
{
/**
* Run Custom Cypress Tests.
*/
public function customTestCypress(array $args)
{
$this->taskExec("npm install cypress")
->dir("docroot/modules/custom")
->run();

$cypress = $this->taskExec('CYPRESS_baseUrl="http://$DKTL_PROXY_DOMAIN" npx cypress run')
->dir("docroot/modules/custom");

foreach ($args as $arg) {
$cypress->arg($arg);
}

return $cypress->run();
}

/**
* Run Custom PhpUnit Tests. Additional phpunit CLI options can be passed.
*
* @see https://phpunit.de/manual/6.5/en/textui.html#textui.clioptions
* @param array $args Arguments to append to phpunit command.
*/
public function customTestPhpunit(array $args)
{
$proj_dir = Util::getProjectDirectory();
$phpunit_executable = $this->getPhpUnitExecutable();

$phpunitExec = $this->taskExec($phpunit_executable)
->option('testsuite', 'Custom Test Suite')
->dir("{$proj_dir}/docroot/modules/custom");

foreach ($args as $arg) {
$phpunitExec->arg($arg);
}

return $phpunitExec->run();
}

private function getPhpUnitExecutable()
{
$proj_dir = Util::getProjectDirectory();

$phpunit_executable = $phpunit_executable = "{$proj_dir}/vendor/bin/phpunit";

if (!file_exists($phpunit_executable)) {
$this->taskExec("dktl installphpunit")->run();
$phpunit_executable = "phpunit";
}

return $phpunit_executable;
}
}