-
Notifications
You must be signed in to change notification settings - Fork 20
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
47b742a
7eb2214
1835e80
1a624e0
395e6f6
707bbe9
2ba3a8d
afcf986
930ae6a
719dfc4
a9856f1
9423b43
95a1dd8
d13f05b
38fb7c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.idea | ||
/composer.lock | ||
/vendor | ||
/auth.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
dist: trusty | ||
|
||
git: | ||
depth: false | ||
|
||
language: php | ||
php: | ||
- '7.1' | ||
- '7.2' | ||
- '7.3' | ||
|
||
env: | ||
- TEST_SUITE=static-unit XDEBUG=true | ||
|
||
install: | ||
- if [ $TRAVIS_SECURE_ENV_VARS == "true" ]; then composer config http-basic.repo.magento.com ${REPO_USERNAME} ${REPO_PASSWORD}; fi; | ||
- if [ $TRAVIS_SECURE_ENV_VARS == "true" ]; then composer config github-oauth.github.com ${GITHUB_TOKEN}; fi; | ||
- composer config repositories.magento composer https://repo.magento.com/ | ||
- composer require "magento/framework:*" --no-update | ||
- composer require "magento/module-store:*" --no-update | ||
- composer require "magento/module-url-rewrite:*" --no-update | ||
- composer update -n --no-suggest | ||
|
||
script: | ||
- if [ "$TEST_SUITE" == "static-unit" ]; then ./Test/static/static-travis.sh; fi | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CloudComponents\Model; | ||
|
||
use Magento\Store\Model\Store; | ||
|
@@ -21,8 +23,7 @@ class UrlFixer | |
*/ | ||
public function run(Store $store, $url): string | ||
{ | ||
if ( | ||
($store->getForceDisableRewrites() || !$store->getConfig(Store::XML_PATH_USE_REWRITES)) | ||
if (($store->getForceDisableRewrites() || !$store->getConfig(Store::XML_PATH_USE_REWRITES)) | ||
&& strpos($url, '/magento/') !== false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also create task to core to fix this issue |
||
) { | ||
return preg_replace('|/magento/|', '/', $url, 1); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CloudComponents\Test\Unit\Model; | ||
|
||
use Magento\CloudComponents\Model\UrlFixer; | ||
use Magento\Store\Model\Store; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
class UrlFixerTest extends TestCase | ||
{ | ||
/** | ||
* @var UrlFixer | ||
*/ | ||
private $urlFixer; | ||
|
||
/** | ||
* @var MockObject|Store | ||
*/ | ||
private $storeMock; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->storeMock = $this->createPartialMock(Store::class, ['getForceDisableRewrites', 'getConfig']); | ||
$this->urlFixer = new UrlFixer(); | ||
} | ||
|
||
/** | ||
* @param bool $rewritesDisabled | ||
* @param bool $useConfigRewrites | ||
* @param string $url | ||
* @param string $expectedUrl | ||
* @dataProvider runDataProvider | ||
*/ | ||
public function testRunWithConfigRewrites( | ||
string $url, | ||
string $expectedUrl, | ||
bool $rewritesDisabled = false, | ||
bool $useConfigRewrites = true | ||
) { | ||
$this->storeMock->expects($this->once()) | ||
->method('getForceDisableRewrites') | ||
->willReturn($rewritesDisabled); | ||
|
||
if (!$rewritesDisabled) { | ||
$this->storeMock->expects($this->once()) | ||
->method('getConfig') | ||
->with(Store::XML_PATH_USE_REWRITES) | ||
->willReturn($useConfigRewrites); | ||
} else { | ||
$this->storeMock->expects($this->never()) | ||
->method('getConfig'); | ||
} | ||
|
||
$this->assertEquals($expectedUrl, $this->urlFixer->run($this->storeMock, $url)); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function runDataProvider(): array | ||
{ | ||
return [ | ||
'rewrites enabled, url without "magento" part' => [ | ||
'http://example.com/', | ||
'http://example.com/', | ||
], | ||
'rewrites disabled, url without "magento" part' => [ | ||
'http://example.com/', | ||
'http://example.com/', | ||
true, | ||
true, | ||
], | ||
'rewrites enabled, url with "magento" part' => [ | ||
'http://example.com/magento/', | ||
'http://example.com/magento/', | ||
false, | ||
true, | ||
], | ||
'rewrites disabled in store, url with "magento" part' => [ | ||
'http://example.com/magento/', | ||
'http://example.com/', | ||
true, | ||
false, | ||
], | ||
'rewrites disabled in config, url with "magento" part' => [ | ||
'http://example.com/magento/', | ||
'http://example.com/', | ||
false, | ||
false, | ||
], | ||
'rewrites disabled, url with multiple "magento" part' => [ | ||
'http://example.com/magento/magento/magento/test.html', | ||
'http://example.com/magento/magento/test.html', | ||
true, | ||
false, | ||
], | ||
'rewrites disabled, url with "magento2" part' => [ | ||
'http://example.com/magento2/', | ||
'http://example.com/magento2/', | ||
true, | ||
false, | ||
], | ||
'rewrites disabled, with "magento" host' => [ | ||
'http://magento.com/magento2/', | ||
'http://magento.com/magento2/', | ||
true, | ||
false, | ||
], | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd" | ||
colors="true" | ||
columns="max" | ||
bootstrap="../../vendor/autoload.php" | ||
beStrictAboutTestsThatDoNotTestAnything="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory suffix="Test.php">./</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<php> | ||
<ini name="date.timezone" value="UTC"/> | ||
</php> | ||
</phpunit> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MagentoCloud\Test\Sniffs\Directives; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
/** | ||
* Sniffer to check if the strict_types declaration is included and add it if not. | ||
* | ||
* Class StrictTypesSniff | ||
*/ | ||
class StrictTypesSniff implements Sniff | ||
{ | ||
/** | ||
* Flag to keep track of whether the file has been fixed. | ||
* | ||
* @var boolean | ||
*/ | ||
private $fixed = false; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_OPEN_TAG | ||
]; | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return int|void | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
// Get all tokens. | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
// Tokens to look for. | ||
$findTokens = [ | ||
T_DECLARE, | ||
T_NAMESPACE, | ||
T_CLASS | ||
]; | ||
|
||
// Find the first occurrence of the tokens to look for. | ||
$position = $phpcsFile->findNext($findTokens, $stackPtr); | ||
|
||
if ($position === false) { | ||
return; | ||
} | ||
|
||
// If the first token found is not T_DECLARE, then the file does not include a strict_types declaration. | ||
if ($tokens[$position]['code'] !== T_DECLARE) { | ||
// Fix and set the boolean flag to true. | ||
$this->fix($phpcsFile, $position); | ||
} | ||
|
||
// If the file includes a declare directive, and the file has not already been fixed, scan specifically | ||
// for strict_types and fix as needed. | ||
if (!$this->fixed) { | ||
if (!$this->scan($phpcsFile, $tokens, $position)) { | ||
$this->fix($phpcsFile, $position); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fixer to add the strict_types declaration. | ||
* | ||
* @param File $phpcsFile | ||
* @param int $position | ||
*/ | ||
private function fix(File $phpcsFile, int $position) : void | ||
{ | ||
// Get the fixer. | ||
$fixer = $phpcsFile->fixer; | ||
// Record the error. | ||
$phpcsFile->addFixableError("Missing strict_types declaration", $position, self::class); | ||
// Prepend content at the given position. | ||
$fixer->addContentBefore($position, "declare(strict_types=1);\n\n"); | ||
// Set flag. | ||
$this->fixed = true; | ||
} | ||
|
||
/** | ||
* Recursive method to scan declare statements for strict_types. | ||
* | ||
* @param File $phpcsFile | ||
* @param array $tokens | ||
* @param int $position | ||
* @return bool | ||
*/ | ||
private function scan(File $phpcsFile, array $tokens, int $position) : bool | ||
{ | ||
// Exit statement, if the beginning of the file has been reached. | ||
if ($tokens[$position]['code'] === T_OPEN_TAG || $position === 0) { | ||
return false; | ||
} | ||
|
||
if (!$phpcsFile->findNext([T_STRING], $position)) { | ||
// If there isn't a T_STRING token for the declare directive, continue scan. | ||
return $this->scan($phpcsFile, $tokens, $phpcsFile->findPrevious([T_DECLARE], $position - 1)); | ||
} else { | ||
// Checking specifically for strict_types. | ||
$temp = $phpcsFile->findNext([T_STRING], $position); | ||
if ($tokens[$temp]['content'] === 'strict_types') { | ||
// Return true as strict_types directive has been found. | ||
return true; | ||
} else { | ||
// Continue scan if strict_types hasn't been found. | ||
return $this->scan($phpcsFile, $tokens, $phpcsFile->findPrevious([T_DECLARE], $position - 1)); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail without keys, don't you want to add if statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok if it will fail, without "magento/framework:*" package test scripts won't run anyway.