Skip to content

Unit tests coverage with GitHub Copilot #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions .github/workflows/php.yml

This file was deleted.

101 changes: 101 additions & 0 deletions .github/workflows/test_extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Test Extension

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
validate-composer:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate

build:

runs-on: ubuntu-latest

needs: validate-composer

steps:
- uses: actions/checkout@v3

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

PHP-Compatibility:
runs-on: ubuntu-latest

needs: build

steps:
- uses: actions/checkout@v3

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: PHP 7.4 compatibility
run: composer sniffer:php7.4

- name: PHP 8.0 compatibility
run: composer sniffer:php8.0

- name: PHP 8.1 compatibility
run: composer sniffer:php8.1

PHP-Unit:
runs-on: ubuntu-latest

needs: build

steps:
- uses: actions/checkout@v3

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Setup PHP with Xdebug
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: xdebug

- name: PHP Unit
run: composer phpunit

- name: phpunit-coverage-badge
uses: timkrase/phpunit-coverage-badge@v1.2.0
with:
push_badge: true
commit_message: "Update coverage badge"
repo_token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.editorconfig
vendor/
composer.lock
.phpunit.result.cache
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![PHP](https://img.shields.io/badge/php-8.1-blue)
![composer](https://shields.io/badge/composer-v2-darkgreen)
![packagist](https://img.shields.io/badge/packagist-f28d1a)
![build](https://github.com/run-as-root/magento-cli-auto-proxy/actions/workflows/php.yml/badge.svg)
![build](https://github.com/run-as-root/magento-cli-auto-proxy/actions/workflows/test_extension.yml/badge.svg)

<br />
<div align="center">
Expand Down Expand Up @@ -45,7 +45,7 @@ composer req run_as_root/magento-cli-auto-proxy:^1
- [x] MVP release
- [x] Documentation
- [x] PHP 8 support (most likely supported already :suspect: )
- [ ] Unit tests coverage
- [x] Unit tests coverage
- [ ] Static tests coverage
- [ ] php linting
- [ ] phpcs
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"roave/security-advisories": "dev-latest",
"squizlabs/php_codesniffer": "^3.7",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.9"
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": ">9"
},
"repositories": [
{
Expand Down Expand Up @@ -44,6 +45,7 @@
"sniffer:php7.4": "phpcs -p ./lib --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 7.4",
"sniffer:php8.0": "phpcs -p ./lib --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.0",
"sniffer:php8.1": "phpcs -p ./lib --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.1",
"phpstan": "phpstan"
"phpstan": "phpstan",
"phpunit": "vendor/bin/phpunit -c phpunit.xml"
}
}
3 changes: 2 additions & 1 deletion lib/Plugin/Dom/EnrichCliConfigWithProxyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Psr\Log\LoggerInterface;
use ReflectionException;
use RunAsRoot\CliConstructorArgAutoProxy\Preference\Framework\ObjectManager\Config\Reader\Dom\Interceptor;
use RunAsRoot\CliConstructorArgAutoProxy\Service\EnrichCliConfigWithProxyService;

class EnrichCliConfigWithProxyPlugin
Expand All @@ -23,7 +24,7 @@ public function __construct(
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterRead($subject, array $result, ?string $scope): array
public function afterRead(Interceptor $subject, array $result, ?string $scope): array
{
if ($scope !== 'global') {
return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function read($scope = null)
*/
private function runPlugin(array $result, ?string $scope): array
{
/** @var EnrichCliConfigWithProxyPlugin $enrichService */
$enrichService = ObjectManager::getInstance()->get(EnrichCliConfigWithProxyPlugin::class);
return $enrichService->afterRead($this, $result, $scope);
/** @var EnrichCliConfigWithProxyPlugin $enrichPlugin */
$enrichPlugin = ObjectManager::getInstance()->get(EnrichCliConfigWithProxyPlugin::class);
return $enrichPlugin->afterRead($this, $result, $scope);
}
}
111 changes: 111 additions & 0 deletions lib/Test/Unit/Map/ProxiedConstructArgsToDiConfigMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);

namespace RunAsRoot\CliConstructorArgAutoProxy\Test\Unit\Map;

use PHPUnit\Framework\TestCase;
use RunAsRoot\CliConstructorArgAutoProxy\Mapper\ProxiedConstructArgsToDiConfigMapper;

/**
* Powered by GitHub Copilot
*/
final class ProxiedConstructArgsToDiConfigMapperTest extends TestCase
{
private ProxiedConstructArgsToDiConfigMapper $sut;

protected function setUp(): void
{
$this->sut = new ProxiedConstructArgsToDiConfigMapper();
}

/**
* @dataProvider mapDataProvider
*/
public function test_map(array $diConfig, string $instanceClassName, array $proxiedConstructArgsConfig, array $expected): void
{
$result = $this->sut->map($diConfig, $instanceClassName, $proxiedConstructArgsConfig);
$this->assertEquals($expected, $result);
}

public function mapDataProvider(): array
{
return [
'case1' => [
'diConfig' => [
'instance1' => [
'arguments' => [
'arg1' => 'value1',
'arg2' => 'value2',
],
],
],
'instanceClassName' => 'instance1',
'proxiedConstructArgsConfig' => [
'arg3' => 'value3',
'arg4' => 'value4',
],
'expected' => [
'instance1' => [
'arguments' => [
'arg1' => 'value1',
'arg2' => 'value2',
'arg3' => 'value3',
'arg4' => 'value4',
],
],
],
],
'case2' => [
'diConfig' => [
'instance1' => [],
],
'instanceClassName' => 'instance1',
'proxiedConstructArgsConfig' => [
'arg3' => 'value3',
'arg4' => 'value4',
],
'expected' => [
'instance1' => [
'arguments' => [
'arg4' => 'value4',
'arg3' => 'value3',
],
],
]
],
'case3' => [
'diConfig' => [
'instance1' => [
'arguments' => [
'arg1' => 'value1',
'arg2' => 'value2',
],
],
],
'instanceClassName' => 'instance1',
'proxiedConstructArgsConfig' => [],
'expected' => [
'instance1' => [
'arguments' => [
'arg1' => 'value1',
'arg2' => 'value2',
],
],
],
],
'case4' => [
'diConfig' => [
'instance1' => [],
],
'instanceClassName' => 'instance1',
'proxiedConstructArgsConfig' => [],
'expected' => [
'instance1' => [
'arguments' => [],
],
],
],

];
}
}
62 changes: 62 additions & 0 deletions lib/Test/Unit/Plugin/Dom/EnrichCliConfigWithProxyPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace RunAsRoot\CliConstructorArgAutoProxy\Test\Unit\Plugin\Dom;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use RunAsRoot\CliConstructorArgAutoProxy\Plugin\Dom\EnrichCliConfigWithProxyPlugin;
use RunAsRoot\CliConstructorArgAutoProxy\Preference\Framework\ObjectManager\Config\Reader\Dom\Interceptor;
use RunAsRoot\CliConstructorArgAutoProxy\Service\EnrichCliConfigWithProxyService;

final class EnrichCliConfigWithProxyPluginTest extends TestCase
{
private EnrichCliConfigWithProxyService $service;
private LoggerInterface $logger;
private EnrichCliConfigWithProxyPlugin $sut;

protected function setUp(): void
{
$this->service = $this->createMock(EnrichCliConfigWithProxyService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->sut = new EnrichCliConfigWithProxyPlugin($this->service, $this->logger);
}

public function test_after_read(): void
{
$subject = $this->createMock(Interceptor::class);
$result = ['foo' => 'bar'];
$scope = 'global';

$this->service->expects($this->once())->method('execute')->with($result)
->willReturn(['abc' => 'def']);
$this->logger->expects($this->never())->method('error');

$this->assertSame(['abc' => 'def'], $this->sut->afterRead($subject, $result, $scope));
}

public function test_after_read_with_non_global_scope(): void
{
$subject = $this->createMock(Interceptor::class);
$result = ['foo' => 'bar'];
$scope = 'foo';

$this->service->expects($this->never())->method('execute');
$this->logger->expects($this->never())->method('error');

$this->assertSame($result, $this->sut->afterRead($subject, $result, $scope));
}

public function test_after_read_with_exception(): void
{
$subject = $this->createMock(Interceptor::class);
$result = ['foo' => 'bar'];
$scope = 'global';

$this->service->expects($this->once())->method('execute')
->with($result)->willThrowException(new \ReflectionException('foo'));
$this->logger->expects($this->once())->method('error');

$this->assertSame($result, $this->sut->afterRead($subject, $result, $scope));
}
}
Loading