class ViewportResolverTest in Viewport 8
@coversDefaultClass \Drupal\viewport\ViewportResolver @group viewport
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\viewport\Unit\ViewportResolverTest
Expanded class hierarchy of ViewportResolverTest
File
- tests/
src/ Unit/ ViewportResolverTest.php, line 12
Namespace
Drupal\Tests\viewport\UnitView source
class ViewportResolverTest extends UnitTestCase {
/**
* The tested ViewportResolver class.
*
* @var \Drupal\viewport\ViewportResolver
*/
protected $viewportResolver;
/**
* The mocked path matcher.
*
* @var \Drupal\Core\Path\PathMatcherInterface
*/
protected $pathMatcher;
/**
* The mocked current path stack.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPathStack;
/**
* The mocked config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Instantiate mocks and stubs.
$configuredPaths = "<front>\n/node/*\n/present-path";
$this->configFactory = $this
->getConfigFactoryStub(array(
'viewport.settings' => array(
'selected_pages' => $configuredPaths,
'width' => '1600',
'height' => 'device-height',
'initial_scale' => '2.0',
'minimum_scale' => '1.0',
'maximum_scale' => '5.0',
'user_scalable' => TRUE,
),
));
$requestStack = $this
->getMock('\\Symfony\\Component\\HttpFoundation\\RequestStack');
$this->currentPathStack = $this
->getMock('\\Drupal\\Core\\Path\\CurrentPathStack', array(), [
$requestStack,
]);
$this->currentPathStack
->expects($this
->any())
->method('getPath')
->will($this
->onConsecutiveCalls('/present-path', '/not-present-path'));
// Mock PathMatcher class directly (instead of interface) to try and add
// some coverage of '<front>'.
$this->pathMatcher = $this
->getMock('\\Drupal\\Core\\Path\\PathMatcher', array(
'getFrontPagePath',
), array(), '', FALSE);
$this->pathMatcher
->expects($this
->any())
->method('getFrontPagePath')
->will($this
->returnValue('/frontpage-path'));
$this->viewportResolver = new ViewportResolver($this->pathMatcher, $this->currentPathStack, $this->configFactory);
}
/**
* Tests isPathSelected method solves filters out properly configured paths.
*/
public function testIsPathSelected() {
$this
->assertTrue($this->viewportResolver
->isPathSelected('/present-path'), 'Check for configured path.');
$this
->assertFalse($this->viewportResolver
->isPathSelected('/not-present-path'), 'Check for non-configured path.');
}
/**
* Tests isPathSelected method solves paths passed as uppercase.
*/
public function testIsPathSelectedNormalisesPathString() {
$this
->assertTrue($this->viewportResolver
->isPathSelected('/pReSent-PATH'), 'Check for configured path.');
}
/**
* Tests isPathSelected method uses current path if none provided.
*/
public function testIsPathSelectedWithoutPathProvided() {
$this
->assertTrue($this->viewportResolver
->isPathSelected(), 'Check configured path as current path.');
$this
->assertFalse($this->viewportResolver
->isPathSelected(), 'Check non-configured path as current path.');
}
/**
* Tests isPathSelected matches frontpage paths not explicitly configured.
*
* Code smell?: This test is not really reliable. As it stands it relies on
* the "<front>" translation to the current frontpage path being done by the
* PathMatcher class. The mocks in this test overwrite the getFrontPagePath
* method to return the one desired, but that wouldn't ensure correct behavior
* if the current path stack service in Drupal changed to a different class.
*/
public function testIsPathSelectedDetectsFrontPage() {
$this
->assertTrue($this->viewportResolver
->isPathSelected('/frontpage-path'), 'Check non-configured path as front page path.');
}
/**
* Tests generateViewportTagArray() generates the correct array structure.
*/
public function testGenerateViewportTagArray() {
// Check the tag array returned is exactly as expected, including the order
// in which 'content' properties are set, although there's no real need to
// be so strict.
$expected = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => "width=1600, height=device-height, initial-scale=2.0, minimum-scale=1.0, maximum-scale=5.0, user-scalable=yes",
),
);
$this
->assertArrayEquals($expected, $this->viewportResolver
->generateViewportTagArray());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
ViewportResolverTest:: |
protected | property | The mocked config factory. | |
ViewportResolverTest:: |
protected | property | The mocked current path stack. | |
ViewportResolverTest:: |
protected | property | The mocked path matcher. | |
ViewportResolverTest:: |
protected | property | The tested ViewportResolver class. | |
ViewportResolverTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ViewportResolverTest:: |
public | function | Tests generateViewportTagArray() generates the correct array structure. | |
ViewportResolverTest:: |
public | function | Tests isPathSelected method solves filters out properly configured paths. | |
ViewportResolverTest:: |
public | function | Tests isPathSelected matches frontpage paths not explicitly configured. | |
ViewportResolverTest:: |
public | function | Tests isPathSelected method solves paths passed as uppercase. | |
ViewportResolverTest:: |
public | function | Tests isPathSelected method uses current path if none provided. |