Increase Mutation Score Indicator (MSI) by creating stronger tests of BaseCollection class

This commit is contained in:
Meritoo
2020-10-24 17:29:50 +02:00
parent 5cd58aec25
commit 8fec0db05f
7 changed files with 113 additions and 47 deletions

View File

@@ -46,13 +46,21 @@ abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggrega
return $elements; return $elements;
} }
/**
* Returns information if given element has valid type
*
* @param mixed $element Element of collection
* @return bool
*/
abstract protected function isValidType($element): bool;
/** /**
* Returns elements of collection with valid types * Returns elements of collection with valid types
* *
* @param array $elements The elements of collection to verify * @param array $elements The elements of collection to verify
* @return array * @return array
*/ */
protected function getElementsWithValidType(array $elements): array private function getElementsWithValidType(array $elements): array
{ {
if (empty($elements)) { if (empty($elements)) {
return []; return [];
@@ -70,12 +78,4 @@ abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggrega
return $result; return $result;
} }
/**
* Returns information if given element has valid type
*
* @param mixed $element Element of collection
* @return bool
*/
abstract protected function isValidType($element): bool;
} }

View File

@@ -8,12 +8,12 @@
declare(strict_types=1); declare(strict_types=1);
namespace Meritoo\Test\Common\Collection\Collection; namespace Meritoo\Test\Common\Collection\BaseCollection;
use Meritoo\Common\Collection\BaseCollection; use Meritoo\Common\Collection\BaseCollection;
/** /**
* Collection of arrays * Collection of first names
* *
* @author Meritoo <github@meritoo.pl> * @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl> * @copyright Meritoo <http://www.meritoo.pl>
@@ -21,10 +21,22 @@ use Meritoo\Common\Collection\BaseCollection;
* @internal * @internal
* @coversNothing * @coversNothing
*/ */
class ArrayCollection extends BaseCollection class FirstNamesCollection extends BaseCollection
{ {
protected function isValidType($element): bool protected function isValidType($element): bool
{ {
return is_array($element); return $element instanceof User;
}
protected function prepareElements(array $elements): array
{
$result = [];
/** @var User $element */
foreach ($elements as $element) {
$result[] = $element->getFirstName();
}
return $result;
} }
} }

View File

@@ -0,0 +1,45 @@
<?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Meritoo\Test\Common\Collection\BaseCollection;
/**
* User. Element of collection.
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
*
* @internal
* @coversNothing
*/
final class User
{
/** @var string */
private $firstName;
/** @var string */
private $lastName;
public function __construct(string $firstName, string $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName(): string
{
return $this->firstName;
}
public function getLastName(): string
{
return $this->lastName;
}
}

View File

@@ -15,7 +15,8 @@ use Meritoo\Common\Collection\DateTimeCollection;
use Meritoo\Common\Collection\StringCollection; use Meritoo\Common\Collection\StringCollection;
use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Test\Base\BaseTestCase;
use Meritoo\Common\Type\OopVisibilityType; use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Test\Common\Collection\Collection\ArrayCollection; use Meritoo\Test\Common\Collection\BaseCollection\FirstNamesCollection;
use Meritoo\Test\Common\Collection\BaseCollection\User;
use ReflectionClass; use ReflectionClass;
/** /**
@@ -360,7 +361,7 @@ class BaseCollectionTest extends BaseTestCase
*/ */
public function testGetElementsWithValidType(string $description, array $elements, array $expected): void public function testGetElementsWithValidType(string $description, array $elements, array $expected): void
{ {
$collection = new ArrayCollection($elements); $collection = new FirstNamesCollection($elements);
static::assertSame($expected, $collection->toArray(), $description); static::assertSame($expected, $collection->toArray(), $description);
} }
@@ -511,21 +512,12 @@ class BaseCollectionTest extends BaseTestCase
yield[ yield[
'Collection with existing index (collection of arrays)', 'Collection with existing index (collection of arrays)',
new ArrayCollection([ new FirstNamesCollection([
[ new User('John', 'Scott'),
'lorem', new User('Jane', 'Brown'),
'ipsum',
],
[
'dolor',
'sit',
],
]), ]),
0, 0,
[ 'John',
'lorem',
'ipsum',
],
]; ];
yield[ yield[
@@ -538,6 +530,16 @@ class BaseCollectionTest extends BaseTestCase
'y', 'y',
new \DateTime('2001-01-01'), new \DateTime('2001-01-01'),
]; ];
yield[
'Collection with first names',
new FirstNamesCollection([
new User('John', 'Scott'),
new User('Jane', 'Brown'),
]),
1,
'Jane',
];
} }
public function provideElementsToValidateType(): ?Generator public function provideElementsToValidateType(): ?Generator
@@ -551,18 +553,12 @@ class BaseCollectionTest extends BaseTestCase
yield[ yield[
'Valid elements only', 'Valid elements only',
[ [
[], new User('John', 'Scott'),
[ new User('Jane', 'Brown'),
'123',
456,
],
], ],
[ [
[], 'John',
[ 'Jane',
'123',
456,
],
], ],
]; ];
@@ -572,20 +568,15 @@ class BaseCollectionTest extends BaseTestCase
1, 1,
'test', 'test',
'', '',
new User('John', 'Scott'),
[], [],
234, 234,
'test', 'test',
[ new User('Jane', 'Brown'),
'123',
456,
],
], ],
[ [
3 => [], 'John',
6 => [ 'Jane',
'123',
456,
],
], ],
]; ];
} }

View File

@@ -1,5 +1,11 @@
<?php <?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1); declare(strict_types=1);
namespace Meritoo\Test\Common\Collection; namespace Meritoo\Test\Common\Collection;

View File

@@ -1,5 +1,11 @@
<?php <?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1); declare(strict_types=1);
namespace Meritoo\Test\Common\Collection; namespace Meritoo\Test\Common\Collection;

View File

@@ -1,5 +1,11 @@
<?php <?php
/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1); declare(strict_types=1);
namespace Meritoo\Test\Common\Collection; namespace Meritoo\Test\Common\Collection;