Skip to content

Commit

Permalink
Backport #533 to 1.0 (#539)
Browse files Browse the repository at this point in the history
Refs: #533

Includes testing suite changes done in "develop"

Co-authored-by: Ben Thomson <git@alfreido.com>
  • Loading branch information
Luke Towers and Ben Thomson committed Nov 20, 2020
1 parent 425b9f5 commit 16d8346
Show file tree
Hide file tree
Showing 15 changed files with 542 additions and 119 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/code-quality-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ jobs:
name: PHP
steps:
- name: Checkout changes
uses: actions/checkout@v1
- name: Install PHP
uses: shivammathur/setup-php@master
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install PHP and PHP Code Sniffer
uses: shivammathur/setup-php@v2
with:
php-version: 7.2
- name: Install Composer dependencies
run: composer install --no-interaction --no-progress --no-suggest
tools: phpcs
- name: Run code quality checks
run: |
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" && git fetch
./vendor/bin/phpcs --ignore=src/Parse/Assetic/Less/* --colors -nq --report="full" --extensions="php" $(git diff --name-only --diff-filter=ACMR origin/${{ github.base_ref }} HEAD)
run: ./.github/workflows/utilities/phpcs-pr ${{ github.base_ref }}
18 changes: 10 additions & 8 deletions .github/workflows/code-quality-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Code Quality
on:
push:
branches:
- master
- 1.0
- 1.1
- develop

jobs:
Expand All @@ -12,12 +13,13 @@ jobs:
name: PHP
steps:
- name: Checkout changes
uses: actions/checkout@v1
- name: Install PHP
uses: shivammathur/setup-php@master
uses: actions/checkout@v2
with:
php-version: 7.2
- name: Install Composer dependencies
run: composer install --no-interaction --no-progress --no-suggest
fetch-depth: 0
- name: Install PHP and PHP Code Sniffer
uses: shivammathur/setup-php@v2
with:
php-version: '7.3'
tools: phpcs
- name: Run code quality checks
run: ./vendor/bin/phpcs --ignore=src/Parse/Assetic/Less/* --colors -nq --report="full" --extensions="php" $(git show --name-only --pretty="" --diff-filter=ACMR ${{ github.sha }})
run: ./.github/workflows/utilities/phpcs-push ${{ github.sha }}
63 changes: 51 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,68 @@ name: Tests
on:
push:
branches:
- master
- 1.0
- 1.1
- develop
pull_request:


jobs:
phpUnitTests:
runs-on: ubuntu-latest
unitTests:
strategy:
max-parallel: 6
matrix:
phpVersions: ['7.1', '7.2', '7.3']
operatingSystem: ['ubuntu-latest']
phpVersion: ['7.2']
fail-fast: false
name: PHP ${{ matrix.phpVersions }}
runs-on: ${{ matrix.operatingSystem }}
name: ${{ matrix.operatingSystem }} / PHP ${{ matrix.phpVersion }}
env:
extensions: curl, fileinfo, gd, mbstring, openssl, pdo, pdo_sqlite, sqlite3, xml
key: october-rain-cache-v1
steps:
- name: Checkout changes
uses: actions/checkout@v1
uses: actions/checkout@v2

- name: Setup extension cache
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ matrix.phpVersion }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}

- name: Cache extensions
uses: actions/cache@v2
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}

- name: Install PHP
uses: shivammathur/setup-php@master
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.phpVersions }}
php-version: ${{ matrix.phpVersion }}
tools: composer
extensions: ${{ env.extensions }}

- name: Setup dependency cache
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-interaction --no-progress --no-suggest
- name: Run Linting and Tests
run: |
./vendor/bin/parallel-lint --exclude vendor .
./vendor/bin/phpunit ./tests

- name: Setup problem matchers for PHPUnit
if: matrix.phpVersion == '7.2'
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run tests
run: ./vendor/bin/phpunit ./tests
77 changes: 77 additions & 0 deletions .github/workflows/utilities/phpcs-pr
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env php
<?php
if (empty($argv[1])) {
echo 'You must provide a base branch to check this PR against.';
echo "\n";
exit(1);
}

// Get a changelist of files from Git for this PR.
$fileList = shell_exec('git diff --name-only --diff-filter=ACMR origin/' . $argv[1] . ' HEAD');
$files = array_filter(explode("\n", $fileList));

foreach ($files as &$file) {
if (strpos($file, ' ') !== false) {
$file = str_replace(' ', '\\ ', $file);
}
}

// Run all changed files through the PHPCS code sniffer and generate a CSV report
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
$lines = array_map(function ($row) {
return array_map(function ($column) {
return trim($column, '"');
}, explode(',', $row));
}, array_filter(explode("\n", $csv)));

// Remove header row
array_shift($lines);

if (!count($lines)) {
echo "\e[0;32mFound no issues with code quality.\e[0m";
echo "\n";
exit(0);
} else {
// Group errors by file
$files = [];

foreach ($lines as $line) {
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);

if (empty($files[$filename])) {
$files[$filename] = [];
}

$files[$filename][] = [
'warning' => ($line[3] === 'warning'),
'message' => $line[4],
'line' => $line[1],
];
}

// Render report
echo "\e[0;31mFound "
. ((count($lines) === 1)
? '1 issue'
: count($lines) . ' issues')
. " with code quality.\e[0m";
echo "\n";

foreach ($files as $file => $errors) {
echo "\n";
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
echo "\n\n";

foreach ($errors as $error) {
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
if ($error['warning'] === false) {
echo "\e[0;31mERR:\e[0m ";
} else {
echo "\e[1;33mWARN:\e[0m ";
}
echo $error['message'];
echo "\n";
}
}
exit(1);
}
77 changes: 77 additions & 0 deletions .github/workflows/utilities/phpcs-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env php
<?php
if (empty($argv[1])) {
echo 'You must provide a commit SHA to check.';
echo "\n";
exit(1);
}

// Get a changelist of files from Git for this push.
$fileList = shell_exec('git show --name-only --pretty="" --diff-filter=ACMR ' . $argv[1]);
$files = array_filter(explode("\n", $fileList));

foreach ($files as &$file) {
if (strpos($file, ' ') !== false) {
$file = str_replace(' ', '\\ ', $file);
}
}

// Run all changed files through the PHPCS code sniffer and generate a CSV report
$csv = shell_exec('phpcs --colors -nq --report="csv" --extensions="php" ' . implode(' ', $files));
$lines = array_map(function ($row) {
return array_map(function ($column) {
return trim($column, '"');
}, explode(',', $row));
}, array_filter(explode("\n", $csv)));

// Remove header row
array_shift($lines);

if (!count($lines)) {
echo "\e[0;32mFound no issues with code quality.\e[0m";
echo "\n";
exit(0);
} else {
// Group errors by file
$files = [];

foreach ($lines as $line) {
$filename = str_replace(dirname(dirname(dirname(__DIR__))), '', $line[0]);

if (empty($files[$filename])) {
$files[$filename] = [];
}

$files[$filename][] = [
'warning' => ($line[3] === 'warning'),
'message' => $line[4],
'line' => $line[1],
];
}

// Render report
echo "\e[0;31mFound "
. ((count($lines) === 1)
? '1 issue'
: count($lines) . ' issues')
. " with code quality.\e[0m";
echo "\n";

foreach ($files as $file => $errors) {
echo "\n";
echo "\e[1;37m" . str_replace('"', '', $file) . "\e[0m";
echo "\n\n";

foreach ($errors as $error) {
echo "\e[2m" . str_pad(' L' . $error['line'], 7) . " | \e[0m";
if ($error['warning'] === false) {
echo "\e[0;31mERR:\e[0m ";
} else {
echo "\e[1;33mWARN:\e[0m ";
}
echo $error['message'];
echo "\n";
}
}
exit(1);
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
"tests/TestCase.php",
"tests/DbTestCase.php"
],
"psr-4": {
"October\\Rain\\Tests\\": "tests/"
Expand Down
37 changes: 37 additions & 0 deletions src/Database/Concerns/GuardsAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php namespace October\Rain\Database\Concerns;

use October\Rain\Support\Str;

trait GuardsAttributes
{
/**
* Determine if the given key is guarded.
*
* This is an override of https://github.com/laravel/framework/commit/897d107775737a958dbd0b2f3ea37877c7526371
* and allows fields that don't exist in the database to be filled if they aren't specified as "guarded", under
* the pretense that they are handled in a special manner - ie. in the `beforeSave` event.
*
* @param string $key
* @return bool
*/
public function isGuarded($key)
{
$guarded = $this->getGuarded();
// Nothing's guarded so just return early
if (empty($guarded) || $guarded === ['*']) {
return false;
}
// Normalize the variables for comparison
$key = trim(strtolower($key));
$guarded = array_map(function ($column) {
return trim(strtolower($column));
}, $guarded);

// JSON columns are tricksy, we only guard base level columns though
if (strpos($key, '->') !== false) {
$key = Str::before($key, '->');
}

return in_array($key, $guarded);
}
}
3 changes: 1 addition & 2 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php namespace October\Rain\Database;

use Db;
use Input;
use Closure;
use October\Rain\Support\Arr;
use October\Rain\Support\Str;
Expand All @@ -21,6 +19,7 @@
*/
class Model extends EloquentModel
{
use Concerns\GuardsAttributes;
use Concerns\HasRelationships;
use \October\Rain\Support\Traits\Emitter;
use \October\Rain\Extension\ExtendableTrait;
Expand Down
15 changes: 0 additions & 15 deletions tests/Database/ModelAddersTest.php

This file was deleted.

0 comments on commit 16d8346

Please sign in to comment.