Skip to content
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

Add code coverage and test run via GitHub Action #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Tests

on:
# Run manually on demand.
workflow_dispatch:
# Run automatically when a pull request is created and on each push to it.
# The runtime is only ~ 30s due to parallelization.
pull_request:

jobs:
test:
name: 'Run unit tests'
runs-on: ubuntu-latest

strategy:
matrix:
php: [ '7.4', '8.2' ]

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Install dependencies (using the workflow cache)
uses: ramsey/composer-install@v2

- name: Run tests
run: |
./vendor/bin/phpunit --group unit
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ StackOverflow (with special tags) and put it as message into Slack channels.
```
composer install
SLACK_MESSAGE_STRUCTURE="<message-structure>" SLACK_WEBHOOK_URL="<webhook-url>" STACK_APPS_KEY="<app-key>" ./vendor/bin/phpunit --group end-to-end
```
```

### Code coverage

1. Download
2. Enable Xdebug
3. Run
```
composer install
SLACK_WEBHOOK_URL="<webhook-url>" php -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-clover <coverage-file>
```
4. Disable Xdebug.
5. Analyze the coverage file with your favorite IDE.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"require": {
"php": ">=7.4",
"ext-json": "*",
"ext-curl": "*"
},
Expand Down
7 changes: 6 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
62 changes: 45 additions & 17 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ abstract class AbstractTestCase extends TestCase
{
protected array $usedEnvVars = [];

protected string $testDirectory = '';

protected function setEnvVar($name, $value): void
{
putenv("$name=$value");
Expand All @@ -30,33 +32,59 @@ protected function createTestDirectory(): void
}
}

protected function addFileToTestDirectory(string $filename, string $content): void
protected function getTestDirectory(): string
{
file_put_contents($this->getPathOfTestDirectoryFile($filename), $content);
}
if ($this->testDirectory === '') {
$inheritingClassFQCN = get_class($this);
$inheritingClassName = substr($inheritingClassFQCN, strrpos($inheritingClassFQCN, '\\') + 1);

protected function getPathOfTestDirectoryFile(string $filename): string
{
return $this->getTestDirectory() . '/' . $filename;
}
try {
$inheritingClassDirectory = dirname((new \ReflectionClass($inheritingClassFQCN))->getFileName());
} catch (\ReflectionException $exception) {
$inheritingClassDirectory = __DIR__;
}

protected function getTestDirectory(): string
{
$inheritingClassFQCN = get_class($this);
$inheritingClassName = substr($inheritingClassFQCN, strrpos($inheritingClassFQCN, '\\') + 1);
return __DIR__ . '/' . $inheritingClassName;
$this->testDirectory = $inheritingClassDirectory.'/'.$inheritingClassName;
}

return $this->testDirectory;
}

protected function removeTestDirectory(): void
{
if (is_dir($this->getTestDirectory())) {
$files = glob($this->getTestDirectory() . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->getTestDirectory(), \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $path) {
if ($path->isDir() && !$path->isLink()) {
rmdir($path->getPathname());
} else {
unlink($path->getPathname());
}
}
rmdir($this->getTestDirectory());
}
}
}

protected function createDirectoryInTestDirectory(string $path): void
{
mkdir($this->getPathInTestDirectory($path), 0777, true);
}

protected function createFileInTestDirectory(string $path, string $content): void
{
file_put_contents($this->getPathInTestDirectory($path), $content);
}

protected function getPathInTestDirectory(string $path): string
{
return $this->getTestDirectory() . '/' . $path;
}

protected function getPathInTestDirectoryAsUrl(string $path): string
{
return 'file://' . $this->getPathInTestDirectory($path);
}
}
2 changes: 1 addition & 1 deletion tests/ConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function testEndToEnd(string $slackMessageStructure, array $expected): vo
$this->markTestSkipped(sprintf('Restrict end-to-end test to %s Slack message structure.', $filterSlackMessageStructure));
}

$lastExecutionFilename = $this->getPathOfTestDirectoryFile('last_execution.txt');
$lastExecutionFilename = $this->getPathInTestDirectory('last_execution.txt');
$this->assertFileDoesNotExist($lastExecutionFilename);

$connector = new Connector();
Expand Down