Skip to content
Snippets Groups Projects
Commit e9d2b317 authored by Alberto Paderno's avatar Alberto Paderno
Browse files

Issue #2985635: Move PHPUnit Example into Testing Example

parent 433f7b4c
No related branches found
No related tags found
1 merge request!87Issue #2985635: Move PHPUnit Example into Testing Example
Pipeline #304406 passed
Showing
with 56 additions and 90 deletions
......@@ -138,7 +138,6 @@ function _examples_toolbar_routes() {
'node_type_example' => 'config_node_type_example.description',
'page_example' => 'page_example.description',
'pager_example' => 'pager_example.page',
'phpunit_example' => 'phpunit_example.description',
'plugin_type_example' => 'plugin_type_example.description',
'queue_example' => 'queue_example.form',
'render_example' => 'render_example.description',
......
name: PHPUnit Example
type: module
description: Demonstrates how to use PHPUnit-based tests.
package: Example modules
core_version_requirement: ^10.3 || ^11.0
dependencies:
- drupal:node
- examples:examples
phpunit_example.description:
title: PHPUnit Example
route_name: phpunit_example.description
<?php
/**
* @file
* Module file for phpunit_example.
*/
/**
* @defgroup phpunit_example Example: PHPUnit
* @ingroup examples
* @{
* This example demonstrates PHPUnit for Drupal 8 unit testing.
*/
/**
* @} End of "defgroup phpunit_example".
*/
# phpunit_example only has one route. It is to a page explaining
# the module.
phpunit_example.description:
path: '/examples/phpunit-example'
defaults:
_controller: '\Drupal\phpunit_example\Controller\PHPUnitExampleController::description'
requirements:
_permission: 'access content'
<?php
namespace Drupal\phpunit_example\Controller;
use Drupal\examples\Utility\DescriptionTemplateTrait;
/**
* Controller for PHPUnit description page.
*
* This class uses the DescriptionTemplateTrait to display text we put in the
* templates/description.html.twig file. We render out the text via its
* description() method, and set up our routing to point to
* PHPUnitExampleController::description().
*/
class PHPUnitExampleController {
use DescriptionTemplateTrait;
/**
* {@inheritdoc}
*/
protected function getModuleName() {
return 'phpunit_example';
}
}
<?php
namespace Drupal\phpunit_example;
namespace Drupal\testing_example;
/**
* A class with features to show how to do unit testing.
......
......@@ -50,6 +50,24 @@ protected function getModuleName() {
return 'testing_example';
}
/**
* Generate a render array for the Simpletest description.
*
* @return array
* A render array.
*/
public function phpUnitDescription() {
$template_file = $this->moduleExtensionList->getPath('testing_example') . '/templates/phpunit.description.html.twig';
$build = [
'description' => [
'#type' => 'inline_template',
'#template' => file_get_contents($template_file),
],
];
return $build;
}
/**
* Generate a render array for the Simpletest description.
*
......
<?php
namespace Drupal\phpunit_example;
namespace Drupal\testing_example;
/**
* An interface to objects that provide displayable information.
......@@ -11,7 +11,7 @@
* This interface as representing objects that can return some sort of display
* information, such as .info.yml file.
*
* @ingroup phpunit_example
* @ingroup testing_example
*/
interface DisplayInfoInterface {
......
<?php
namespace Drupal\phpunit_example;
namespace Drupal\testing_example;
/**
* An example class to demonstrate unit testing.
......@@ -11,9 +11,9 @@
*
* But it never will, because it's just an example class.
*
* Part of the PHPUnit Example module.
* Part of the Testing Example module.
*
* @ingroup phpunit_example
* @ingroup testing_example
*/
class DisplayManager {
......
<?php
namespace Drupal\phpunit_example;
namespace Drupal\testing_example;
/**
* A class with features to show how to do unit testing.
......@@ -12,7 +12,7 @@
* We do this so we're concentrating on the testing instead of the
* code being tested.
*
* @ingroup phpunit_example
* @ingroup testing_example
*/
class ProtectedPrivates {
......
......@@ -2,6 +2,9 @@ testing_example.description:
title: Testing Example
route_name: testing_example.description
expanded: TRUE
testing_example.phpunit_example.description:
title: PHPUnit Example
route_name: testing_example.phpunit_example.description
testing_example.simpletest_description:
title: Simpletest information
route_name: testing_example.simpletest_description
......
......@@ -13,6 +13,13 @@ testing_example.simpletest_description:
requirements:
_permission: 'access content'
testing_example.phpunit_description:
path: '/examples/testing-example/phpunit'
defaults:
_controller: '\Drupal\testing_example\Controller\TestingExampleController::phpUnitDescription'
requirements:
_permission: 'access content'
# This route displays a sum of two numbers in terms of how many hands are
# required to count it.
testing_example.sum_in_hands:
......
<?php
namespace Drupal\Tests\phpunit_example\Functional;
namespace Drupal\Tests\testing_example\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
......@@ -11,9 +11,9 @@
* Note that this is _not_ a PHPUnit-based test. It's a functional
* test of whether this module can be enabled properly.
*
* @ingroup phpunit_example
* @ingroup testing_example
*
* @group phpunit_example
* @group testing_example
* @group examples
*/
class PHPUnitExampleMenuTest extends BrowserTestBase {
......@@ -28,7 +28,7 @@ class PHPUnitExampleMenuTest extends BrowserTestBase {
*
* @var array
*/
protected static $modules = ['phpunit_example'];
protected static $modules = ['testing_example'];
/**
* The installation profile to use with this test.
......@@ -47,7 +47,7 @@ public function testLinksAndPages() {
$this->drupalLogin($this->createUser(['access content']));
$assert = $this->assertSession();
$links = [
'' => Url::fromRoute('phpunit_example.description'),
'' => Url::fromRoute('testing_example.phpunit_description'),
];
// Go to the page and see if the link appears on it.
foreach ($links as $page => $path) {
......
<?php
namespace Drupal\Tests\phpunit_example\Unit;
namespace Drupal\Tests\testing_example\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\phpunit_example\AddClass;
use Drupal\testing_example\AddClass;
/**
* AddClass units tests.
......
<?php
namespace Drupal\Tests\phpunit_example\Unit;
namespace Drupal\Tests\testing_example\Unit;
use Drupal\phpunit_example\DisplayInfoInterface;
use Drupal\phpunit_example\DisplayManager;
use Drupal\testing_example\DisplayInfoInterface;
use Drupal\testing_example\DisplayManager;
use Drupal\Tests\UnitTestCase;
/**
* DisplayManager unit test with doubles.
*
* @ingroup phpunit_example
* @ingroup testing_example
*
* @group phpunit_example
* @group testing_example
* @group examples
*/
class DisplayManagerTest extends UnitTestCase {
......
<?php
namespace Drupal\Tests\phpunit_example\Unit;
namespace Drupal\Tests\test_example\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\phpunit_example\ProtectedPrivates;
use Drupal\testing_example\ProtectedPrivates;
use Drupal\Tests\phpunit_example\Unit\Subclasses\ProtectedPrivatesSubclass;
use Drupal\Tests\testing_example\Unit\Subclasses\ProtectedPrivatesSubclass;
/**
* ProtectedPrivates unit testing of restricted methods.
......@@ -33,9 +33,9 @@
* test, and just make a public accessor method that way. So we
* demonstrate that here in testProtectedAdd().
*
* @ingroup phpunit_example
* @ingroup testing_example
*
* @group phpunit_example
* @group testing_example
* @group examples
*/
class ProtectedPrivatesTest extends UnitTestCase {
......
<?php
namespace Drupal\Tests\phpunit_example\Unit\Subclasses;
namespace Drupal\Tests\testing_example\Unit\Subclasses;
use Drupal\phpunit_example\ProtectedPrivates;
use Drupal\testing_example\ProtectedPrivates;
/**
* A class for testing ProtectedPrivate::protectedAdd().
......@@ -13,7 +13,7 @@
*
* This subclass allows us to get access to the protected method.
*
* @ingroup phpunit_example
* @ingroup testing_example
*/
class ProtectedPrivatesSubclass extends ProtectedPrivates {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment