Skip to content

Commit

Permalink
Merge pull request #4 from denisyukphp/3.0.x
Browse files Browse the repository at this point in the history
Upgrade package
  • Loading branch information
denisyukphp authored Sep 12, 2022
2 parents cf82f05 + 089b635 commit 517b7aa
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 94 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ indent_size = 4

[*.{yml,yaml}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/README.md export-ignore
/build/ export-ignore
/composer.lock export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml export-ignore
Expand Down
115 changes: 91 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,108 @@
name: CI
name: 'CI'

on: ['push', 'pull_request']
on:
push:
paths-ignore:
- 'LICENSE'
- 'README.md'
pull_request:
paths-ignore:
- 'LICENSE'
- 'README.md'

jobs:
phpunit:
name: PHPUnit
php-parallel-lint:
name: 'PHPParallelLint'
runs-on: 'ubuntu-latest'

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: latest
coverage: none

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run PHPParallelLint
run: composer php-parallel-lint -- --no-progress --ignore-fails

psalm:
name: 'Psalm'
runs-on: 'ubuntu-latest'

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: latest
coverage: none

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run Psalm
run: composer psalm -- --no-progress --no-cache --output-format=github

php-cs-fixer:
name: 'PHPCsFixer'
runs-on: 'ubuntu-latest'

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: latest
coverage: none

- name: Install dependencies
uses: ramsey/composer-install@v2

- name: Run PHPCsFixer
run: composer php-cs-fixer:diff -- --no-interaction --using-cache=no

phpunit:
name: 'PHPUnit'
needs: ['php-parallel-lint', 'psalm', 'php-cs-fixer']
runs-on: ${{ matrix.operating-system }}

strategy:
fail-fast: false
matrix:
php-versions: ['8.0', '8.1']
stability: ['prefer-lowest', 'prefer-stable']
operating-system:
- 'ubuntu-latest'
- 'windows-latest'
php-version:
- '8.0'
- '8.1'
composer-dependency:
- 'lowest'
- 'highest'

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache Composer
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: composer:v2
php-version: ${{ matrix.php-version }}
coverage: none

- name: Update Composer
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
- name: Install dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.composer-dependency }}

- name: Run PHPUnit
run: vendor/bin/phpunit --verbose --no-interaction
run: composer phpunit -- --no-interaction --do-not-cache-result
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/.idea/
/.php-cs-fixer.php
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
/phpunit.xml
/psalm.xml
/vendor/
9 changes: 4 additions & 5 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

$finder = (new PhpCsFixer\Finder())
->in([
'./src/',
'./tests/'
__DIR__.'/src/',
__DIR__.'/tests/',
])
;

return (new PhpCsFixer\Config())
->setUsingCache(false)
->setUsingCache(true)
->setCacheFile(__DIR__.'/build/cache/php-cs-fixer.cache')
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'strict_param' => true,
'single_line_throw' => false,
])
->setFinder($finder)
;
2 changes: 0 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
The MIT License (MIT)

Copyright (c) 2022 Aleksandr Denisyuk

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TmpFile

[![Build Status](https://img.shields.io/github/workflow/status/denisyukphp/tmpfile/build/master?style=plastic)](https://github.com/denisyukphp/tmpfile/actions/workflows/continuous-integration.yml)
[![Build Status](https://img.shields.io/github/workflow/status/denisyukphp/tmpfile/build/master?style=plastic)](https://github.com/denisyukphp/tmpfile/actions/workflows/ci.yml)
[![Latest Stable Version](https://img.shields.io/packagist/v/denisyukphp/tmpfile?style=plastic)](https://packagist.org/packages/denisyukphp/tmpfile)
[![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/denisyukphp/tmpfile?style=plastic&color=8892BF)](https://packagist.org/packages/denisyukphp/tmpfile)
[![Total Downloads](https://img.shields.io/packagist/dt/denisyukphp/tmpfile?style=plastic)](https://packagist.org/packages/denisyukphp/tmpfile)
Expand Down
2 changes: 2 additions & 0 deletions build/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "denisyukphp/tmpfile",
"description": "Alternative to tmpfile() function",
"description": "Alternative to tmpfile() function.",
"keywords": [
"tmpfile",
"tempnam",
Expand All @@ -25,8 +25,10 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.10",
"php-parallel-lint/php-console-highlighter": "^1.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpunit/phpunit": "^9.5",
"symfony/process": "^6.0",
"psalm/plugin-phpunit": "^0.17.0",
"vimeo/psalm": "^4.26"
},
"suggest": {
Expand All @@ -43,9 +45,17 @@
}
},
"scripts": {
"phpunit": "./vendor/bin/phpunit --verbose --colors=always",
"php-cs-fixer:fix": "./vendor/bin/php-cs-fixer fix --verbose --show-progress=dots",
"php-cs-fixer:diff": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --diff",
"psalm": "./vendor/bin/psalm --show-info=true"
"phpunit": "./vendor/bin/phpunit --verbose --colors=always --no-coverage",
"phpunit-coverage": "./vendor/bin/phpunit --verbose --colors=always --coverage-text",
"php-parallel-lint": "./vendor/bin/parallel-lint --colors ./src/ ./tests/",
"php-cs-fixer:fix": "./vendor/bin/php-cs-fixer fix --ansi --verbose --show-progress=dots",
"php-cs-fixer:diff": "./vendor/bin/php-cs-fixer fix --ansi --verbose --dry-run --diff",
"psalm": "./vendor/bin/psalm --show-info=true",
"test": [
"@php-parallel-lint",
"@psalm",
"@php-cs-fixer:diff",
"@phpunit"
]
}
}
19 changes: 15 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
cacheResultFile="./build/cache/phpunit.cache"
>
<testsuites>
<testsuite name="TmpFile">
<testsuite name="tmpfile">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<coverage
processUncoveredFiles="true"
cacheDirectory="./build/cache/phpunit-coverage/"
>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
</phpunit>
19 changes: 19 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" ?>
<psalm
errorLevel="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config ./vendor/vimeo/psalm/config.xsd"
cacheDirectory="./build/cache/psalm/"
>
<projectFiles>
<directory name="./src/" />
<directory name="./tests/" />
<ignoreFiles>
<directory name="./vendor/" />
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin" />
</plugins>
</psalm>
9 changes: 0 additions & 9 deletions psalm.xml.dist

This file was deleted.

20 changes: 15 additions & 5 deletions src/TmpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ final class TmpFile implements TmpFileInterface

public function __construct()
{
$this->filename = tempnam(sys_get_temp_dir(), 'php');

if (false === $this->filename) {
throw new \RuntimeException("tempnam() couldn't create a temp file.");
}
$this->filename = $this->createTmpFile();

$this->handler = static function (string $filename): void {
if (file_exists($filename)) {
Expand All @@ -26,6 +22,20 @@ public function __construct()
register_shutdown_function($this->handler, $this->filename);
}

/**
* @codeCoverageIgnore
*/
private function createTmpFile(): string
{
$filename = tempnam(sys_get_temp_dir(), 'php');

if (false === $filename) {
throw new \RuntimeException("tempnam() couldn't create a temp file.");
}

return $filename;
}

public function getFilename(): string
{
return $this->filename;
Expand Down
39 changes: 4 additions & 35 deletions tests/TmpFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace TmpFile\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\PhpProcess;
use TmpFile\TmpFile;

class TmpFileTest extends TestCase
{
public function testCreateTmpFile(): void
public function testCreateTmpFileAndReturnFilename(): void
{
$tmpFile = new TmpFile();

Expand All @@ -19,40 +18,10 @@ public function testCreateTmpFile(): void

public function testRemoveTmpFileOnGarbageCollection(): void
{
$filename = '';
$filename = (function (): string {
return (new TmpFile())->getFilename();
})();

$callback = function () use (&$filename): void {
$filename = (string) new TmpFile();
};

$callback();

$this->assertNotEmpty($filename);
$this->assertFileDoesNotExist($filename);
}

public function testRemoveTmpFileOnFatalError(): void
{
$fatalErrorUseCase = <<<'EOF'
<?php
require_once __DIR__ . './../vendor/autoload.php';
use TmpFile\TmpFile;
$tmpFile = new TmpFile();
echo $tmpFile->getFilename();
trigger_error('Fatal error!', E_USER_ERROR);
EOF;

$process = new PhpProcess($fatalErrorUseCase, __DIR__);
$process->run();
$output = $process->getOutput();
$data = explode(\PHP_EOL, $output);

$this->assertMatchesRegularExpression('~'.sys_get_temp_dir().'~', $data[0]);
$this->assertFileDoesNotExist($data[0]);
}
}

0 comments on commit 517b7aa

Please sign in to comment.