Skip to content

Commit 9917f0a

Browse files
committed
Unit tests coverage with GitHub Copilot
1 parent 7859a30 commit 9917f0a

File tree

13 files changed

+694
-50
lines changed

13 files changed

+694
-50
lines changed

.github/workflows/php.yml

-42
This file was deleted.

.github/workflows/test_extension.yml

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Test Extension
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Validate composer.json and composer.lock
21+
run: composer validate
22+
23+
- name: Cache Composer packages
24+
id: composer-cache
25+
uses: actions/cache@v3
26+
with:
27+
path: vendor
28+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
29+
restore-keys: |
30+
${{ runner.os }}-php-
31+
32+
- name: Install dependencies
33+
run: composer install --prefer-dist --no-progress
34+
35+
PHP-Compatibility:
36+
runs-on: ubuntu-latest
37+
38+
needs: build
39+
40+
steps:
41+
- uses: actions/checkout@v3
42+
43+
- name: Cache Composer packages
44+
id: composer-cache
45+
uses: actions/cache@v3
46+
with:
47+
path: vendor
48+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
49+
restore-keys: |
50+
${{ runner.os }}-php-
51+
52+
- name: PHP 7.4 compatibility
53+
run: composer sniffer:php7.4
54+
55+
- name: PHP 8.0 compatibility
56+
run: composer sniffer:php8.0
57+
58+
- name: PHP 8.1 compatibility
59+
run: composer sniffer:php8.1
60+
61+
PHP-Unit:
62+
runs-on: ubuntu-latest
63+
64+
needs: build
65+
66+
steps:
67+
- uses: actions/checkout@v3
68+
69+
- name: Cache Composer packages
70+
id: composer-cache
71+
uses: actions/cache@v3
72+
with:
73+
path: vendor
74+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
75+
restore-keys: |
76+
${{ runner.os }}-php-
77+
78+
- name: Setup PHP with Xdebug
79+
uses: shivammathur/setup-php@v2
80+
with:
81+
php-version: '8.1'
82+
coverage: xdebug
83+
84+
- name: PHP Unit
85+
run: composer phpunit
86+
87+
- name: phpunit-coverage-badge
88+
uses: timkrase/[email protected]
89+
with:
90+
push_badge: true
91+
commit_message: "Update coverage badge"
92+
repo_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.editorconfig
44
vendor/
55
composer.lock
6+
.phpunit.result.cache

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![PHP](https://img.shields.io/badge/php-8.1-blue)
55
![composer](https://shields.io/badge/composer-v2-darkgreen)
66
![packagist](https://img.shields.io/badge/packagist-f28d1a)
7-
![build](https://github.com/run-as-root/magento-cli-auto-proxy/actions/workflows/php.yml/badge.svg)
7+
![build](https://github.com/run-as-root/magento-cli-auto-proxy/actions/workflows/test_extension.yml/badge.svg)
88

99
<br />
1010
<div align="center">
@@ -45,7 +45,7 @@ composer req run_as_root/magento-cli-auto-proxy:^1
4545
- [x] MVP release
4646
- [x] Documentation
4747
- [x] PHP 8 support (most likely supported already :suspect: )
48-
- [ ] Unit tests coverage
48+
- [x] Unit tests coverage
4949
- [ ] Static tests coverage
5050
- [ ] php linting
5151
- [ ] phpcs

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"roave/security-advisories": "dev-latest",
1212
"squizlabs/php_codesniffer": "^3.7",
1313
"phpcompatibility/php-compatibility": "^9.3",
14-
"phpstan/phpstan": "^1.9"
14+
"phpstan/phpstan": "^1.9",
15+
"phpunit/phpunit": ">9"
1516
},
1617
"repositories": [
1718
{
@@ -44,6 +45,7 @@
4445
"sniffer:php7.4": "phpcs -p ./lib --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 7.4",
4546
"sniffer:php8.0": "phpcs -p ./lib --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.0",
4647
"sniffer:php8.1": "phpcs -p ./lib --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.1",
47-
"phpstan": "phpstan"
48+
"phpstan": "phpstan",
49+
"phpunit": "vendor/bin/phpunit -c phpunit.xml"
4850
}
4951
}

lib/Plugin/Dom/EnrichCliConfigWithProxyPlugin.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Psr\Log\LoggerInterface;
77
use ReflectionException;
8+
use RunAsRoot\CliConstructorArgAutoProxy\Preference\Framework\ObjectManager\Config\Reader\Dom\Interceptor;
89
use RunAsRoot\CliConstructorArgAutoProxy\Service\EnrichCliConfigWithProxyService;
910

1011
class EnrichCliConfigWithProxyPlugin
@@ -23,7 +24,7 @@ public function __construct(
2324
/**
2425
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2526
*/
26-
public function afterRead($subject, array $result, ?string $scope): array
27+
public function afterRead(Interceptor $subject, array $result, ?string $scope): array
2728
{
2829
if ($scope !== 'global') {
2930
return $result;

lib/Preference/Framework/ObjectManager/Config/Reader/Dom/Interceptor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function read($scope = null)
2222
*/
2323
private function runPlugin(array $result, ?string $scope): array
2424
{
25-
/** @var EnrichCliConfigWithProxyPlugin $enrichService */
26-
$enrichService = ObjectManager::getInstance()->get(EnrichCliConfigWithProxyPlugin::class);
27-
return $enrichService->afterRead($this, $result, $scope);
25+
/** @var EnrichCliConfigWithProxyPlugin $enrichPlugin */
26+
$enrichPlugin = ObjectManager::getInstance()->get(EnrichCliConfigWithProxyPlugin::class);
27+
return $enrichPlugin->afterRead($this, $result, $scope);
2828
}
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace RunAsRoot\CliConstructorArgAutoProxy\Test\Unit\Map;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use RunAsRoot\CliConstructorArgAutoProxy\Mapper\ProxiedConstructArgsToDiConfigMapper;
8+
9+
/**
10+
* Powered by GitHub Copilot
11+
*/
12+
final class ProxiedConstructArgsToDiConfigMapperTest extends TestCase
13+
{
14+
private ProxiedConstructArgsToDiConfigMapper $sut;
15+
16+
protected function setUp(): void
17+
{
18+
$this->sut = new ProxiedConstructArgsToDiConfigMapper();
19+
}
20+
21+
/**
22+
* @dataProvider mapDataProvider
23+
*/
24+
public function test_map(array $diConfig, string $instanceClassName, array $proxiedConstructArgsConfig, array $expected): void
25+
{
26+
$result = $this->sut->map($diConfig, $instanceClassName, $proxiedConstructArgsConfig);
27+
$this->assertEquals($expected, $result);
28+
}
29+
30+
public function mapDataProvider(): array
31+
{
32+
return [
33+
'case1' => [
34+
'diConfig' => [
35+
'instance1' => [
36+
'arguments' => [
37+
'arg1' => 'value1',
38+
'arg2' => 'value2',
39+
],
40+
],
41+
],
42+
'instanceClassName' => 'instance1',
43+
'proxiedConstructArgsConfig' => [
44+
'arg3' => 'value3',
45+
'arg4' => 'value4',
46+
],
47+
'expected' => [
48+
'instance1' => [
49+
'arguments' => [
50+
'arg1' => 'value1',
51+
'arg2' => 'value2',
52+
'arg3' => 'value3',
53+
'arg4' => 'value4',
54+
],
55+
],
56+
],
57+
],
58+
'case2' => [
59+
'diConfig' => [
60+
'instance1' => [],
61+
],
62+
'instanceClassName' => 'instance1',
63+
'proxiedConstructArgsConfig' => [
64+
'arg3' => 'value3',
65+
'arg4' => 'value4',
66+
],
67+
'expected' => [
68+
'instance1' => [
69+
'arguments' => [
70+
'arg4' => 'value4',
71+
'arg3' => 'value3',
72+
],
73+
],
74+
]
75+
],
76+
'case3' => [
77+
'diConfig' => [
78+
'instance1' => [
79+
'arguments' => [
80+
'arg1' => 'value1',
81+
'arg2' => 'value2',
82+
],
83+
],
84+
],
85+
'instanceClassName' => 'instance1',
86+
'proxiedConstructArgsConfig' => [],
87+
'expected' => [
88+
'instance1' => [
89+
'arguments' => [
90+
'arg1' => 'value1',
91+
'arg2' => 'value2',
92+
],
93+
],
94+
],
95+
],
96+
'case4' => [
97+
'diConfig' => [
98+
'instance1' => [],
99+
],
100+
'instanceClassName' => 'instance1',
101+
'proxiedConstructArgsConfig' => [],
102+
'expected' => [
103+
'instance1' => [
104+
'arguments' => [],
105+
],
106+
],
107+
],
108+
109+
];
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace RunAsRoot\CliConstructorArgAutoProxy\Test\Unit\Plugin\Dom;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Log\LoggerInterface;
8+
use RunAsRoot\CliConstructorArgAutoProxy\Plugin\Dom\EnrichCliConfigWithProxyPlugin;
9+
use RunAsRoot\CliConstructorArgAutoProxy\Preference\Framework\ObjectManager\Config\Reader\Dom\Interceptor;
10+
use RunAsRoot\CliConstructorArgAutoProxy\Service\EnrichCliConfigWithProxyService;
11+
12+
final class EnrichCliConfigWithProxyPluginTest extends TestCase
13+
{
14+
private EnrichCliConfigWithProxyService $service;
15+
private LoggerInterface $logger;
16+
private EnrichCliConfigWithProxyPlugin $sut;
17+
18+
protected function setUp(): void
19+
{
20+
$this->service = $this->createMock(EnrichCliConfigWithProxyService::class);
21+
$this->logger = $this->createMock(LoggerInterface::class);
22+
$this->sut = new EnrichCliConfigWithProxyPlugin($this->service, $this->logger);
23+
}
24+
25+
public function test_after_read(): void
26+
{
27+
$subject = $this->createMock(Interceptor::class);
28+
$result = ['foo' => 'bar'];
29+
$scope = 'global';
30+
31+
$this->service->expects($this->once())->method('execute')->with($result)
32+
->willReturn(['abc' => 'def']);
33+
$this->logger->expects($this->never())->method('error');
34+
35+
$this->assertSame(['abc' => 'def'], $this->sut->afterRead($subject, $result, $scope));
36+
}
37+
38+
public function test_after_read_with_non_global_scope(): void
39+
{
40+
$subject = $this->createMock(Interceptor::class);
41+
$result = ['foo' => 'bar'];
42+
$scope = 'foo';
43+
44+
$this->service->expects($this->never())->method('execute');
45+
$this->logger->expects($this->never())->method('error');
46+
47+
$this->assertSame($result, $this->sut->afterRead($subject, $result, $scope));
48+
}
49+
50+
public function test_after_read_with_exception(): void
51+
{
52+
$subject = $this->createMock(Interceptor::class);
53+
$result = ['foo' => 'bar'];
54+
$scope = 'global';
55+
56+
$this->service->expects($this->once())->method('execute')
57+
->with($result)->willThrowException(new \ReflectionException('foo'));
58+
$this->logger->expects($this->once())->method('error');
59+
60+
$this->assertSame($result, $this->sut->afterRead($subject, $result, $scope));
61+
}
62+
}

0 commit comments

Comments
 (0)