Skip to content

Commit

Permalink
added ci(test & coverage) support; minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang Xuan committed Jan 11, 2019
1 parent 6811970 commit ecd7a03
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 11 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
/logs/
composer.phar
composer.lock
clover.xml
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php
php:
- "7.3"
- "7.2"
- "7.1"
# env:
# - PHPUNIT_VERSION=^7 TESTBENCH_VERSION=3.*
# matrix:
# include:
# - php: 7.0
# env: PHPUNIT_VERSION=^6 TESTBENCH_VERSION=3.5.*
# before_install:
# - composer require --dev "phpunit/phpunit:${PHPUNIT_VERSION}"
# - composer require --dev "orchestra/testbench:${TESTBENCH_VERSION}"
install:
- composer install -n
script:
- composer test:dist
after_success:
- travis_retry composer test:coverage
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<p align="center">
<a href="https://travis-ci.org/seancheung/history"><img src="https://travis-ci.org/seancheung/history.svg?branch=master" alt="Build Status"></a>
<a href='https://coveralls.io/github/seancheung/history?branch=master'><img src='https://coveralls.io/repos/github/seancheung/history/badge.svg?branch=master' alt='Coverage Status' /></a>
<a href="https://packagist.org/packages/panoscape/history"><img src="https://poser.pugx.org/panoscape/history/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/panoscape/history"><img src="https://poser.pugx.org/panoscape/history/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/panoscape/history"><img src="https://poser.pugx.org/panoscape/history/license.svg" alt="License"></a>
</p>

# History

Eloquent model history tracking for Laravel

## Installation
Expand Down Expand Up @@ -121,7 +130,6 @@ $model->histories()->orderBy('performed_at', 'desc')->take(10)
$model->histories()->where('user_id', 10010)
```


### History

```php
Expand Down Expand Up @@ -201,3 +209,35 @@ This will translate your model history into
### Filters

You may set whitelist and blacklist in config file. Please follow the description guide in the published config file.

### Known issues

1. When updating a model, if its model label(attributes returned from `getModelLabel`) has been modified, the history message will use its new attributes, which might not be what you expect.

```php
class Article extends Model
{
use HasHistories;

public function getModelLabel()
{
return $this->title;
}
}
// original title is 'my title'
// modify title
$article->title = 'new title';
$article->save();
// the updating history message
// expect: Updating Article my title
// actual: Updating Article new title
```

A workaround

```php
public function getModelLabel()
{
return $this->getOriginal('title', $this->title);
}
```
18 changes: 17 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage": "https://github.com/seancheung/history",
"require": {
"php": ">=5.6.4",
"illuminate/support": "^5.3"
"illuminate/support": "^5.3"
},
"license": "MIT",
"authors": [
Expand All @@ -18,5 +18,21 @@
"psr-4": {
"Panoscape\\History\\": "src/"
}
},
"scripts": {
"test": "phpunit",
"test:dist": "phpunit --coverage-clover clover.xml",
"test:coverage": "php-coveralls -v -x clover.xml -o ./logs --exclude-no-stmt"
},
"autoload-dev": {
"psr-4": {
"Panoscape\\History\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "~7.4",
"orchestra/testbench": "~3.7",
"mockery/mockery": "^1.2",
"php-coveralls/php-coveralls": "^2.1"
}
}
28 changes: 28 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Panoscape\History Test Suite">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
</phpunit>
8 changes: 4 additions & 4 deletions src/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public function __construct(array $attributes = [])
}

/**
* Get the user of this record
* Get the user who performed this record
*/
public function user()
{
return $this->morphTo();
return $this->hasUser()? $this->morphTo()->first(): null;
}

/**
Expand All @@ -70,14 +70,14 @@ public function user()
*/
public function hasUser()
{
return !empty($this->agent_type) && !empty($this->agent_id);
return !empty($this->user_type) && !empty($this->user_id);
}

/**
* Get the model of this record
*/
public function model()
{
return $this->morphTo();
return $this->morphTo()->first();
}
}
7 changes: 6 additions & 1 deletion src/HistoryObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function updating($model)
* Gets the model's altered values and tracks what had changed
*/
$changes = $model->getDirty();
/**
* Bypass restoring event
*/
if(array_key_exists('deleted_at', $changes)) return;

$changed = [];
foreach ($changes as $key => $value) {
if(static::isIgnored($model, $key)) continue;
Expand Down Expand Up @@ -111,7 +116,7 @@ public static function isIgnored($model, $key)
{
$blacklist = config('history.attributes_blacklist');
$name = get_class($model);
$array = $blacklist[$name];
$array = isset($blacklist[$name])? $blacklist[$name]: null;
return !empty($array) && in_array($key, $array);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Panoscape\History\Tests;

use Illuminate\Database\Eloquent\Model;
use Panoscape\History\HasHistories;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends Model
{
use SoftDeletes, HasHistories;
public $timestamps = false;
protected $guarded = [];
protected $hidden = [];

public function getModelLabel()
{
return $this->getOriginal('title', $this->title);
}
}
Loading

0 comments on commit ecd7a03

Please sign in to comment.