Skip to content

Commit 5bfc2d4

Browse files
authored
Merge pull request #8 from run-as-root/feature/unit-tests-coverage-lib
Unit tests coverage with GitHub Copilot
2 parents 7859a30 + 03a9bfd commit 5bfc2d4

File tree

13 files changed

+703
-50
lines changed

13 files changed

+703
-50
lines changed

.github/workflows/php.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
validate-composer:
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+
build:
24+
25+
runs-on: ubuntu-latest
26+
27+
needs: validate-composer
28+
29+
steps:
30+
- uses: actions/checkout@v3
31+
32+
- name: Cache Composer packages
33+
id: composer-cache
34+
uses: actions/cache@v3
35+
with:
36+
path: vendor
37+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
38+
restore-keys: |
39+
${{ runner.os }}-php-
40+
41+
- name: Install dependencies
42+
run: composer install --prefer-dist --no-progress
43+
44+
PHP-Compatibility:
45+
runs-on: ubuntu-latest
46+
47+
needs: build
48+
49+
steps:
50+
- uses: actions/checkout@v3
51+
52+
- name: Cache Composer packages
53+
id: composer-cache
54+
uses: actions/cache@v3
55+
with:
56+
path: vendor
57+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
58+
restore-keys: |
59+
${{ runner.os }}-php-
60+
61+
- name: PHP 7.4 compatibility
62+
run: composer sniffer:php7.4
63+
64+
- name: PHP 8.0 compatibility
65+
run: composer sniffer:php8.0
66+
67+
- name: PHP 8.1 compatibility
68+
run: composer sniffer:php8.1
69+
70+
PHP-Unit:
71+
runs-on: ubuntu-latest
72+
73+
needs: build
74+
75+
steps:
76+
- uses: actions/checkout@v3
77+
78+
- name: Cache Composer packages
79+
id: composer-cache
80+
uses: actions/cache@v3
81+
with:
82+
path: vendor
83+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
84+
restore-keys: |
85+
${{ runner.os }}-php-
86+
87+
- name: Setup PHP with Xdebug
88+
uses: shivammathur/setup-php@v2
89+
with:
90+
php-version: '8.1'
91+
coverage: xdebug
92+
93+
- name: PHP Unit
94+
run: composer phpunit
95+
96+
- name: phpunit-coverage-badge
97+
uses: timkrase/phpunit-coverage-badge@v1.2.0
98+
with:
99+
push_badge: true
100+
commit_message: "Update coverage badge"
101+
repo_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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
}
Lines changed: 111 additions & 0 deletions
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+
}
Lines changed: 62 additions & 0 deletions
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)