Commit eb564eb6 authored by Daniel Hürtgen's avatar Daniel Hürtgen

Merge branch '7-implement-tests' into 'develop'

TASK: Resolve "Implement tests"

Closes #7

See merge request !44
parents 9a1bf782 b45a4aa9
Pipeline #1117 failed with stages
in 8 minutes and 53 seconds
stages:
- lint
- test
cache:
paths:
- vendor/
before_script:
- apt-get update; apt-get install -y unzip
- composer install --optimize-autoloader --no-interaction --no-ansi --prefer-dist
lint:coding-guideline: &PHP-LINTING
image: tetraweb/php:7.0
stage: test
script:
- mkdir result
- ./vendor/bin/phpcs -s -n --report-full=result/phpcs-full.txt --report-diff=result/phpcs-diff.txt --report-summary=result/phpcs-summary.txt
.lint: &lint
image: composer
stage: lint
before_script:
- apk --no-cache add parallel
- php -v
- composer --no-ansi self-update
- composer --no-ansi --version
- composer install --optimize-autoloader --classmap-authoritative --no-interaction --prefer-dist --no-ansi
artifacts:
when: on_failure
paths:
- result
lint:
<<: *lint
script:
- find . -name \*.php ! -path "./vendor/*" | parallel --gnu php -d display_errors=stderr -l {} > /dev/null \;
lint:coding-guideline:
<<: *lint
script:
- mkdir result
- ./vendor/bin/phpcs -s -n --report=full --report-full=result/phpcs-full.txt --report-diff=result/phpcs-diff.txt --report-summary=result/phpcs-summary.txt
lint:php-mass-detection:
<<: *PHP-LINTING
<<: *lint
script:
- mkdir result
- ./vendor/bin/phpmd src text phpmd.xml | tee result/phpmd.txt
test:5.4: &PHP-UNITTESTING
image: tetraweb/php:5.4
.test: &test
stage: test
tags:
- docker
variables:
TIMEZONE: "Europe/Berlin"
WITH_XDEBUG: "true"
before_script:
- apt-get update; apt-get install -y unzip
- composer install --optimize-autoloader --no-interaction --no-ansi --prefer-dist
script:
- ./vendor/bin/phpunit --coverage-text --colors=never
test:5.4:
<<: *test
image: tetraweb/php:5.4
test:5.5:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:5.5
test:5.6:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:5.6
test:7.0:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:7.0
test:7.1:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:7.1
test:latest:
<<: *PHP-UNITTESTING
<<: *test
image: tetraweb/php:latest
......@@ -28,7 +28,7 @@
},
"autoload-dev": {
"psr-4": {
"Higidi\\ComposerPhpCSStandardsPlugin\\Tests\\": "tests/src/"
"Higidi\\ComposerPhpCSStandardsPlugin\\Tests\\": "tests/"
}
},
"require": {
......@@ -40,7 +40,7 @@
},
"require-dev": {
"composer/composer": "^1.4",
"phpunit/phpunit": "^4.5 || ^5.0.5",
"phpunit/phpunit": "^4.8.36 || ^5.0.5",
"phpmd/phpmd": "^2.6"
},
"extra": {
......
......@@ -11,8 +11,11 @@
bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Project Test Suite">
<directory>./tests/src/</directory>
<testsuite name="Unit Tests">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="Integration Tests">
<directory>./tests/Integration/</directory>
</testsuite>
</testsuites>
......
......@@ -70,15 +70,15 @@ class Installer extends LibraryInstaller
*/
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package)
{
if (!parent::isInstalled($repo, $package)) {
if (! parent::isInstalled($repo, $package)) {
return false;
}
$srcStandards = $this->getSourceStandards($package);
$dstStandards = $this->getDestinationStandards($repo);
foreach ($srcStandards as $srcStandard) {
if (!$dstStandards->hasStandard($srcStandard)
|| !$this->compareStandards($srcStandard, $dstStandards->getStandard($srcStandard))
if (! $dstStandards->hasStandard($srcStandard)
|| ! $this->compareStandards($srcStandard, $dstStandards->getStandard($srcStandard))
) {
return false;
}
......@@ -110,7 +110,7 @@ class Installer extends LibraryInstaller
*/
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
{
if (!$repo->hasPackage($package)) {
if (! $repo->hasPackage($package)) {
throw new \InvalidArgumentException('Package is not installed: ' . $package);
}
$this->removeStandards($repo, $package);
......@@ -121,6 +121,7 @@ class Installer extends LibraryInstaller
* @param InstalledRepositoryInterface $repo
* @param PackageInterface $package
* @param bool $override
*
* @return void
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
......@@ -138,13 +139,14 @@ class Installer extends LibraryInstaller
$this->io->writeError(sprintf(' <info>%s</info>', $srcStandard->getName()));
$srcPath = $srcStandard->getPath();
$dstPath = $dstStdBasePath . DIRECTORY_SEPARATOR . $srcStandard->getName();
$filesystem->mirror($srcPath, $dstPath, null, array('override' => $override));
$filesystem->mirror($srcPath, $dstPath, null, ['override' => $override]);
}
}
/**
* @param InstalledRepositoryInterface $repo
* @param PackageInterface $package
*
* @return void
*/
protected function removeStandards(InstalledRepositoryInterface $repo, PackageInterface $package)
......@@ -153,7 +155,7 @@ class Installer extends LibraryInstaller
$dstStandards = $this->getDestinationStandards($repo);
$this->io->writeError(' Removing PHP-CodeSniffer Standards:', false);
foreach ($srcStandards as $srcStandard) {
if (!$dstStandards->hasStandard($srcStandard)) {
if (! $dstStandards->hasStandard($srcStandard)) {
continue;
}
$this->io->writeError(sprintf(' <info>%s</info>', $srcStandard->getName()));
......@@ -167,6 +169,7 @@ class Installer extends LibraryInstaller
* Get source (provided by the composer package) standards for package.
*
* @param PackageInterface $package
*
* @return Standards
*/
protected function getSourceStandards(PackageInterface $package)
......@@ -180,6 +183,7 @@ class Installer extends LibraryInstaller
* Get destination (provided by PHPCodeSniffer) standards.
*
* @param InstalledRepositoryInterface $repo
*
* @return Standards
*/
protected function getDestinationStandards(InstalledRepositoryInterface $repo)
......@@ -192,6 +196,7 @@ class Installer extends LibraryInstaller
/**
* @param Standard $source
* @param Standard $destination
*
* @return bool
*/
protected function compareStandards(Standard $source, Standard $destination)
......@@ -202,6 +207,7 @@ class Installer extends LibraryInstaller
/**
* @param string $basePath
*
* @return Standards
*/
protected function findStandards($basePath)
......@@ -211,6 +217,7 @@ class Installer extends LibraryInstaller
/**
* @param InstalledRepositoryInterface $repo
*
* @return string
*/
protected function getPHPCodeSnifferStandardsBasePath(InstalledRepositoryInterface $repo)
......@@ -223,13 +230,14 @@ class Installer extends LibraryInstaller
/**
* @param InstalledRepositoryInterface $repo
*
* @return PackageInterface
*/
protected function getPHPCodeSnifferPackage(InstalledRepositoryInterface $repo)
{
$packageKey = 'squizlabs/php_codesniffer';
$package = $repo->findPackage($packageKey, '*');
if (!$package) {
if (! $package) {
throw new \RuntimeException(sprintf('Package "%s" not installed.', $packageKey));
}
......@@ -238,6 +246,7 @@ class Installer extends LibraryInstaller
/**
* @param InstalledRepositoryInterface $repo
*
* @return string
*/
protected function getPHPCodeSnifferInstallPath(InstalledRepositoryInterface $repo)
......@@ -250,8 +259,8 @@ class Installer extends LibraryInstaller
*/
public function supports($packageType)
{
$secondaryTypes = array('phpcodesniffer-standards');
$deprecatedTypes = array('php-codesniffer-standards');
$secondaryTypes = ['phpcodesniffer-standards'];
$deprecatedTypes = ['php-codesniffer-standards'];
return parent::supports($packageType)
|| in_array($packageType, array_merge($secondaryTypes, $deprecatedTypes));
......
......@@ -23,9 +23,11 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\StandardInterface;
/**
* Class holds a set of PHPCodeSniffer standards.
*/
class Standards implements \Iterator
{
/**
* @var StandardInterface[]
*/
......@@ -36,15 +38,15 @@ class Standards implements \Iterator
*/
public function __construct(array $standards = [])
{
foreach ($standards as $standard) {
$this->addStandard($standard);
}
$this->setStandards($standards);
}
/**
* @param StandardInterface $standard
* Add a single standard to set.
*
* @param StandardInterface $standard The standard to add
*
* @return $this
* @return $this Fluent interface
*/
public function addStandard(StandardInterface $standard)
{
......@@ -54,9 +56,11 @@ class Standards implements \Iterator
}
/**
* @param string|StandardInterface $standard
* Check whether set holds the given standard.
*
* @return bool
* @param string|StandardInterface $standard The standard to check
*
* @return bool True if sets hold the standard, otherwise false
*/
public function hasStandard($standard)
{
......@@ -64,9 +68,11 @@ class Standards implements \Iterator
}
/**
* @param string|StandardInterface $standard
* Remove a single standard from the set.
*
* @param string|StandardInterface $standard The standard to remove
*
* @return $this
* @return $this Fluent interface
*/
public function removeStandard($standard)
{
......@@ -78,9 +84,11 @@ class Standards implements \Iterator
}
/**
* @param string|StandardInterface $standard
* Get a single standard.
*
* @param string|StandardInterface $standard The standard to get
*
* @return StandardInterface|null
* @return StandardInterface|null Return the standard or null if not exist
*/
public function getStandard($standard)
{
......@@ -92,17 +100,38 @@ class Standards implements \Iterator
}
/**
* @return array|StandardInterface[]
* Get all standards hold by this set.
*
* @return array|StandardInterface[] Array of standards
*/
public function getStandards()
{
return $this->standards;
return array_values($this->standards);
}
/**
* Set standards hold by this set.
*
* @param StandardInterface[] $standards Array with standards to set
*
* @return $this Fluent interface
*/
public function setStandards(array $standards)
{
$this->standards = [];
foreach ($standards as $standard) {
$this->addStandard($standard);
}
return $this;
}
/**
* @param string|StandardInterface $standard
* Extract the name of the given standard.
*
* @param string|StandardInterface $standard The standard get name from
*
* @return string
* @return string The standard name
*/
protected function getStandardName($standard)
{
......@@ -146,7 +175,9 @@ class Standards implements \Iterator
*/
public function key()
{
return key($this->standards);
$key = key($this->standards);
return array_search($key, array_keys($this->standards));
}
/**
......
......@@ -25,6 +25,9 @@ use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
/**
* Plugin class register our plugin with composer.
*/
class Plugin implements PluginInterface
{
/**
......
......@@ -23,11 +23,12 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Integration\PHPCodeSniffer\S
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Factory;
use PHPUnit\Framework\TestCase;
/**
* Integration test case for class \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder
*/
class FinderTest extends \PHPUnit_Framework_TestCase
class FinderTest extends TestCase
{
/**
* @var Finder
......@@ -59,7 +60,7 @@ class FinderTest extends \PHPUnit_Framework_TestCase
public function testFinder()
{
$path = realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', '..', 'Fixtures', 'Standards']));
$path = realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'Fixtures', 'Standards']));
$expected = [
$path . DIRECTORY_SEPARATOR . 'Standard1',
$path . DIRECTORY_SEPARATOR . 'Standard2',
......
......@@ -24,12 +24,15 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Unit\PHPCodeSniffer\Standard
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Factory;
use Higidi\ComposerPhpCSStandardsPlugin\Symfony\Finder\Factory as SymfonyFinderFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\Finder as SymfonyFinder;
/**
* Test case for class \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder
*
* @covers \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Finder
*/
class FinderTest extends \PHPUnit_Framework_TestCase
class FinderTest extends TestCase
{
/**
* @var Finder
......
......@@ -23,11 +23,14 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Unit\PHPCodeSniffer\Standard
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Factory;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Factory as StandardFactory;
use PHPUnit\Framework\TestCase;
/**
* Test case for class \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\StandardsFactory
* Test case for class \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Factory
*
* @covers \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Factory
*/
class FactoryTest extends \PHPUnit_Framework_TestCase
class FactoryTest extends TestCase
{
/**
* @var Factory
......
......@@ -22,11 +22,14 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Unit\PHPCodeSniffer\Standard
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Factory;
use PHPUnit\Framework\TestCase;
/**
* Test case for class \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Factory
*
* @covers \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Factory
*/
class FactoryTest extends \PHPUnit_Framework_TestCase
class FactoryTest extends TestCase
{
/**
* @var Factory
......@@ -49,7 +52,7 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
{
$path = implode(
DIRECTORY_SEPARATOR,
[__DIR__, '..', '..', '..', '..', '..', 'Fixtures', 'Standards', 'Standard1']
[__DIR__, '..', '..', '..', '..', 'Fixtures', 'Standards', 'Standard1']
);
$standard = $this->classUnderTesting->create($path);
......
......@@ -22,20 +22,26 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Unit\PHPCodeSniffer\Standard
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Standard;
use PHPUnit\Framework\TestCase;
/**
* Test case for class \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Standard
*
* @covers \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\Standard
*/
class StandardTest extends \PHPUnit_Framework_TestCase
class StandardTest extends TestCase
{
/**
* @dataProvider differentStandards
*
* @param string $standardName
* @param string $expectedName
*/
public function testStandard($standardName, $expectedName)
{
$path = implode(
DIRECTORY_SEPARATOR,
[__DIR__, '..', '..', '..', '..', '..', 'Fixtures', 'Standards', $standardName]
[__DIR__, '..', '..', '..', '..', 'Fixtures', 'Standards', $standardName]
);
$ruleSetXmlPath = $path . DIRECTORY_SEPARATOR . 'ruleset.xml';
......@@ -91,7 +97,7 @@ class StandardTest extends \PHPUnit_Framework_TestCase
{
$path = implode(
DIRECTORY_SEPARATOR,
[__DIR__, '..', '..', '..', '..', '..', 'Fixtures', 'Standards', 'InvalidStandard']
[__DIR__, '..', '..', '..', '..', 'Fixtures', 'Standards', 'InvalidStandard']
);
new Standard($path);
......
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Unit\PHPCodeSniffer\Standards;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\StandardInterface;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standards;
use PHPUnit\Framework\TestCase;
/**
* Test case for "\Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standards".
*
* @covers \Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standards
*/
class StandardsTest extends TestCase
{
/**
* @param string $name
*
* @return StandardInterface
*/
protected function createStandardMock($name)
{
$standard = $this->prophesize(
'\Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\StandardInterface'
);
$standard
->getName()
->willReturn($name);
return $standard->reveal();
}
/**
* @test
*/
public function itImplementsTheIteratorInterface()
{
$sut = new Standards();
$this->assertInstanceOf('\Iterator', $sut);
}
/**
* @test
*/
public function itHasAnEmptyArrayAsDefaultStandards()
{
$sut = new Standards();
$standards = $sut->getStandards();
$this->assertInternalType('array', $standards);
$this->assertEmpty($standards);
}
/**
* @test
*/
public function itAcceptsStandardViaConstructor()
{
$standards = [
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
];
$sut = new Standards($standards);
$this->assertSame($standards, $sut->getStandards());
}
/**
* @test
*/
public function itHoldsStandards()
{
$standards = [
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
];
$sut = new Standards();
$sut->setStandards($standards);
$this->assertSame($standards, $sut->getStandards());
return $sut;
}
/**
* @test
* @depends itHoldsStandards
*
* @param Standards $sut
*/
public function itCanAddStandardsToTheSet(Standards $sut)
{
$standards = $sut->getStandards();
$newStandard = $this->createStandardMock('standard4');
$this->assertSame($standards, $sut->getStandards());
$sut->addStandard($newStandard);
$expectedStandards = array_merge($standards, [$newStandard]);
$this->assertSame($expectedStandards, $sut->getStandards());
}
/**
* @test
* @depends itHoldsStandards
*
* @param Standards $sut
*/
public function itCanRemoveStandardsFromTheSet(Standards $sut)
{
$standards = $sut->getStandards();
$removeableStandard = array_shift($standards);
$sut->removeStandard($removeableStandard);
$this->assertSame($standards, $sut->getStandards());
}
/**
* @return array
*/
public function differentStandardIdentifierTypesDataProvider()
{
$standardName = 'blafoo';
$standard = $this->createStandardMock($standardName);
return [
'as_string' => [$standard, $standardName],
'as_object' => [$standard, $standard],
];
}
/**
* @test
* @dataProvider differentStandardIdentifierTypesDataProvider
*
* @param StandardInterface $standard
* @param string|object $identifier
*/
public function itReturnsWhetherItHoldsAStandardOrNot(StandardInterface $standard, $identifier)
{
$sut = new Standards();
$this->assertFalse($sut->hasStandard($identifier));
$sut->addStandard($standard);
$this->assertTrue($sut->hasStandard($identifier));
}
/**
* @test
* @dataProvider differentStandardIdentifierTypesDataProvider
*
* @param StandardInterface $standard
* @param string|object $identifier
*/
public function itReturnsAStandardItHolds(StandardInterface $standard, $identifier)
{
$sut = new Standards();
$this->assertNull($sut->getStandard($identifier));
$sut->addStandard($standard);
$this->assertSame($standard, $sut->getStandard($identifier));
}
/**
* @test
*/
public function itIsIteratable()
{
$standards = [
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
];
$iteratedStandards = [];
$sut = new Standards($standards);
foreach ($sut as $key => $standard) {
$iteratedStandards[$key] = $standard;
}
$this->assertSame($standards, $iteratedStandards);
}
}
<?php
namespace Higidi\ComposerPhpCSStandardsPlugin\Tests\Unit;
/*
* Copyright (C) 2017 Daniel Hürtgen <daniel@higidi.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*