Skip to content

Commit

Permalink
Merge pull request #18 from UseMuffin/ci
Browse files Browse the repository at this point in the history
Switch to GA workflow
  • Loading branch information
ADmad committed Jan 29, 2021
2 parents 835bc25 + b9568b0 commit 1a4f1d3
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -14,3 +14,6 @@ trim_trailing_whitespace = true
[*.yml]
indent_style = space
indent_size = 2

[*.neon]
indent_style = tab
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -9,3 +9,4 @@ phpunit.xml.dist export-ignore
tests export-ignore
docs export-ignore
.github export-ignore
.stickler.yml export-ignore
105 changes: 105 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,105 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- '*'

jobs:
testsuite:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.4', '8.0']
db-type: [mysql, pgsql, sqlite]
prefer-lowest: ['']
include:
- php-version: '7.2'
db-type: 'sqlite'
prefer-lowest: 'prefer-lowest'

services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup Service
if: matrix.db-type == 'mysql'
run: |
sudo service mysql start
mysql -h 127.0.0.1 -u root -proot -e 'CREATE DATABASE cakephp;'
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, pdo_${{ matrix.db-type }}
coverage: pcov

- name: Composer install
run: |
composer --version
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer install
fi
- name: Run PHPUnit
run: |
if [[ ${{ matrix.db-type }} == 'sqlite' ]]; then export DB_URL='sqlite:///:memory:'; fi
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_URL='mysql://root:root@127.0.0.1/cakephp'; fi
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_URL='postgres://postgres:postgres@127.0.0.1/postgres'; fi
if [[ ${{ matrix.php-version }} == '7.2' && ${{ matrix.db-type }} == 'mysql' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: success() && matrix.php-version == '7.2' && matrix.db-type == 'mysql'
uses: codecov/codecov-action@v1

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: psalm:^4.4, phpstan:^0.12

- name: Composer Install
run: composer require --dev cakephp/cakephp-codesniffer:^4.2

- name: Run phpcs
run: vendor/bin/phpcs src/ tests/

- name: Run psalm
if: success() || failure()
run: psalm --output-format=github

- name: Run phpstan
if: success() || failure()
run: phpstan analyse
65 changes: 0 additions & 65 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
@@ -1,7 +1,7 @@
# Obfuscate

[![Build Status](https://img.shields.io/travis/UseMuffin/Obfuscate/master.svg?style=flat-square)](https://travis-ci.org/UseMuffin/Obfuscate)
[![Coverage](https://img.shields.io/codecov/c/github/UseMuffin/Obfuscate.svg?style=flat-square)](https://codecov.io/github/UseMuffin/Obfuscate)
[![Build Status](https://img.shields.io/github/workflow/status/UseMuffin/Obfuscate/CI/master?style=flat-square)](https://github.com/UseMuffin/Obfuscate/actions?query=workflow%3ACI+branch%3Amaster)
[![Coverage Status](https://img.shields.io/codecov/c/github/UseMuffin/Obfuscate.svg?style=flat-square)](https://codecov.io/github/UseMuffin/Obfuscate)
[![Total Downloads](https://img.shields.io/packagist/dt/muffin/obfuscate.svg?style=flat-square)](https://packagist.org/packages/muffin/obfuscate)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -31,10 +31,10 @@
"source": "https://github.com/usemuffin/obfuscate"
},
"require": {
"cakephp/orm": "^4.0"
"cakephp/orm": "^4.1"
},
"require-dev": {
"cakephp/cakephp": "^4.0",
"cakephp/cakephp": "^4.1",
"phpunit/phpunit": "~8.5.0",
"zackkitzmiller/tiny": "^1.2",
"jenssegers/optimus": "^0.2",
Expand Down
8 changes: 8 additions & 0 deletions phpcs.xml
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset name="Muffin/Obfuscate">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />

<arg value="s"/>
</ruleset>
10 changes: 6 additions & 4 deletions phpstan.neon
@@ -1,7 +1,9 @@
includes:
- phpstan-baseline.neon
- phpstan-baseline.neon

parameters:
level: 6
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
level: 6
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
paths:
- src/
4 changes: 2 additions & 2 deletions src/Model/Behavior/ObfuscateBehavior.php
Expand Up @@ -4,7 +4,7 @@
namespace Muffin\Obfuscate\Model\Behavior;

use ArrayObject;
use Cake\Database\Expression\Comparison;
use Cake\Database\Expression\ComparisonExpression;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
Expand Down Expand Up @@ -105,7 +105,7 @@ public function beforeFind(EventInterface $event, Query $query, ArrayObject $opt
}

if (
$expression instanceof Comparison
$expression instanceof ComparisonExpression
&& in_array($expression->getField(), [$pk, $this->_table->aliasField($pk)])
) {
$expression->setValue($this->elucidate($expression->getValue()));
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Behavior/Strategy/UuidStrategy.php
Expand Up @@ -41,7 +41,7 @@ public function __construct(Table $table, string $field = 'uuid')
*/
public function obfuscate($str): string
{
/** @psalm-suppress InvalidArgument */
/** @psalm-suppress InvalidArrayOffset */
$record = $this->_table
->find()
->where([$this->_table->getPrimaryKey() => $str])
Expand Down
30 changes: 22 additions & 8 deletions tests/TestCase/Model/Behavior/ObfuscateBehaviorTest.php
Expand Up @@ -6,7 +6,7 @@
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use Muffin\Obfuscate\Model\Behavior\Strategy\TinyStrategy;
use Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface;
use Muffin\Obfuscate\Model\Behavior\Strategy\UuidStrategy;

class ObfuscateBehaviorTest extends TestCase
Expand Down Expand Up @@ -55,13 +55,27 @@ public function setUp(): void

$this->Comments = $this->getTableLocator()->get('Muffin/Obfuscate.Comments', ['table' => 'obfuscate_comments']);

$strategy = $this->getMockBuilder(StrategyInterface::class)
->getMock();
$strategy->expects($this->any())
->method('obfuscate')
->will($this->returnCallback(function ($id) {
return 'a' . $id;
}));

$strategy->expects($this->any())
->method('elucidate')
->will($this->returnCallback(function ($hashId) {
return (int)substr($hashId, 1);
}));

$this->Tags = $this->getTableLocator()->get('Muffin/Obfuscate.Tags', ['table' => 'obfuscate_tags']);
$this->Tags->addBehavior('Muffin/Obfuscate.Obfuscate', [
'strategy' => new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B'),
'strategy' => $strategy,
]);

$this->Articles = $this->getTableLocator()->get('Muffin/Obfuscate.Articles', ['table' => 'obfuscate_articles']);
$this->Articles->addBehavior('Muffin/Obfuscate.Obfuscate', ['strategy' => new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B')]);
$this->Articles->addBehavior('Muffin/Obfuscate.Obfuscate', ['strategy' => $strategy]);
$this->Articles->hasMany('Muffin/Obfuscate.Comments');
$this->Articles->belongsTo('Muffin/Obfuscate.Authors');
$this->Articles->belongsToMany('Muffin/Obfuscate.Tags', [
Expand Down Expand Up @@ -92,7 +106,7 @@ public function testAfterSave(): void
{
$entity = new Entity(['id' => 5, 'title' => 'foo']);
$this->Articles->save($entity);
$this->assertEquals('E', $entity['id']);
$this->assertEquals('a5', $entity['id']);
$this->assertFalse($entity->isDirty('id'));
}

Expand All @@ -109,7 +123,7 @@ public function testFindObfuscate(): void
])->first();

$this->assertEquals('f1f88079-ec15-4863-ad41-7e85cfa98f3d', $result['author']['id']);
$this->assertEquals('S', $result['tags'][0]['id']);
$this->assertEquals('a1', $result['tags'][0]['id']);
$this->assertEquals(1, $result['comments'][0]['id']);
$this->assertEquals(2, $result['comments'][1]['id']);
}
Expand Down Expand Up @@ -139,7 +153,7 @@ public function testFindWithoutObfuscate(): void
public function testFindObfuscated(): void
{
$results = $this->Articles->find('obfuscated')
->where(['id' => 'S'])
->where(['id' => 'a1'])
->toArray();
$this->assertEquals('1', $results[0]['id']);

Expand All @@ -163,11 +177,11 @@ public function testFindWithoutObfuscated(): void

public function testObfuscate(): void
{
$this->assertEquals('S', $this->Articles->behaviors()->call('obfuscate', [1]));
$this->assertEquals('a1', $this->Articles->behaviors()->call('obfuscate', [1]));
}

public function testElucidate(): void
{
$this->assertEquals(1, $this->Articles->behaviors()->call('elucidate', ['S']));
$this->assertEquals(1, $this->Articles->behaviors()->call('elucidate', ['a1']));
}
}
4 changes: 3 additions & 1 deletion tests/TestCase/Model/Behavior/Strategy/TinyStrategyTest.php
Expand Up @@ -3,8 +3,8 @@

namespace Muffin\Obfuscate\Test\TestCase\Model\Behavior\Strategy;

use Cake\TestSuite\TestCase;
use Muffin\Obfuscate\Model\Behavior\Strategy\TinyStrategy;
use PHPUnit\Framework\TestCase;

class TinyStrategyTest extends TestCase
{
Expand All @@ -15,6 +15,8 @@ class TinyStrategyTest extends TestCase

public function setUp(): void
{
$this->skipIf(PHP_VERSION_ID >= 80000, 'Tiny doesn\'t have PHP 8 support');

$this->strategy = new TinyStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B');
}

Expand Down

0 comments on commit 1a4f1d3

Please sign in to comment.