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;
}
/**
* 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
*
* @param array $elements The elements of collection to verify
* @return array
*/
protected function getElementsWithValidType(array $elements): array
private function getElementsWithValidType(array $elements): array
{
if (empty($elements)) {
return [];
@@ -70,12 +78,4 @@ abstract class BaseCollection implements Countable, ArrayAccess, IteratorAggrega
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);
namespace Meritoo\Test\Common\Collection\Collection;
namespace Meritoo\Test\Common\Collection\BaseCollection;
use Meritoo\Common\Collection\BaseCollection;
/**
* Collection of arrays
* Collection of first names
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
@@ -21,10 +21,22 @@ use Meritoo\Common\Collection\BaseCollection;
* @internal
* @coversNothing
*/
class ArrayCollection extends BaseCollection
class FirstNamesCollection extends BaseCollection
{
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\Test\Base\BaseTestCase;
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;
/**
@@ -360,7 +361,7 @@ class BaseCollectionTest extends BaseTestCase
*/
public function testGetElementsWithValidType(string $description, array $elements, array $expected): void
{
$collection = new ArrayCollection($elements);
$collection = new FirstNamesCollection($elements);
static::assertSame($expected, $collection->toArray(), $description);
}
@@ -511,21 +512,12 @@ class BaseCollectionTest extends BaseTestCase
yield[
'Collection with existing index (collection of arrays)',
new ArrayCollection([
[
'lorem',
'ipsum',
],
[
'dolor',
'sit',
],
new FirstNamesCollection([
new User('John', 'Scott'),
new User('Jane', 'Brown'),
]),
0,
[
'lorem',
'ipsum',
],
'John',
];
yield[
@@ -538,6 +530,16 @@ class BaseCollectionTest extends BaseTestCase
'y',
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
@@ -551,18 +553,12 @@ class BaseCollectionTest extends BaseTestCase
yield[
'Valid elements only',
[
[],
[
'123',
456,
],
new User('John', 'Scott'),
new User('Jane', 'Brown'),
],
[
[],
[
'123',
456,
],
'John',
'Jane',
],
];
@@ -572,20 +568,15 @@ class BaseCollectionTest extends BaseTestCase
1,
'test',
'',
new User('John', 'Scott'),
[],
234,
'test',
[
'123',
456,
],
new User('Jane', 'Brown'),
],
[
3 => [],
6 => [
'123',
456,
],
'John',
'Jane',
],
];
}

View File

@@ -1,5 +1,11 @@
<?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;

View File

@@ -1,5 +1,11 @@
<?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;

View File

@@ -1,5 +1,11 @@
<?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;