Skip to content

Commit 6ac4f6c

Browse files
authored
improved code coverage (#1)
* test: improved code coverage * fix: improved code coverage * feat: exclude .phpstorm.meta.php
1 parent 75dfdbb commit 6ac4f6c

File tree

12 files changed

+315
-4
lines changed

12 files changed

+315
-4
lines changed

.phpstorm.meta.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace PHPSTORM_META
4+
{
5+
$STATIC_METHOD_TYPES = [
6+
\Test\_generated\UnitTesterActions::grabService('') => [
7+
"" == "@",
8+
],
9+
];
10+
}

grumphp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ parameters:
4141
- test
4242
- scopes: []
4343
phpstan:
44-
ignore_patterns: [src/Migrations, c3.php]
44+
ignore_patterns: [src/Migrations, c3.php, .phpstorm.meta.php]
4545
phplint: ~
4646
phpversion:
4747
project: '7.4'

src/Repository/MakeRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public function __construct(ManagerRegistry $registry)
1717
}
1818

1919
/**
20+
* @param int $id
21+
* @return Make
2022
* @throws RowNotFoundException
2123
*/
2224
public function findById(int $id): Make

src/Service/SearchService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function search(Make $make, VehicleType $type): array
3535
return $results;
3636
}
3737

38-
private function createLog(Make $make, VehicleType $type, int $resultCount, StopwatchEvent $stopwatchEvent): void
38+
private function createLog(Make $make, VehicleType $type, int $resultsCount, StopwatchEvent $stopwatchEvent): void
3939
{
4040
$ip = '';
4141
$userAgent = '';
@@ -46,7 +46,7 @@ private function createLog(Make $make, VehicleType $type, int $resultCount, Stop
4646

4747
$log = new SearchLog();
4848
$log->setIpAddress($ip);
49-
$log->setFoundModels($resultCount);
49+
$log->setFoundModels($resultsCount);
5050
$log->setUserAgent($userAgent);
5151
$log->setMakeAbbr($make->getCode());
5252
$log->setVehicleType($type->getCode());

tests/functional/ModelCest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ public function tryToTest(FunctionalTester $I): void
1616
$content = $I->grabPageSource();
1717
$I->assertStringContainsString('[{"id":1,"description":"LESABRE"}]', $content);
1818
}
19+
20+
public function tryToTestEmptyModel(FunctionalTester $I): void
21+
{
22+
$I->haveInDatabase('vehicle_type', ['id' => 1, 'description' => 'Automobile', 'code' => 'V']);
23+
$I->haveInDatabase('make', ['id' => 2, 'type_id' => 1, 'code' => 'BUIC', 'description' => 'Buic']);
24+
$I->sendAjaxGetRequest('/models/1/2');
25+
$I->canSeeResponseCodeIs(404);
26+
}
27+
28+
public function tryToTestNotFound(FunctionalTester $I): void
29+
{
30+
$I->sendAjaxGetRequest('/models/1/1');
31+
$I->canSeeResponseCodeIs(404);
32+
}
1933
}

tests/unit.suite.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@ modules:
77
enabled:
88
- Asserts
99
- \Test\Helper\Unit
10-
step_decorators: ~
10+
- Symfony:
11+
app_path: 'src'
12+
environment: 'test'
13+
- Doctrine2:
14+
depends: Symfony
15+
cleanup: true
16+
- Db:
17+
dsn: 'mysql:host=mysql;dbname=example'
18+
user: 'dev'
19+
password: 'dev'
20+
step_decorators: ~

tests/unit/Entity/MakeTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Entity;
6+
7+
use App\Entity\Make;
8+
use App\Entity\VehicleType;
9+
use Codeception\Test\Unit;
10+
use Doctrine\ORM\EntityManagerInterface;
11+
use Test\UnitTester;
12+
13+
class MakeTest extends Unit
14+
{
15+
protected UnitTester $tester;
16+
17+
public function testSetters(): void
18+
{
19+
$em = $this->tester->grabService(EntityManagerInterface::class);
20+
$vehicleType = new VehicleType();
21+
$vehicleType->setCode('V');
22+
$vehicleType->setDescription('Automobile');
23+
$em->persist($vehicleType);
24+
25+
$make = new Make();
26+
$make->setType($vehicleType);
27+
$make->setDescription('Buic');
28+
$make->setCode('BUIC');
29+
$em->persist($make);
30+
$em->flush();
31+
}
32+
}

tests/unit/Entity/ModelTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Entity;
6+
7+
use App\Entity\Make;
8+
use App\Entity\Model;
9+
use App\Entity\VehicleType;
10+
use Codeception\Test\Unit;
11+
use Doctrine\ORM\EntityManagerInterface;
12+
use Test\UnitTester;
13+
14+
class ModelTest extends Unit
15+
{
16+
protected UnitTester $tester;
17+
18+
public function testSetters(): void
19+
{
20+
$em = $this->tester->grabService(EntityManagerInterface::class);
21+
$vehicleType = new VehicleType();
22+
$vehicleType->setCode('V');
23+
$vehicleType->setDescription('Automobile');
24+
$em->persist($vehicleType);
25+
26+
$make = new Make();
27+
$make->setType($vehicleType);
28+
$make->setDescription('Buic');
29+
$make->setCode('BUIC');
30+
$em->persist($make);
31+
32+
$model = new Model();
33+
$model->setType($vehicleType);
34+
$model->setMake($make);
35+
$model->setDescription('RIVIERA');
36+
$em->persist($model);
37+
38+
$em->flush();
39+
40+
$this->assertIsInt($model->getId());
41+
$this->assertInstanceOf(VehicleType::class, $model->getType());
42+
$this->assertInstanceOf(Make::class, $model->getMake());
43+
$this->assertSame('RIVIERA', $model->getDescription());
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Entity;
6+
7+
use App\Entity\SearchLog;
8+
use Codeception\Test\Unit;
9+
use Doctrine\ORM\EntityManagerInterface;
10+
use Laminas\Hydrator\ClassMethodsHydrator;
11+
use Test\UnitTester;
12+
13+
class SearchLogTest extends Unit
14+
{
15+
protected UnitTester $tester;
16+
17+
private array $logData = [
18+
'vehicle_type' => 'V',
19+
'make_abbr' => 'TESL',
20+
'found_models' => 500,
21+
'ip_address' => '127.0.0.1',
22+
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
23+
'request_time' => 100.0
24+
];
25+
26+
public function testSettersAndGetters(): void
27+
{
28+
$em = $this->tester->grabService(EntityManagerInterface::class);
29+
$searchLog = new SearchLog();
30+
$hydrator = new ClassMethodsHydrator();
31+
$hydrator->hydrate($this->logData, $searchLog);
32+
$em->persist($searchLog);
33+
$em->flush();
34+
35+
$data = $hydrator->extract($searchLog);
36+
unset($data['id']);
37+
$this->assertSame($this->logData, $data);
38+
}
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\Entity;
6+
7+
use App\Entity\VehicleType;
8+
use Codeception\Test\Unit;
9+
use Doctrine\ORM\EntityManagerInterface;
10+
use Laminas\Hydrator\ClassMethodsHydrator;
11+
use Test\UnitTester;
12+
13+
class VehicleTypeTest extends Unit
14+
{
15+
protected UnitTester $tester;
16+
17+
private array $vehicleTypeData = [
18+
'code' => 'V',
19+
'description' => 'Automobile'
20+
];
21+
22+
public function testSettersAndGetters(): void
23+
{
24+
$em = $this->tester->grabService(EntityManagerInterface::class);
25+
$searchLog = new VehicleType();
26+
$hydrator = new ClassMethodsHydrator();
27+
$hydrator->hydrate($this->vehicleTypeData, $searchLog);
28+
$em->persist($searchLog);
29+
$em->flush();
30+
31+
$this->assertEquals($this->vehicleTypeData['code'], $searchLog->getCode());
32+
$this->assertEquals($this->vehicleTypeData['description'], $searchLog->getDescription());
33+
}
34+
}

0 commit comments

Comments
 (0)