Skip to content

Commit

Permalink
Improve code and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Oct 8, 2019
1 parent 7e81c05 commit 46a875a
Show file tree
Hide file tree
Showing 31 changed files with 1,049 additions and 589 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:
run: node lib/install.js
env:
php-version: ${{ matrix.php-versions }}
extension-csv: "mbstring, xdebug, pcov" #optional
ini-values-csv: "post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata" #optional
extension-csv: mbstring, xdebug, pcov #optional
ini-values-csv: post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata #optional

- name: Testing PHP version
run: php -v
Expand Down
47 changes: 28 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<a href="https://www.codementor.io/shivammathur?utm_source=github&utm_medium=button&utm_term=shivammathur&utm_campaign=github"><img alt="Contact me on Codementor" src="https://cdn.codementor.io/badges/contact_me_github.svg"></a>
</p>

Setup PHP with required extensions, php.ini configuration and composer in [GitHub Actions](https://github.com/features/actions). This action can be added as a step in your action workflow and it will setup the PHP environment you need to test your application. Refer to [Usage](#memo-usage) section to see how to use this.
Setup PHP with required extensions, php.ini configuration and composer in [GitHub Actions](https://github.com/features/actions). This action can be added as a step in your action workflow and it will setup the PHP environment you need to test your application. Refer to [Usage](#memo-usage) section and [examples](#examples) to see how to use this.

## :tada: PHP Support

Expand All @@ -26,7 +26,7 @@ Setup PHP with required extensions, php.ini configuration and composer in [GitHu
|7.1|`Stable`|`Security fixes only`|
|7.2|`Stable`|`Active`|
|7.3|`Stable`|`Active`|
|7.4|`Beta`/`RC`|`Active`|
|7.4|`RC3`|`Active`|

**Note:** PHP 7.4 is currently in development, do not use in production/release branches.

Expand All @@ -40,7 +40,6 @@ Setup PHP with required extensions, php.ini configuration and composer in [GitHu
|Ubuntu 16.04|`ubuntu-16.04`|
|macOS X Mojave 10.14|`macOS-latest` or `macOS-10.14`|


## :wrench: PHP Extension Support
- On `ubuntu` extensions which have the package in apt are installed.
- On `windows` and `macOS` PECL extensions are installed.
Expand All @@ -53,10 +52,11 @@ Setup PHP with required extensions, php.ini configuration and composer in [GitHu

Specify `coverage: xdebug` to use `Xdebug`.
Runs on all [PHP versions supported](#tada-php-support)
```

```yaml
uses: shivammathur/setup-php@master
with:
php-version: 7.3
php-version: '7.3'
coverage: xdebug
```
Expand All @@ -65,20 +65,22 @@ with:
Specify `coverage: pcov` to use `PCOV`. `PCOV` is way faster than `Xdebug`.
For `pcov.directory` to be other than `src`, `lib` or, `app`, specify it using the `ini-values-csv` input.
`PCOV` needs `PHPUnit >= 8.0` and `PHP >= 7.1`, `PHPUnit` needs `PHP >= 7.2`. So use `PHP >= 7.2` with `PCOV`
```

```yaml
uses: shivammathur/setup-php@master
with:
php-version: 7.3
ini-values-csv: 'pcov.directory=api' #optional, see above for usage.
php-version: '7.3'
ini-values-csv: pcov.directory=api #optional, see above for usage.
coverage: pcov
```

### Disable coverage
Specify `coverage: none` to disable both `Xdebug` and `PCOV`.
```

```yaml
uses: shivammathur/setup-php@master
with:
php-version: 7.3
php-version: '7.3'
coverage: none
```

Expand All @@ -91,7 +93,7 @@ Inputs supported by this GitHub Action.
- ini-values-csv `optional`
- coverage `optional`

See [action.yml](action.yml) for more info
See [action.yml](action.yml) and usage below for more info.

### Basic Usage

Expand All @@ -102,9 +104,9 @@ steps:
- name: Installing PHP
uses: shivammathur/setup-php@master
with:
php-version: 7.3
php-version: '7.3'
extension-csv: mbstring, xdebug #optional
ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional
ini-values-csv: post_max_size=256M, short_open_tag=On #optional
coverage: xdebug #optional
- name: Check PHP Version
run: php -v
Expand Down Expand Up @@ -134,7 +136,7 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
extension-csv: mbstring, xdebug #optional
ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional
ini-values-csv: post_max_size=256M, short_open_tag=On #optional
coverage: xdebug #optional
- name: Check PHP Version
run: php -v
Expand All @@ -149,11 +151,18 @@ jobs:

Examples for setting up this GitHub Action with different PHP Frameworks/Packages.

**Note:** Make sure you add steps to run your tests after the setup steps given in these examples.

- [Laravel with MySQL](./examples/laravel-mysql.yml)
- [Laravel with PostgreSQL](./examples/laravel-postgres.yml)
- [Slim Framework](./examples/slim-framework.yml)
|Framework/Package|Workflow|
|--- |--- |
|CodeIgniter|[codeigniter.yml](./examples/codeigniter.yml)|
|Laravel `MySQL` `Redis`|[laravel-mysql.yml](./examples/laravel-mysql.yml)|
|Laravel `PostgreSQL` `Redis`|[laravel-postgres.yml](./examples/laravel-postgres.yml)|
|Laravel|[laravel.yml](./examples/laravel.yml)|
|Slim Framework|[slim-framework.yml](./examples/slim-framework.yml)|
|Symfony `MySQL`|[symfony-mysql.yml](./examples/symfony-mysql.yml)|
|Symfony `PostgreSQL`|[symfony-postgres.yml](./examples/symfony-postgres.yml)|
|Yii2 Starter Kit `MySQL`|[yii2-mysql.yml](./examples/yii2-mysql.yml)|
|Yii2 Starter Kit `PostgreSQL`|[yii2-postgres.yml](./examples/yii2-postgres.yml)|
|Zend Framework|[zend-framework.yml](./examples/zend-framework.yml)|

## :scroll: License

Expand Down
57 changes: 57 additions & 0 deletions __tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as config from '../src/config';

describe('Config tests', () => {
it('checking addINIValuesOnWindows', async () => {
let win32: string = await config.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'win32'
);
expect(win32).toContain(
'Add-Content C:\\tools\\php\\php.ini "post_max_size=256M"'
);
expect(win32).toContain(
'Add-Content C:\\tools\\php\\php.ini "short_open_tag=On"'
);
expect(win32).toContain(
'Add-Content C:\\tools\\php\\php.ini "date.timezone=Asia/Kolkata"'
);

win32 = await config.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'fedora'
);
expect(win32).toContain('Platform fedora is not supported');
});

it('checking addINIValuesOnLinux', async () => {
let linux: string = await config.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'linux'
);
expect(linux).toContain('echo "post_max_size=256M" >> $ini_file');
expect(linux).toContain('echo "short_open_tag=On" >> $ini_file');
expect(linux).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');

linux = await config.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'fedora'
);
expect(linux).toContain('Platform fedora is not supported');
});

it('checking addINIValuesOnDarwin', async () => {
let darwin: string = await config.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'darwin'
);
expect(darwin).toContain('echo "post_max_size=256M" >> $ini_file');
expect(darwin).toContain('echo "short_open_tag=On" >> $ini_file');
expect(darwin).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');

darwin = await config.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'fedora'
);
expect(darwin).toContain('Platform fedora is not supported');
});
});
80 changes: 80 additions & 0 deletions __tests__/coverage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as config from '../src/config';
import * as coverage from '../src/coverage';
import * as extensions from '../src/coverage';

jest.mock('../src/extensions', () => ({
addExtension: jest.fn().mockImplementation(extension => {
return 'addExtension ' + extension + '\n';
})
}));

describe('Config tests', () => {
it('checking addCoverage with PCOV on windows', async () => {
let win32: string = await coverage.addCoverage('pcov', '7.4', 'win32');
expect(win32).toContain('addExtension pcov');
expect(win32).toContain(
'if(php -m | findstr -i xdebug) { Disable-PhpExtension xdebug C:\\tools\\php'
);

win32 = await coverage.addCoverage('pcov', '7.0', 'win32');
expect(win32).toContain('PCOV requires PHP 7.1 or newer');

win32 = await coverage.addCoverage('pcov', '5.6', 'win32');
expect(win32).toContain('PCOV requires PHP 7.1 or newer');
});

it('checking addCoverage with PCOV on linux', async () => {
let linux: string = await coverage.addCoverage('pcov', '7.4', 'linux');
expect(linux).toContain('addExtension pcov');
expect(linux).toContain('sudo sed -i "/xdebug/d" $ini_file');
expect(linux).toContain('sudo phpdismod xdebug');
});

it('checking addCoverage with PCOV on darwin', async () => {
let darwin: string = await coverage.addCoverage('pcov', '7.4', 'darwin');
expect(darwin).toContain('addExtension pcov');
});

it('checking addCoverage with Xdebug on windows', async () => {
let win32: string = await coverage.addCoverage('xdebug', '7.3', 'win32');
expect(win32).toContain('addExtension xdebug');
});

it('checking addCoverage with Xdebug on linux', async () => {
let linux: string = await coverage.addCoverage('xdebug', '7.4', 'linux');
expect(linux).toContain('addExtension xdebug');
});

it('checking addCoverage with Xdebug on darwin', async () => {
let darwin: string = await coverage.addCoverage('xdebug', '7.4', 'darwin');
expect(darwin).toContain('addExtension xdebug');
});

it('checking disableCoverage windows', async () => {
let win32 = await coverage.addCoverage('none', '7.4', 'win32');
expect(win32).toContain('Disable-PhpExtension xdebug');
expect(win32).toContain('Disable-PhpExtension pcov');
});

it('checking disableCoverage on linux', async () => {
let linux: string = await coverage.addCoverage('none', '7.4', 'linux');
expect(linux).toContain('sudo phpdismod xdebug');
expect(linux).toContain('sudo phpdismod pcov');
expect(linux).toContain('sudo sed -i "/xdebug/d" $ini_file');
expect(linux).toContain('sudo sed -i "/pcov/d" $ini_file');
});

it('checking disableCoverage on darwin', async () => {
let darwin: string = await coverage.addCoverage('none', '7.4', 'darwin');
expect(darwin).toContain('sudo sed -i \'\' "/xdebug/d" $ini_file');
expect(darwin).toContain('sudo sed -i \'\' "/pcov/d" $ini_file');
});

it('checking no or invalid coverage driver', async () => {
let nocov: string = await coverage.addCoverage('nocov', '7.x', 'any');
expect(nocov).toEqual('');

nocov = await coverage.addCoverage('', '7.x', 'any');
expect(nocov).toEqual('');
});
});
92 changes: 92 additions & 0 deletions __tests__/extensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as extensions from '../src/extensions';

let valid_extensions = ['xdebug', 'pcov'];
jest.mock('../src/pecl', () => ({
checkPECLExtension: jest.fn().mockImplementation(extension => {
return valid_extensions.indexOf(extension) !== -1;
})
}));

describe('Extension tests', () => {
it('checking addExtensionOnWindows', async () => {
let win32: string = await extensions.addExtension(
'xdebug, pcov',
'7.2',
'win32'
);
expect(win32).toContain('Install-PhpExtension xdebug');
expect(win32).toContain('Install-PhpExtension pcov');
win32 = await extensions.addExtension('xdebug, pcov', '7.4', 'win32');
const extension_url: string =
'https://xdebug.org/files/php_xdebug-2.8.0beta2-7.4-vc15.dll';
expect(win32).toContain(
'Invoke-WebRequest -Uri ' +
extension_url +
' -OutFile C:\\tools\\php\\ext\\php_xdebug.dll'
);
expect(win32).toContain('Install-PhpExtension pcov');

win32 = await extensions.addExtension('does_not_exist', '7.2', 'win32');
expect(win32).toContain('Could not find does_not_exist for PHP7.2 on PECL');

win32 = await extensions.addExtension('xdebug', '7.2', 'fedora');
expect(win32).toContain('Platform fedora is not supported');
});

it('checking addExtensionOnLinux', async () => {
let linux: string = await extensions.addExtension(
'xdebug, pcov',
'7.2',
'linux'
);
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.2-xdebug'
);
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.2-pcov'
);

linux = await extensions.addExtension('xdebug, pcov', '7.4', 'linux');
expect(linux).toContain('./xdebug.sh');
expect(linux).toContain('./pcov.sh');

linux = await extensions.addExtension('xdebug', '7.2', 'fedora');
expect(linux).toContain('Platform fedora is not supported');
});

it('checking addExtensionOnDarwin', async () => {
let darwin: string = await extensions.addExtension(
'xdebug, pcov',
'7.2',
'darwin'
);
expect(darwin).toContain('sudo pecl install xdebug');
expect(darwin).toContain('sudo pecl install pcov');

darwin = await extensions.addExtension('pcov', '5.6', 'darwin');
expect(darwin).toContain('sudo pecl install pcov');

darwin = await extensions.addExtension('pcov', '7.2', 'darwin');
expect(darwin).toContain('sudo pecl install pcov');

darwin = await extensions.addExtension('xdebug', '5.6', 'darwin');
expect(darwin).toContain('sudo pecl install xdebug-2.5.5');

darwin = await extensions.addExtension('xdebug', '7.4', 'darwin');
expect(darwin).toContain('sh ./xdebug_darwin.sh');

darwin = await extensions.addExtension('pcov', '7.4', 'darwin');
expect(darwin).toContain('sh ./pcov.sh');

darwin = await extensions.addExtension('xdebug', '7.2', 'darwin');
expect(darwin).toContain('sudo pecl install xdebug');

darwin = await extensions.addExtension('does_not_exist', '7.2', 'darwin');
expect(darwin).toContain(
'Could not find does_not_exist for PHP7.2 on PECL'
);

darwin = await extensions.addExtension('xdebug', '7.2', 'fedora');
expect(darwin).toContain('Platform fedora is not supported');
});
});
Loading

0 comments on commit 46a875a

Please sign in to comment.