mirror of
https://github.com/nullthoughts/laravel-data-sync.git
synced 2026-02-01 08:47:16 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
734a6a3cbd | ||
|
|
a241493142 |
@@ -1,18 +0,0 @@
|
|||||||
# EditorConfig is awesome: https://editorconfig.org
|
|
||||||
|
|
||||||
# top-most EditorConfig file
|
|
||||||
root = true
|
|
||||||
|
|
||||||
# Unix-style newlines with a newline ending every file
|
|
||||||
[*]
|
|
||||||
end_of_line = lf
|
|
||||||
insert_final_newline = true
|
|
||||||
charset = utf-8
|
|
||||||
indent_style = space
|
|
||||||
indent_size = 4
|
|
||||||
|
|
||||||
[*.md]
|
|
||||||
trim_trailing_whitespace = false
|
|
||||||
|
|
||||||
[*.{yml,yaml}]
|
|
||||||
indent_size = 2
|
|
||||||
69
.github/workflows/phpunit.yml
vendored
69
.github/workflows/phpunit.yml
vendored
@@ -1,69 +1,20 @@
|
|||||||
---
|
|
||||||
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
|
|
||||||
name: PHPUnit
|
name: PHPUnit
|
||||||
|
|
||||||
on:
|
on: [push]
|
||||||
push:
|
|
||||||
branches: [master, main]
|
|
||||||
pull_request:
|
|
||||||
branches: [master, main]
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-test:
|
build-test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
statuses: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: true
|
|
||||||
matrix:
|
|
||||||
php: [ '8.0', '8.1', '8.2', '8.3' ]
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Setup PHP
|
- uses: php-actions/composer@v5
|
||||||
uses: shivammathur/setup-php@v2
|
|
||||||
|
- name: PHPUnit Tests
|
||||||
|
uses: php-actions/phpunit@v2
|
||||||
with:
|
with:
|
||||||
php-version: ${{ matrix.php }}
|
php_extensions: xdebug mbstring
|
||||||
tools: composer
|
bootstrap: vendor/autoload.php
|
||||||
extensions: json, dom, curl, libxml, mbstring
|
configuration: phpunit.xml
|
||||||
coverage: xdebug
|
args: --coverage-text
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Get composer cache directory
|
|
||||||
id: composer-cache-dir
|
|
||||||
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Get composer.lock or composer.json hash for caching
|
|
||||||
id: hash
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
if [ -f composer.lock ]; then
|
|
||||||
echo "lock=${{ hashFiles('**/composer.lock') }}" >> $GITHUB_OUTPUT
|
|
||||||
else
|
|
||||||
echo "lock=${{ hashFiles('**/composer.json') }}" >> $GITHUB_OUTPUT
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Cache Composer packages
|
|
||||||
id: composer-cache
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: ${{ steps.composer-cache-dir.outputs.dir }}
|
|
||||||
key: ${{ runner.os }}-php-${{ matrix.php }}-${{ steps.hash.outputs.lock }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-php-${{ matrix.php }}-${{ steps.hash.outputs.lock }}
|
|
||||||
${{ runner.os }}-php-${{ matrix.php }}-
|
|
||||||
${{ runner.os }}-php-
|
|
||||||
|
|
||||||
- name: Install Dependencies (prefer-${{ matrix.stability }})
|
|
||||||
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
|
|
||||||
|
|
||||||
- name: Configure matchers
|
|
||||||
uses: mheap/phpunit-matcher-action@v1
|
|
||||||
|
|
||||||
- name: Execute composer test (Unit and Feature tests)
|
|
||||||
run: composer test:ci
|
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,5 +12,4 @@ Homestead.yaml
|
|||||||
npm-debug.log
|
npm-debug.log
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
.env
|
.env
|
||||||
*.cache
|
.phpunit.result.cache
|
||||||
composer.lock
|
|
||||||
11
.travis.yml
11
.travis.yml
@@ -7,14 +7,11 @@ env:
|
|||||||
matrix:
|
matrix:
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
include:
|
include:
|
||||||
- php: 8.0
|
- php: 7.2
|
||||||
- php: 8.0
|
- php: 7.2
|
||||||
env: setup=lowest
|
env: setup=lowest
|
||||||
- php: 8.1
|
- php: 7.3
|
||||||
- php: 8.1
|
- php: 7.3
|
||||||
env: setup=lowest
|
|
||||||
- php: 8.2
|
|
||||||
- php: 8.2
|
|
||||||
env: setup=lowest
|
env: setup=lowest
|
||||||
|
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|||||||
@@ -8,35 +8,21 @@
|
|||||||
"email": "jani@nullincorporated.com"
|
"email": "jani@nullincorporated.com"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"scripts": {
|
|
||||||
"lint": "pint --test",
|
|
||||||
"lint:fix": "pint --repair",
|
|
||||||
"psalm": "psalm",
|
|
||||||
"psalm:fix": "psalm --alter --issues=MissingReturnType,MissingParamType",
|
|
||||||
"test": "phpunit --coverage-text",
|
|
||||||
"test:ci": "phpunit --teamcity"
|
|
||||||
},
|
|
||||||
"require": {
|
"require": {
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"php": "^8.0"
|
"php": "^8.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"orchestra/testbench": "^7|^8",
|
"orchestra/testbench": "^7.0"
|
||||||
"rector/rector": "^1",
|
|
||||||
"ergebnis/composer-normalize": "^2",
|
|
||||||
"vimeo/psalm": "^5",
|
|
||||||
"psalm/plugin-laravel": "^2",
|
|
||||||
"laravel/pint": "^1"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4" : {
|
||||||
"nullthoughts\\LaravelDataSync\\": "src/"
|
"nullthoughts\\LaravelDataSync\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4" : {
|
||||||
"nullthoughts\\LaravelDataSync\\Tests\\": "tests/",
|
"nullthoughts\\LaravelDataSync\\Tests\\": "tests/"
|
||||||
"nullthoughts\\LaravelDataSync\\Tests\\Fakes\\": "tests/fakes/"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
@@ -45,10 +31,5 @@
|
|||||||
"nullthoughts\\LaravelDataSync\\DataSyncBaseServiceProvider"
|
"nullthoughts\\LaravelDataSync\\DataSyncBaseServiceProvider"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"config": {
|
|
||||||
"allow-plugins": {
|
|
||||||
"ergebnis/composer-normalize": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7630
composer.lock
generated
Normal file
7630
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'path' => base_path('sync'),
|
/**
|
||||||
|
* Namespace for Laravel Models
|
||||||
|
*/
|
||||||
|
'namespace' => '\\App\\Models\\',
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path to directory containing JSON files for synchronization
|
||||||
|
*/
|
||||||
|
'path' => base_path('sync'),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array of Model names which controls the synchronization order
|
||||||
|
*/
|
||||||
'order' => [
|
'order' => [
|
||||||
//
|
//
|
||||||
],
|
],
|
||||||
|
|||||||
17
psalm.xml
17
psalm.xml
@@ -1,17 +0,0 @@
|
|||||||
<?xml version="1.0"?>
|
|
||||||
<psalm
|
|
||||||
errorLevel="7"
|
|
||||||
resolveFromConfigFile="true"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns="https://getpsalm.org/schema/config"
|
|
||||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
|
||||||
findUnusedBaselineEntry="true"
|
|
||||||
findUnusedCode="true"
|
|
||||||
>
|
|
||||||
<projectFiles>
|
|
||||||
<directory name="src" />
|
|
||||||
<ignoreFiles>
|
|
||||||
<directory name="vendor" />
|
|
||||||
</ignoreFiles>
|
|
||||||
</projectFiles>
|
|
||||||
</psalm>
|
|
||||||
28
readme.md
28
readme.md
@@ -8,34 +8,6 @@
|
|||||||
|
|
||||||
Laravel utility to keep records synchronized between environments through source control
|
Laravel utility to keep records synchronized between environments through source control
|
||||||
|
|
||||||
## [V3.1 branch](https://github.com/nullthoughts/laravel-data-sync/tree/v3.1) Notes (Work in progress)
|
|
||||||
- Adds support for Laravel 8+ models directory in `config/data-sync.php`:
|
|
||||||
```
|
|
||||||
'namespace' => '\\App\\Models\\',
|
|
||||||
```
|
|
||||||
|
|
||||||
- Adds ability to export existing Models to data sync files:
|
|
||||||
```
|
|
||||||
php artisan data:export User --criteria=name --criteria=email --except=id
|
|
||||||
```
|
|
||||||
|
|
||||||
which generates
|
|
||||||
|
|
||||||
```json
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"_name": "Cameron Frye",
|
|
||||||
"properties->title": "Best Friend",
|
|
||||||
"phone_numbers->mobile": "555-555-5556",
|
|
||||||
"_email": "noreply@buellerandco.com",
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
- Further work is required to support remote disks. For testing & development of new V3.1 features, use `composer require nullthoughts/laravel-data-sync:v3.1.x-dev`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
You can install this package via composer:
|
You can install this package via composer:
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
16
rector.php
16
rector.php
@@ -1,16 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
use Rector\Config\RectorConfig;
|
|
||||||
|
|
||||||
return RectorConfig::configure()
|
|
||||||
->withPaths([
|
|
||||||
__DIR__.'/config',
|
|
||||||
__DIR__.'/src',
|
|
||||||
__DIR__.'/tests',
|
|
||||||
])
|
|
||||||
->withPhpSets()
|
|
||||||
->withPreparedSets(deadCode: true, codeQuality: true)
|
|
||||||
->withImportNames(removeUnusedImports: true)
|
|
||||||
->withTypeCoverageLevel(0);
|
|
||||||
14
scratch.md
14
scratch.md
@@ -1,14 +1,12 @@
|
|||||||
# Dev
|
# Dev
|
||||||
|
- Check if null return from json decode (invalid JSON)
|
||||||
- Check if null return from json decode (invalid JSON)
|
- Check if updateOrCreate returns anything
|
||||||
- Check if updateOrCreate returns anything
|
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
- Must have the columsn you are updating as fillable
|
||||||
|
- In config, specify disk to use (string)
|
||||||
|
- Document Forge example
|
||||||
|
|
||||||
- Must have the columsn you are updating as fillable
|
|
||||||
- In config, specify disk to use (string)
|
|
||||||
- Document Forge example
|
|
||||||
|
|
||||||
# Troubleshooting
|
# Troubleshooting
|
||||||
|
- Make sure columns are fillable in model
|
||||||
- Make sure columns are fillable in model
|
|
||||||
24
src/Console/Commands/Export.php
Normal file
24
src/Console/Commands/Export.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace nullthoughts\LaravelDataSync\Console\Commands;
|
||||||
|
|
||||||
|
use nullthoughts\LaravelDataSync\Exporter;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class Export extends Command
|
||||||
|
{
|
||||||
|
protected $signature = 'data:export {model} {--criteria=*} {--except=*} {--only=*}';
|
||||||
|
|
||||||
|
protected $description = 'Export Model data for synchronization';
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$model = $this->argument('model');
|
||||||
|
|
||||||
|
$this->info('Exporting ' . $model . ' model to sync data file');
|
||||||
|
|
||||||
|
(new Exporter($model, $this->option('criteria'), $this->option('except'), $this->option('only')))->run();
|
||||||
|
|
||||||
|
$this->comment('Export for data sync completed');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace nullthoughts\LaravelDataSync\Console\Commands;
|
namespace nullthoughts\LaravelDataSync\Console\Commands;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
|
||||||
use nullthoughts\LaravelDataSync\Updater;
|
use nullthoughts\LaravelDataSync\Updater;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
class Sync extends Command
|
class Sync extends Command
|
||||||
{
|
{
|
||||||
@@ -11,8 +11,7 @@ class Sync extends Command
|
|||||||
|
|
||||||
protected $description = 'Update Models with respective sync data files';
|
protected $description = 'Update Models with respective sync data files';
|
||||||
|
|
||||||
/** @psalm-api */
|
public function handle()
|
||||||
public function handle(): void
|
|
||||||
{
|
{
|
||||||
$path = $this->option('path');
|
$path = $this->option('path');
|
||||||
$model = $this->option('model');
|
$model = $this->option('model');
|
||||||
|
|||||||
@@ -3,12 +3,10 @@
|
|||||||
namespace nullthoughts\LaravelDataSync;
|
namespace nullthoughts\LaravelDataSync;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use nullthoughts\LaravelDataSync\Console\Commands\Sync;
|
|
||||||
|
|
||||||
/** @psalm-api */
|
|
||||||
class DataSyncBaseServiceProvider extends ServiceProvider
|
class DataSyncBaseServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function boot(): void
|
public function boot()
|
||||||
{
|
{
|
||||||
if ($this->app->runningInConsole()) {
|
if ($this->app->runningInConsole()) {
|
||||||
$this->registerPublishing();
|
$this->registerPublishing();
|
||||||
@@ -18,11 +16,12 @@ class DataSyncBaseServiceProvider extends ServiceProvider
|
|||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->commands([
|
$this->commands([
|
||||||
Sync::class,
|
Console\Commands\Sync::class,
|
||||||
|
Console\Commands\Export::class,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function registerPublishing(): void
|
protected function registerPublishing()
|
||||||
{
|
{
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__.'/../config/data-sync.php' => config_path('data-sync.php'),
|
__DIR__.'/../config/data-sync.php' => config_path('data-sync.php'),
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use Throwable;
|
|||||||
|
|
||||||
class ErrorUpdatingModelException extends Exception
|
class ErrorUpdatingModelException extends Exception
|
||||||
{
|
{
|
||||||
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
|
public function __construct(string $message = '', int $code = 0, Throwable $previous = null)
|
||||||
{
|
{
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use Throwable;
|
|||||||
|
|
||||||
class NoRecordsInvalidJSONException extends Exception
|
class NoRecordsInvalidJSONException extends Exception
|
||||||
{
|
{
|
||||||
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
|
public function __construct(string $message = '', int $code = 0, Throwable $previous = null)
|
||||||
{
|
{
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
|
|
||||||
|
|||||||
96
src/Exporter.php
Normal file
96
src/Exporter.php
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace nullthoughts\LaravelDataSync;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
|
||||||
|
|
||||||
|
class Exporter
|
||||||
|
{
|
||||||
|
private $modelName;
|
||||||
|
|
||||||
|
private $criteria;
|
||||||
|
|
||||||
|
private $except;
|
||||||
|
|
||||||
|
private $only;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get files in sync directory.
|
||||||
|
*
|
||||||
|
* @param string $model
|
||||||
|
* @param array $criteria
|
||||||
|
*
|
||||||
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
||||||
|
*/
|
||||||
|
public function __construct(string $modelName, array $criteria = [], $except = [], $only = [])
|
||||||
|
{
|
||||||
|
$this->modelName = $modelName;
|
||||||
|
$this->criteria = $criteria;
|
||||||
|
$this->except = $except;
|
||||||
|
$this->only = $only;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export Model data to sync file.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$class = config('data-sync.namespace', '\\App\\Models\\') . $this->modelName;
|
||||||
|
$filename = $this->getDirectory() . '/' . $this->modelName . '.json';
|
||||||
|
$data = $this->prepareData($class);
|
||||||
|
|
||||||
|
file_put_contents($filename, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get directory path for sync files.
|
||||||
|
*
|
||||||
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getDirectory()
|
||||||
|
{
|
||||||
|
$directory = config('data-sync.path', base_path('sync'));
|
||||||
|
|
||||||
|
if (! file_exists($directory)) {
|
||||||
|
throw new FileDirectoryNotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function prepareData($class)
|
||||||
|
{
|
||||||
|
$class = new $class;
|
||||||
|
$keys = $this->prepareKeys($class);
|
||||||
|
|
||||||
|
return $class->select($keys)->get()->toJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function prepareKeys($class)
|
||||||
|
{
|
||||||
|
$keys = Schema::getColumnListing($class->getTable());
|
||||||
|
|
||||||
|
if ($this->only) {
|
||||||
|
$keys = array_intersect($keys, $this->only);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->except) {
|
||||||
|
$keys = array_diff($keys, $this->except);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return collect($keys)->map(function ($key) {
|
||||||
|
if (in_array($key, $this->criteria)) {
|
||||||
|
return $key . ' as _' . $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $key;
|
||||||
|
})->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,25 +10,40 @@ use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
|||||||
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
|
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
|
||||||
use nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException;
|
use nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException;
|
||||||
use nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
|
use nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
|
||||||
use stdClass;
|
|
||||||
|
|
||||||
class Updater
|
class Updater
|
||||||
{
|
{
|
||||||
private string $directory;
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $directory;
|
||||||
|
|
||||||
private Collection $files;
|
/**
|
||||||
|
* @var \Illuminate\Support\Collection
|
||||||
|
*/
|
||||||
|
private $files;
|
||||||
|
|
||||||
private bool $remote;
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $remote;
|
||||||
|
|
||||||
private string $disk;
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $disk;
|
||||||
|
|
||||||
private string $baseNamespace = '\\App\\';
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $modelNamespace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get files in sync directory.
|
* Get files in sync directory.
|
||||||
*
|
*
|
||||||
* @param string|null $path
|
* @param string|null $path
|
||||||
* @param string|null $model
|
* @param string|null $model
|
||||||
|
*
|
||||||
* @param bool $remote
|
* @param bool $remote
|
||||||
* @param string $disk
|
* @param string $disk
|
||||||
*
|
*
|
||||||
@@ -38,6 +53,7 @@ class Updater
|
|||||||
{
|
{
|
||||||
$this->remote = $remote;
|
$this->remote = $remote;
|
||||||
$this->disk = $disk;
|
$this->disk = $disk;
|
||||||
|
$this->modelNamespace = config('data-sync.namespace', '\\App\\Models\\');
|
||||||
|
|
||||||
$this->directory = $this->getDirectory($path);
|
$this->directory = $this->getDirectory($path);
|
||||||
$this->files = $this->getFiles($this->directory, $model);
|
$this->files = $this->getFiles($this->directory, $model);
|
||||||
@@ -46,25 +62,17 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Override the default namespace for the class.
|
* Override the default namespace for the class.
|
||||||
*
|
*
|
||||||
* @psalm-api
|
* @param $namespace
|
||||||
*/
|
*/
|
||||||
public function setNamespace(string $namespace): void
|
public function setNamespace($namespace)
|
||||||
{
|
{
|
||||||
$this->baseNamespace = $namespace;
|
$this->modelNamespace = $namespace;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override the default directory for the class.
|
|
||||||
*
|
|
||||||
* @psalm-api
|
|
||||||
*/
|
|
||||||
public function setDirectory(string $directory): void
|
|
||||||
{
|
|
||||||
$this->directory = $directory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute syncModel for each file.
|
* Execute syncModel for each file.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
@@ -73,7 +81,7 @@ class Updater
|
|||||||
return $files->map(function ($file) {
|
return $files->map(function ($file) {
|
||||||
try {
|
try {
|
||||||
return $this->syncModel($file);
|
return $this->syncModel($file);
|
||||||
} catch (\ErrorException) {
|
} catch (\ErrorException $e) {
|
||||||
$model = pathinfo($file, PATHINFO_FILENAME);
|
$model = pathinfo($file, PATHINFO_FILENAME);
|
||||||
|
|
||||||
throw new ErrorUpdatingModelException(ucwords($model));
|
throw new ErrorUpdatingModelException(ucwords($model));
|
||||||
@@ -84,9 +92,9 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Parse each record for criteria/values and update/create model.
|
* Parse each record for criteria/values and update/create model.
|
||||||
*
|
*
|
||||||
|
* @param string $file
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*
|
|
||||||
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||||
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
||||||
*/
|
*/
|
||||||
@@ -113,28 +121,29 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Get directory path for sync files.
|
* Get directory path for sync files.
|
||||||
*
|
*
|
||||||
*
|
* @param $path
|
||||||
* @return string
|
|
||||||
*
|
*
|
||||||
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getDirectory(?string $path)
|
protected function getDirectory($path)
|
||||||
{
|
{
|
||||||
$directory = $path ?? config('data-sync.path', base_path('sync'));
|
$directory = $path ?? config('data-sync.path', base_path('sync'));
|
||||||
|
|
||||||
if ($this->directoryMissingLocally($directory) || $this->directoryMissingRemotely($directory)) {
|
if ($this->directoryMissingLocally($directory) || $this->directoryMissingRemotely($directory)) {
|
||||||
throw new FileDirectoryNotFoundException;
|
throw new FileDirectoryNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->directory = $directory;
|
return $directory;
|
||||||
|
|
||||||
return $this->directory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get list of files in directory.
|
* Get list of files in directory.
|
||||||
*
|
*
|
||||||
* @param string|null $model
|
* @param string $directory
|
||||||
|
* @param string|null $model
|
||||||
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
protected function getFiles(string $directory, $model = null)
|
protected function getFiles(string $directory, $model = null)
|
||||||
@@ -161,6 +170,7 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Sort Models by pre-configured order.
|
* Sort Models by pre-configured order.
|
||||||
*
|
*
|
||||||
|
* @param \Illuminate\Support\Collection $files
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
@@ -185,19 +195,20 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Filter record criteria.
|
* Filter record criteria.
|
||||||
*
|
*
|
||||||
*
|
* @param object $record
|
||||||
* @return \Illuminate\Support\Collection
|
|
||||||
*
|
*
|
||||||
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
protected function getCriteria(stdClass $record)
|
protected function getCriteria(object $record)
|
||||||
{
|
{
|
||||||
$criteria = collect($record)->filter(function ($value, $key) {
|
$criteria = collect($record)->filter(function ($value, $key) {
|
||||||
return $this->isCriteria($key);
|
return $this->isCriteria($key);
|
||||||
});
|
});
|
||||||
|
|
||||||
if ($criteria->count() == 0) {
|
if ($criteria->count() == 0) {
|
||||||
throw new NoCriteriaException;
|
throw new NoCriteriaException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $criteria->mapWithKeys(function ($value, $key) {
|
return $criteria->mapWithKeys(function ($value, $key) {
|
||||||
@@ -208,10 +219,11 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Filter record values.
|
* Filter record values.
|
||||||
*
|
*
|
||||||
|
* @param object $record
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
protected function getValues(stdClass $record)
|
protected function getValues(object $record)
|
||||||
{
|
{
|
||||||
return collect($record)->reject(function ($value, $key) {
|
return collect($record)->reject(function ($value, $key) {
|
||||||
if ($this->isCriteria($key)) {
|
if ($this->isCriteria($key)) {
|
||||||
@@ -229,20 +241,21 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Returns model name for file.
|
* Returns model name for file.
|
||||||
*
|
*
|
||||||
|
* @param string $name
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getModel(string $name)
|
protected function getModel(string $name)
|
||||||
{
|
{
|
||||||
return $this->baseNamespace.Str::studly(pathinfo($name, PATHINFO_FILENAME));
|
return $this->modelNamespace . Str::studly(pathinfo($name, PATHINFO_FILENAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses JSON from file and returns collection.
|
* Parses JSON from file and returns collection.
|
||||||
*
|
*
|
||||||
|
* @param string $file
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*
|
|
||||||
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||||
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
||||||
*/
|
*/
|
||||||
@@ -262,7 +275,8 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Check if column is criteria for a condition match.
|
* Check if column is criteria for a condition match.
|
||||||
*
|
*
|
||||||
* @param string $key
|
* @param string $key
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function isCriteria($key)
|
protected function isCriteria($key)
|
||||||
@@ -273,10 +287,12 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Return ID for nested key-value pairs.
|
* Return ID for nested key-value pairs.
|
||||||
*
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param object $values
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function resolveId(string $key, stdClass $values)
|
protected function resolveId(string $key, object $values)
|
||||||
{
|
{
|
||||||
$model = $this->getModel($key);
|
$model = $this->getModel($key);
|
||||||
|
|
||||||
@@ -294,6 +310,7 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Detect nested objects and resolve them.
|
* Detect nested objects and resolve them.
|
||||||
*
|
*
|
||||||
|
* @param \Illuminate\Support\Collection $record
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -310,19 +327,21 @@ class Updater
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Illuminate\Config\Repository $directory
|
* @param \Illuminate\Config\Repository $directory
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function directoryMissingLocally($directory)
|
protected function directoryMissingLocally($directory)
|
||||||
{
|
{
|
||||||
return ! $this->remote && ! file_exists($directory);
|
return !$this->remote && !file_exists($directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Illuminate\Config\Repository $directory
|
* @param \Illuminate\Config\Repository $directory
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function directoryMissingRemotely($directory)
|
protected function directoryMissingRemotely($directory)
|
||||||
{
|
{
|
||||||
return $this->remote && ! Storage::disk($this->disk)->exists($directory);
|
return $this->remote && !Storage::disk($this->disk)->exists($directory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
namespace nullthoughts\LaravelDataSync\Tests;
|
namespace nullthoughts\LaravelDataSync\Tests;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
||||||
|
|
||||||
class Roles extends Model
|
class Roles extends Model
|
||||||
{
|
{
|
||||||
@@ -11,7 +10,7 @@ class Roles extends Model
|
|||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
public function supervisor(): BelongsTo
|
public function supervisor()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Supervisor::class);
|
return $this->belongsTo(Supervisor::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
namespace nullthoughts\LaravelDataSync\Tests;
|
namespace nullthoughts\LaravelDataSync\Tests;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
||||||
|
|
||||||
class Supervisor extends Model
|
class Supervisor extends Model
|
||||||
{
|
{
|
||||||
@@ -11,7 +10,7 @@ class Supervisor extends Model
|
|||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
public function roles(): HasMany
|
public function roles()
|
||||||
{
|
{
|
||||||
return $this->hasMany(Roles::class);
|
return $this->hasMany(Roles::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,30 +7,25 @@ use Illuminate\Support\Facades\Schema;
|
|||||||
|
|
||||||
class TestCase extends \Orchestra\Testbench\TestCase
|
class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
{
|
{
|
||||||
/** @psalm-suppress PropertyNotSetInConstructor */
|
protected function getEnvironmentSetUp($app)
|
||||||
public string $testDataPath;
|
|
||||||
|
|
||||||
protected function getEnvironmentSetUp($app): void
|
|
||||||
{
|
{
|
||||||
$app['config']->set('database.default', 'testdb');
|
$app['config']->set('database.default', 'testdb');
|
||||||
$app['config']->set('database.connections.testdb', [
|
$app['config']->set('database.connections.testdb', [
|
||||||
'driver' => 'sqlite',
|
'driver' => 'sqlite',
|
||||||
'database' => ':memory:',
|
'database' => ':memory:',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->testDataPath = __DIR__.'/test-data';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
Schema::create('supervisors', static function (Blueprint $table): void {
|
Schema::create('supervisors', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
});
|
});
|
||||||
|
|
||||||
Schema::create('roles', static function (Blueprint $table): void {
|
Schema::create('roles', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('slug');
|
$table->string('slug');
|
||||||
$table->unsignedInteger('supervisor_id')->nullable();
|
$table->unsignedInteger('supervisor_id')->nullable();
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace nullthoughts\LaravelDataSync\Tests\Unit;
|
namespace nullthoughts\LaravelDataSync\Tests;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
||||||
use nullthoughts\LaravelDataSync\Tests\Fakes\UpdaterFake;
|
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
|
||||||
use nullthoughts\LaravelDataSync\Tests\Roles;
|
use Exception;
|
||||||
use nullthoughts\LaravelDataSync\Tests\Supervisor;
|
|
||||||
use nullthoughts\LaravelDataSync\Tests\TestCase;
|
|
||||||
|
|
||||||
class UpdaterRemoteTest extends TestCase
|
class UpdaterRemoteTest extends TestCase
|
||||||
{
|
{
|
||||||
@@ -84,10 +81,9 @@ class UpdaterRemoteTest extends TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*
|
|
||||||
* @group current
|
* @group current
|
||||||
*/
|
*/
|
||||||
public function exception_is_thrown_if_the_directory_does_not_exists(): void
|
public function exception_is_thrown_if_the_directory_does_not_exists()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
new UpdaterFake(null, null, true, 's3');
|
new UpdaterFake(null, null, true, 's3');
|
||||||
@@ -99,7 +95,7 @@ class UpdaterRemoteTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function invalid_json_throws_an_exception_in_remote(): void
|
public function invalid_json_throws_an_exception_in_remote()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$updater = new UpdaterFake('test-data/invalid-json', null, true, 's3');
|
$updater = new UpdaterFake('test-data/invalid-json', null, true, 's3');
|
||||||
@@ -112,7 +108,7 @@ class UpdaterRemoteTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function the_json_must_contain_a_key_with_an_underscore_in_remote(): void
|
public function the_json_must_contain_a_key_with_an_underscore_in_remote()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$updater = new UpdaterFake('test-data/no-criteria', null, true, 's3');
|
$updater = new UpdaterFake('test-data/no-criteria', null, true, 's3');
|
||||||
@@ -125,7 +121,7 @@ class UpdaterRemoteTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function order_of_imports_can_be_defined_in_config_in_remote(): void
|
public function order_of_imports_can_be_defined_in_config_in_remote()
|
||||||
{
|
{
|
||||||
config()->set('data-sync.order', [
|
config()->set('data-sync.order', [
|
||||||
'Supervisor',
|
'Supervisor',
|
||||||
@@ -140,7 +136,7 @@ class UpdaterRemoteTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote(): void
|
public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote()
|
||||||
{
|
{
|
||||||
config()->set('data-sync.order', [
|
config()->set('data-sync.order', [
|
||||||
'Roles',
|
'Roles',
|
||||||
@@ -154,7 +150,7 @@ class UpdaterRemoteTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_ignores_non_json_files_in_remote(): void
|
public function it_ignores_non_json_files_in_remote()
|
||||||
{
|
{
|
||||||
$updater = new UpdaterFake('test-data/not-json', null, true, 's3');
|
$updater = new UpdaterFake('test-data/not-json', null, true, 's3');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|||||||
@@ -1,20 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace nullthoughts\LaravelDataSync\Tests\Unit;
|
namespace nullthoughts\LaravelDataSync\Tests;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
||||||
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
|
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
|
||||||
use nullthoughts\LaravelDataSync\Tests\Roles;
|
use Exception;
|
||||||
use nullthoughts\LaravelDataSync\Tests\Supervisor;
|
|
||||||
use nullthoughts\LaravelDataSync\Tests\TestCase;
|
|
||||||
|
|
||||||
class UpdaterTest extends TestCase
|
class UpdaterTest extends TestCase
|
||||||
{
|
{
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_adds_roles_to_the_database(): void
|
public function it_adds_roles_to_the_database()
|
||||||
{
|
{
|
||||||
$updater = new UpdaterFake($this->testDataPath, 'roles');
|
$updater = new UpdaterFake(__DIR__.'/../test-data', 'roles');
|
||||||
|
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
@@ -26,9 +23,9 @@ class UpdaterTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function it_can_default_to_configuration()
|
public function it_can_default_to_configuration()
|
||||||
{
|
{
|
||||||
config()->set('data-sync.path', $this->testDataPath);
|
config()->set('data-sync.path', __DIR__.'/../test-data');
|
||||||
|
|
||||||
$updater = new UpdaterFake;
|
$updater = new UpdaterFake();
|
||||||
|
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
@@ -40,11 +37,11 @@ class UpdaterTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function it_can_update_an_existing_record()
|
public function it_can_update_an_existing_record()
|
||||||
{
|
{
|
||||||
config()->set('data-sync.path', $this->testDataPath);
|
config()->set('data-sync.path', __DIR__.'/../test-data');
|
||||||
(new UpdaterFake)->run();
|
(new UpdaterFake())->run();
|
||||||
|
|
||||||
config()->set('data-sync.path', $this->testDataPath.'/valid');
|
config()->set('data-sync.path', __DIR__.'/../test-data/valid');
|
||||||
(new UpdaterFake)->run();
|
(new UpdaterFake())->run();
|
||||||
|
|
||||||
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
||||||
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
||||||
@@ -52,24 +49,24 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_can_update_the_relationship(): void
|
public function it_can_update_the_relationship()
|
||||||
{
|
{
|
||||||
$supervisor = Supervisor::create([
|
$supervisor = Supervisor::create([
|
||||||
'name' => 'CEO',
|
'name' => 'CEO',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
config()->set('data-sync.path', $this->testDataPath.'/relationship', 'roles');
|
config()->set('data-sync.path', __DIR__.'/../test-data/relationship', 'roles');
|
||||||
(new UpdaterFake)->run();
|
(new UpdaterFake())->run();
|
||||||
|
|
||||||
$this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
|
$this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
|
||||||
$this->assertTrue($supervisor->is(Roles::first()->supervisor));
|
$this->assertTrue($supervisor->is(Roles::first()->supervisor));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function exception_is_thrown_if_the_directory_does_not_exists(): void
|
public function exception_is_thrown_if_the_directory_does_not_exists()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
new UpdaterFake;
|
new UpdaterFake();
|
||||||
|
|
||||||
$this->fail('exception was thrown');
|
$this->fail('exception was thrown');
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@@ -78,10 +75,10 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function invalid_json_throws_an_exception(): void
|
public function invalid_json_throws_an_exception()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$updater = new UpdaterFake($this->testDataPath.'/invalid-json');
|
$updater = new UpdaterFake(__DIR__.'/../test-data/invalid-json');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->fail('exception was thrown');
|
$this->fail('exception was thrown');
|
||||||
@@ -91,10 +88,10 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function the_json_must_contain_a_key_with_an_underscore(): void
|
public function the_json_must_contain_a_key_with_an_underscore()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$updater = new UpdaterFake($this->testDataPath.'/no-criteria');
|
$updater = new UpdaterFake(__DIR__.'/../test-data/no-criteria');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->fail('exception was thrown');
|
$this->fail('exception was thrown');
|
||||||
@@ -104,21 +101,22 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function order_of_imports_can_be_defined_in_config(): void
|
public function order_of_imports_can_be_defined_in_config()
|
||||||
{
|
{
|
||||||
config()->set('data-sync.order', [
|
config()->set('data-sync.order', [
|
||||||
'Supervisor',
|
'Supervisor',
|
||||||
'Roles',
|
'Roles',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$updater = new UpdaterFake($this->testDataPath.'/ordered');
|
$updater = new UpdaterFake(__DIR__.'/../test-data/ordered');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
|
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
|
||||||
$this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
|
$this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exception_is_thrown_if_imports_are_in_incorrect_order(): void
|
/** @test */
|
||||||
|
public function exception_is_thrown_if_imports_are_in_incorrect_order()
|
||||||
{
|
{
|
||||||
config()->set('data-sync.order', [
|
config()->set('data-sync.order', [
|
||||||
'Roles',
|
'Roles',
|
||||||
@@ -127,14 +125,14 @@ class UpdaterTest extends TestCase
|
|||||||
|
|
||||||
$this->expectException(ErrorUpdatingModelException::class);
|
$this->expectException(ErrorUpdatingModelException::class);
|
||||||
|
|
||||||
$updater = new UpdaterFake($this->testDataPath.'/ordered');
|
$updater = new UpdaterFake(__DIR__.'/../test-data/ordered');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_ignores_non_json_files(): void
|
public function it_ignores_non_json_files()
|
||||||
{
|
{
|
||||||
$updater = new UpdaterFake($this->testDataPath.'/not-json');
|
$updater = new UpdaterFake(__DIR__.'/../test-data/not-json');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
|
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
|
||||||
|
|||||||
@@ -2,15 +2,15 @@
|
|||||||
|
|
||||||
namespace nullthoughts\LaravelDataSync\Tests\Fakes;
|
namespace nullthoughts\LaravelDataSync\Tests\Fakes;
|
||||||
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use nullthoughts\LaravelDataSync\Updater;
|
use nullthoughts\LaravelDataSync\Updater;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class UpdaterFake extends Updater
|
class UpdaterFake extends Updater
|
||||||
{
|
{
|
||||||
protected function getModel(string $name)
|
protected function getModel(string $name)
|
||||||
{
|
{
|
||||||
return '\\nullthoughts\\LaravelDataSync\\Tests\\'.Str::studly(
|
return '\\nullthoughts\\LaravelDataSync\\Tests\\'.Str::studly(
|
||||||
pathinfo($name, PATHINFO_FILENAME)
|
pathinfo($name, PATHINFO_FILENAME)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1 +1,3 @@
|
|||||||
[]
|
[
|
||||||
|
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user