Skip to content

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

Merged
merged 15 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
47b742a
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
7eb2214
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
1835e80
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
1a624e0
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
395e6f6
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
707bbe9
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
2ba3a8d
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 27, 2019
afcf986
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 30, 2019
930ae6a
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 30, 2019
719dfc4
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 30, 2019
a9856f1
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Sep 30, 2019
9423b43
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Oct 1, 2019
95a1dd8
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Oct 1, 2019
d13f05b
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Oct 1, 2019
38fb7c7
MAGECLOUD-3967: Add static and unit tests to magento-cloud-components
oshmyheliuk Oct 1, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/composer.lock
/vendor
/auth.json
26 changes: 26 additions & 0 deletions .travis.yml
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
Copy link
Member

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?

Copy link
Contributor Author

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.

- 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be in tests directory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why must? it will be strange to have Test and tests directories on one level.


2 changes: 2 additions & 0 deletions Console/Command/ConfigShowDefaultUrlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudComponents\Console\Command;

use Magento\CloudComponents\Model\UrlFixer;
Expand Down
3 changes: 3 additions & 0 deletions Console/Command/ConfigShowEntityUrlsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudComponents\Console\Command;

use Magento\CloudComponents\Model\UrlFixer;
Expand All @@ -23,6 +25,7 @@

/**
* Returns list of category or cms-page urls for given stores
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConfigShowEntityUrlsCommand extends Command
{
Expand Down
2 changes: 2 additions & 0 deletions Console/Command/ConfigShowStoreUrlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudComponents\Console\Command;

use Magento\CloudComponents\Model\UrlFixer;
Expand Down
5 changes: 3 additions & 2 deletions Model/UrlFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The 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);
Expand Down
123 changes: 123 additions & 0 deletions Test/Unit/Model/UrlFixerTest.php
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,
],
];
}
}
19 changes: 19 additions & 0 deletions Test/Unit/phpunit.xml.dist
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>
124 changes: 124 additions & 0 deletions Test/static/Sniffs/Directives/StrictTypesSniff.php
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));
}
}
}
}
Loading