Skip to content

Commit

Permalink
Added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
foxycode committed Jan 10, 2021
1 parent c1007a6 commit 6f47ec4
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 2 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Tests

on:
- push
- pull_request

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php:
- 7.1
- 7.2
- 7.3
- 7.4

fail-fast: false

name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Add NewRelic APT source
run: echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | sudo tee /etc/apt/sources.list.d/newrelic.list
- name: Add NewRelic APT key
run: wget -O- https://download.newrelic.com/548C16BF.gpg | sudo apt-key add -
- name: Update APT
run: sudo apt-get update
- name: Install NewRelic PHP extension
run: sudo apt-get -y install newrelic-php5

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

- run: composer install --no-progress --prefer-dist
- run: composer tests
- if: failure()
run: for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
shell: bash


lowest_dependencies:
name: Lowest Dependencies
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v1
with:
php-version: 7.1
coverage: none

- name: Add NewRelic APT source
run: echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | sudo tee /etc/apt/sources.list.d/newrelic.list
- name: Add NewRelic APT key
run: wget -O- https://download.newrelic.com/548C16BF.gpg | sudo apt-key add -
- name: Update APT
run: sudo apt-get update
- name: Install NewRelic PHP extension
run: sudo apt-get -y install newrelic-php5

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

- run: composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable
- run: composer tests
- if: failure()
run: for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
shell: bash
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
"ninjify/coding-standard": "^0.10.0"
"ninjify/coding-standard": "^0.10.0",
"ninjify/nunjuck": "^0.3.0"
},
"autoload": {
"psr-4": {
"Contributte\\NewRelic\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"ContributteTests\\NewRelic\\": "tests"
}
},
"scripts": {
"lint": "vendor/bin/parallel-lint -e php,phpt src",
"cs": "vendor/bin/phpcs --standard=vendor/ninjify/coding-standard/ruleset-gamee.xml --extensions=php,phpt --tab-width=4 -sp src",
"csfix": "vendor/bin/phpcbf --standard=vendor/ninjify/coding-standard/ruleset-gamee.xml --extensions=php,phpt --tab-width=4 -sp src"
"csfix": "vendor/bin/phpcbf --standard=vendor/ninjify/coding-standard/ruleset-gamee.xml --extensions=php,phpt --tab-width=4 -sp src",
"tests": "vendor/bin/tester -s -p php --colors 1 -C tests/Cases"
},
"archive": {
"exclude": [
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tmp
81 changes: 81 additions & 0 deletions tests/Cases/NewRelicExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types = 1);

namespace ContributteTests\NewRelic\Cases;

use Contributte\NewRelic\Callbacks\OnErrorCallback;
use Contributte\NewRelic\Callbacks\OnRequestCallback;
use Contributte\NewRelic\DI\NewRelicExtension;
use ContributteTests\NewRelic\Mocks\ApplicationExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tester\TestCase;

require_once __DIR__ . '/../bootstrap.php';

/**
* @TestCase
*/
final class NewRelicExtensionTest extends TestCase
{

public function testExtension(): void
{
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addConfig([
'newrelic' => [
'enabled' => true,
'appName' => 'YourApplicationName',
'license' => 'yourLicenseCode',
'actionKey' => 'action',
'logLevel' => [
'critical',
'exception',
'error',
],
'rum' => [
'enabled' => 'true',
],
'transactionTracer' => [
'enabled' => true,
'detail' => 1,
'recordSql' => 'obfuscated',
'slowSql' => true,
'threshold' => 'apdex_f',
'stackTraceThreshold' => 500,
'explainThreshold' => 500,
],
'errorCollector' => [
'enabled' => true,
'recordDatabaseErrors' => true,
],
'parameters' => [
'capture' => false,
'ignored' => [],
],
'custom' => [
'parameters' => [
'paramName' => 'paramValue',
],
'tracers' => [],
],
],
]);
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('newrelic', new NewRelicExtension());
}, [getmypid(), 1]);

/** @var Container $container */
$container = new $class();

Assert::count(1, $container->findByType(OnRequestCallback::class));
Assert::count(1, $container->findByType(OnErrorCallback::class));
}

}

(new NewRelicExtensionTest())->run();
11 changes: 11 additions & 0 deletions tests/Mocks/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types = 1);

namespace ContributteTests\NewRelic\Mocks;

final class Application
{
public $onRequest = [];
public $onError = [];
}
27 changes: 27 additions & 0 deletions tests/Mocks/ApplicationExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace ContributteTests\NewRelic\Mocks;

use Nette;


/**
* Application extension for Nette DI.
*/
final class ApplicationExtension extends Nette\DI\CompilerExtension
{

public function loadConfiguration()
{
$config = $this->config;
$builder = $this->getContainerBuilder();

$builder->addDefinition('application')
->setFactory(Application::class);

$this->compiler->addExportedType(Application::class);
}

}
11 changes: 11 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

use Ninjify\Nunjuck\Environment;

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}

// Configure environment
Environment::setup(__DIR__);

0 comments on commit 6f47ec4

Please sign in to comment.