Skip to content

Commit

Permalink
WV-3992: Added test for PathautoGeneratorTrait. (#7)
Browse files Browse the repository at this point in the history
* WV-3992: Added test for PathautoGeneratorTrait.

* WV-3992: Added pathauto to require-dev.

* WV-3992: Added drupal.org repo in composer.json

* WV-3992: Corrected test

* WV-3992: Removed wrong located trait.

* WV-3992: Correctly autoload pathouto for test.
  • Loading branch information
mostepaniukvm authored Aug 28, 2020
1 parent 03f2152 commit 770fc19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
},
"require-dev": {
"phpunit/phpunit": "~4.8",
"drupal/pathauto": "^1.6",
"drupal/coder": "^8.2"
},
"repositories": {
"0": {
"type": "composer",
"url": "https://packages.drupal.org/8"
}
},
"scripts": {
"cs": "phpcs --colors",
"cbf": "phpcbf",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace drunomics\ServiceUtils\Core\Path;
namespace drunomics\ServiceUtils\pathauto;

use Drupal\pathauto\PathautoGeneratorInterface;

Expand Down
1 change: 1 addition & 0 deletions tests/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
$classLoader = new ClassLoader();
$classLoader->addPsr4("Drupal\\file\\", __DIR__ . '/../vendor/drupal/core/modules/file/src');
$classLoader->addPsr4("Drupal\\user\\", __DIR__ . '/../vendor/drupal/core/modules/user/src');
$classLoader->addPsr4("Drupal\\pathauto\\", __DIR__ . '/../vendor/drupal/pathauto/src');
$classLoader->register();
70 changes: 70 additions & 0 deletions tests/src/pathauto/PathautoGeneratorTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace drunomics\ServiceUtils\Tests\pathauto;

use drunomics\ServiceUtils\pathauto\PathautoGeneratorTrait;
use Drupal\Core\DependencyInjection\Container;
use Drupal\pathauto\PathautoGeneratorInterface;

/**
* @coversDefaultClass \drunomics\ServiceUtils\pathauto\PathautoGeneratorTrait
* @group ServiceUtils
*/
class PathautoGeneratorTraitTest extends \PHPUnit_Framework_TestCase {

use PathautoGeneratorTrait;

/**
* The id of the trait's service.
*
* @var string
*/
protected $serviceId = 'pathauto.generator';

/**
* @covers ::getPathautoGenerator
*/
public function testGetter() {
// Verify the container is used once and the right service is returned.
$service = $this->mockContainerWithFakeService(['calls' => 1]);
$this->assertsame($service, $this->getPathautoGenerator());
// Multiple calls should fetch the service from the container only once.
$this->getPathautoGenerator();
}

/**
* @covers ::setPathautoGenerator
*/
public function testSetter() {
// Verify the set service is returned.
$this->mockContainerWithFakeService(['calls' => 0]);
$service = $this->prophesize()
->willImplement(PathautoGeneratorInterface::class)
->reveal();
$this->setPathautoGenerator($service);
$this->assertsame($service, $this->getPathautoGenerator());
}

/**
* Helper to mock the container with a stub service.
*
* @param int[] $options
* An array with the following keys:
* - calls: The number of calls to get the service the mocked container
* expects.
*
* @return object
* The fake service returned by the container.
*/
protected function mockContainerWithFakeService(array $options) {
$service = new \Stdclass();
$container = $this->prophesize(Container::class);
$prophecy = $container->get($this->serviceId);
/** @var \Prophecy\Prophecy\MethodProphecy $prophecy */
$prophecy->shouldBeCalledTimes($options['calls']);
$prophecy->willReturn($service);
\Drupal::setContainer($container->reveal());
return $service;
}

}

0 comments on commit 770fc19

Please sign in to comment.