Commit 612dc41f authored by Daniel Hürtgen's avatar Daniel Hürtgen

FEATURE: Provide Standards test cases

parent 085d06f0
......@@ -23,6 +23,9 @@ namespace Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards;
use Higidi\ComposerPhpCSStandardsPlugin\PHPCodeSniffer\Standards\Standard\StandardInterface;
/**
* Class holds a set of PHPCodeSniffer standards.
*/
class Standards implements \Iterator
{
/**
......@@ -35,15 +38,15 @@ class Standards implements \Iterator
*/
public function __construct(array $standards = array())
{
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)
{
......@@ -53,9 +56,11 @@ class Standards implements \Iterator
}
/**
* @param string|StandardInterface $standard
* Check whether set holds the given standard.
*
* @param string|StandardInterface $standard The standard to check
*
* @return bool
* @return bool True if sets hold the standard, otherwise false
*/
public function hasStandard($standard)
{
......@@ -63,9 +68,11 @@ class Standards implements \Iterator
}
/**
* @param string|StandardInterface $standard
* Remove a single standard from the set.
*
* @return $this
* @param string|StandardInterface $standard THe standard to remove
*
* @return $this Fluent interface
*/
public function removeStandard($standard)
{
......@@ -77,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)
{
......@@ -91,17 +100,38 @@ class Standards implements \Iterator
}
/**
* @return array|StandardInterface[]
* Get all standards holds by this set.
*
* @return array|StandardInterface[] Array of standards
*/
public function getStandards()
{
return $this->standards;
return array_values($this->standards);
}
/**
* @param string|StandardInterface $standard
* Set standards hold by this set.
*
* @return string
* @param StandardInterface[] $standards Array with standards to set
*
* @return $this Fluent interface
*/
public function setStandards(array $standards)
{
$this->standards = array();
foreach ($standards as $standard) {
$this->addStandard($standard);
}
return $this;
}
/**
* Extract the name of the given standard.
*
* @param string|StandardInterface $standard The standard get name from
*
* @return string The standard name
*/
protected function getStandardName($standard)
{
......@@ -142,7 +172,9 @@ class Standards implements \Iterator
*/
public function key()
{
return key($this->standards);
$key = key($this->standards);
return array_search($key, array_keys($this->standards));
}
/**
......
<?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 = array(
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
);
$sut = new Standards($standards);
$this->assertSame($standards, $sut->getStandards());
}
/**
* @test
*/
public function itHoldsStandards()
{
$standards = array(
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
);
$sut = new Standards();
$sut->setStandards($standards);
$this->assertSame($standards, $sut->getStandards());
}
/**
* @test
*/
public function itCanAddStandardsToTheSet()
{
$standards = array(
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
);
$newStandard = $this->createStandardMock('standard4');
$sut = new Standards($standards);
$this->assertSame($standards, $sut->getStandards());
$sut->addStandard($newStandard);
$expectedStandards = array_merge($standards, array($newStandard));
$this->assertSame($expectedStandards, $sut->getStandards());
}
/**
* @test
*/
public function itCanRemoveStandardsFromTheSet()
{
$standards = array(
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
);
$removeableStandard = $this->createStandardMock('standard4');
$initalStandards = array_merge($standards, array($removeableStandard));
$sut = new Standards($initalStandards);
$this->assertSame($initalStandards, $sut->getStandards());
$sut->removeStandard($removeableStandard);
$this->assertSame($standards, $sut->getStandards());
}
/**
* @return array
*/
public function differentStandardIdentifierTypesDataProvider()
{
$standardName = 'blafoo';
$standard = $this->createStandardMock($standardName);
return array(
'as_string' => array($standard, $standardName),
'as_object' => array($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 = array(
$this->createStandardMock('standard1'),
$this->createStandardMock('standard2'),
$this->createStandardMock('standard3'),
);
$iteratedStandards = array();
$sut = new Standards($standards);
foreach ($sut as $key => $standard) {
$iteratedStandards[$key] = $standard;
}
$this->assertSame($standards, $iteratedStandards);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment