Skip to content

Commit b2a5913

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components (#11)
1 parent a430f26 commit b2a5913

15 files changed

+484
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.idea
22
/composer.lock
33
/vendor
4+
/auth.json

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
dist: trusty
2+
3+
git:
4+
depth: false
5+
6+
language: php
7+
php:
8+
- '7.1'
9+
- '7.2'
10+
- '7.3'
11+
12+
env:
13+
- TEST_SUITE=static-unit XDEBUG=true
14+
15+
install:
16+
- if [ $TRAVIS_SECURE_ENV_VARS == "true" ]; then composer config http-basic.repo.magento.com ${REPO_USERNAME} ${REPO_PASSWORD}; fi;
17+
- if [ $TRAVIS_SECURE_ENV_VARS == "true" ]; then composer config github-oauth.github.com ${GITHUB_TOKEN}; fi;
18+
- composer config repositories.magento composer https://repo.magento.com/
19+
- composer require "magento/framework:*" --no-update
20+
- composer require "magento/module-store:*" --no-update
21+
- composer require "magento/module-url-rewrite:*" --no-update
22+
- composer update -n --no-suggest
23+
24+
script:
25+
- if [ "$TEST_SUITE" == "static-unit" ]; then ./Test/static/static-travis.sh; fi
26+

Console/Command/ConfigShowDefaultUrlCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CloudComponents\Console\Command;
79

810
use Magento\CloudComponents\Model\UrlFixer;

Console/Command/ConfigShowEntityUrlsCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CloudComponents\Console\Command;
79

810
use Magento\CloudComponents\Model\UrlFixer;
@@ -23,6 +25,7 @@
2325

2426
/**
2527
* Returns list of category or cms-page urls for given stores
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2629
*/
2730
class ConfigShowEntityUrlsCommand extends Command
2831
{

Console/Command/ConfigShowStoreUrlCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CloudComponents\Console\Command;
79

810
use Magento\CloudComponents\Model\UrlFixer;

Model/UrlFixer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\CloudComponents\Model;
79

810
use Magento\Store\Model\Store;
@@ -21,8 +23,7 @@ class UrlFixer
2123
*/
2224
public function run(Store $store, $url): string
2325
{
24-
if (
25-
($store->getForceDisableRewrites() || !$store->getConfig(Store::XML_PATH_USE_REWRITES))
26+
if (($store->getForceDisableRewrites() || !$store->getConfig(Store::XML_PATH_USE_REWRITES))
2627
&& strpos($url, '/magento/') !== false
2728
) {
2829
return preg_replace('|/magento/|', '/', $url, 1);

Test/Unit/Model/UrlFixerTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CloudComponents\Test\Unit\Model;
9+
10+
use Magento\CloudComponents\Model\UrlFixer;
11+
use Magento\Store\Model\Store;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
class UrlFixerTest extends TestCase
19+
{
20+
/**
21+
* @var UrlFixer
22+
*/
23+
private $urlFixer;
24+
25+
/**
26+
* @var MockObject|Store
27+
*/
28+
private $storeMock;
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function setUp()
34+
{
35+
$this->storeMock = $this->createPartialMock(Store::class, ['getForceDisableRewrites', 'getConfig']);
36+
$this->urlFixer = new UrlFixer();
37+
}
38+
39+
/**
40+
* @param bool $rewritesDisabled
41+
* @param bool $useConfigRewrites
42+
* @param string $url
43+
* @param string $expectedUrl
44+
* @dataProvider runDataProvider
45+
*/
46+
public function testRunWithConfigRewrites(
47+
string $url,
48+
string $expectedUrl,
49+
bool $rewritesDisabled = false,
50+
bool $useConfigRewrites = true
51+
) {
52+
$this->storeMock->expects($this->once())
53+
->method('getForceDisableRewrites')
54+
->willReturn($rewritesDisabled);
55+
56+
if (!$rewritesDisabled) {
57+
$this->storeMock->expects($this->once())
58+
->method('getConfig')
59+
->with(Store::XML_PATH_USE_REWRITES)
60+
->willReturn($useConfigRewrites);
61+
} else {
62+
$this->storeMock->expects($this->never())
63+
->method('getConfig');
64+
}
65+
66+
$this->assertEquals($expectedUrl, $this->urlFixer->run($this->storeMock, $url));
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
public function runDataProvider(): array
73+
{
74+
return [
75+
'rewrites enabled, url without "magento" part' => [
76+
'http://example.com/',
77+
'http://example.com/',
78+
],
79+
'rewrites disabled, url without "magento" part' => [
80+
'http://example.com/',
81+
'http://example.com/',
82+
true,
83+
true,
84+
],
85+
'rewrites enabled, url with "magento" part' => [
86+
'http://example.com/magento/',
87+
'http://example.com/magento/',
88+
false,
89+
true,
90+
],
91+
'rewrites disabled in store, url with "magento" part' => [
92+
'http://example.com/magento/',
93+
'http://example.com/',
94+
true,
95+
false,
96+
],
97+
'rewrites disabled in config, url with "magento" part' => [
98+
'http://example.com/magento/',
99+
'http://example.com/',
100+
false,
101+
false,
102+
],
103+
'rewrites disabled, url with multiple "magento" part' => [
104+
'http://example.com/magento/magento/magento/test.html',
105+
'http://example.com/magento/magento/test.html',
106+
true,
107+
false,
108+
],
109+
'rewrites disabled, url with "magento2" part' => [
110+
'http://example.com/magento2/',
111+
'http://example.com/magento2/',
112+
true,
113+
false,
114+
],
115+
'rewrites disabled, with "magento" host' => [
116+
'http://magento.com/magento2/',
117+
'http://magento.com/magento2/',
118+
true,
119+
false,
120+
],
121+
];
122+
}
123+
}

Test/Unit/phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
5+
colors="true"
6+
columns="max"
7+
bootstrap="../../vendor/autoload.php"
8+
beStrictAboutTestsThatDoNotTestAnything="false"
9+
>
10+
<testsuites>
11+
<testsuite name="Unit">
12+
<directory suffix="Test.php">./</directory>
13+
</testsuite>
14+
</testsuites>
15+
16+
<php>
17+
<ini name="date.timezone" value="UTC"/>
18+
</php>
19+
</phpunit>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Test\Sniffs\Directives;
9+
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
13+
/**
14+
* Sniffer to check if the strict_types declaration is included and add it if not.
15+
*
16+
* Class StrictTypesSniff
17+
*/
18+
class StrictTypesSniff implements Sniff
19+
{
20+
/**
21+
* Flag to keep track of whether the file has been fixed.
22+
*
23+
* @var boolean
24+
*/
25+
private $fixed = false;
26+
27+
/**
28+
* @return array
29+
*/
30+
public function register()
31+
{
32+
return [
33+
T_OPEN_TAG
34+
];
35+
}
36+
37+
/**
38+
* @param File $phpcsFile
39+
* @param int $stackPtr
40+
* @return int|void
41+
*/
42+
public function process(File $phpcsFile, $stackPtr)
43+
{
44+
// Get all tokens.
45+
$tokens = $phpcsFile->getTokens();
46+
47+
// Tokens to look for.
48+
$findTokens = [
49+
T_DECLARE,
50+
T_NAMESPACE,
51+
T_CLASS
52+
];
53+
54+
// Find the first occurrence of the tokens to look for.
55+
$position = $phpcsFile->findNext($findTokens, $stackPtr);
56+
57+
if ($position === false) {
58+
return;
59+
}
60+
61+
// If the first token found is not T_DECLARE, then the file does not include a strict_types declaration.
62+
if ($tokens[$position]['code'] !== T_DECLARE) {
63+
// Fix and set the boolean flag to true.
64+
$this->fix($phpcsFile, $position);
65+
}
66+
67+
// If the file includes a declare directive, and the file has not already been fixed, scan specifically
68+
// for strict_types and fix as needed.
69+
if (!$this->fixed) {
70+
if (!$this->scan($phpcsFile, $tokens, $position)) {
71+
$this->fix($phpcsFile, $position);
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Fixer to add the strict_types declaration.
78+
*
79+
* @param File $phpcsFile
80+
* @param int $position
81+
*/
82+
private function fix(File $phpcsFile, int $position) : void
83+
{
84+
// Get the fixer.
85+
$fixer = $phpcsFile->fixer;
86+
// Record the error.
87+
$phpcsFile->addFixableError("Missing strict_types declaration", $position, self::class);
88+
// Prepend content at the given position.
89+
$fixer->addContentBefore($position, "declare(strict_types=1);\n\n");
90+
// Set flag.
91+
$this->fixed = true;
92+
}
93+
94+
/**
95+
* Recursive method to scan declare statements for strict_types.
96+
*
97+
* @param File $phpcsFile
98+
* @param array $tokens
99+
* @param int $position
100+
* @return bool
101+
*/
102+
private function scan(File $phpcsFile, array $tokens, int $position) : bool
103+
{
104+
// Exit statement, if the beginning of the file has been reached.
105+
if ($tokens[$position]['code'] === T_OPEN_TAG || $position === 0) {
106+
return false;
107+
}
108+
109+
if (!$phpcsFile->findNext([T_STRING], $position)) {
110+
// If there isn't a T_STRING token for the declare directive, continue scan.
111+
return $this->scan($phpcsFile, $tokens, $phpcsFile->findPrevious([T_DECLARE], $position - 1));
112+
} else {
113+
// Checking specifically for strict_types.
114+
$temp = $phpcsFile->findNext([T_STRING], $position);
115+
if ($tokens[$temp]['content'] === 'strict_types') {
116+
// Return true as strict_types directive has been found.
117+
return true;
118+
} else {
119+
// Continue scan if strict_types hasn't been found.
120+
return $this->scan($phpcsFile, $tokens, $phpcsFile->findPrevious([T_DECLARE], $position - 1));
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)