mirror of
https://github.com/nullthoughts/laravel-data-sync.git
synced 2026-01-26 11:44:11 +00:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c6bcd068b | ||
|
|
ae5828cc3f | ||
|
|
38c451fa1c | ||
|
|
3d1a5da432 | ||
|
|
0be2ed7c16 | ||
|
|
da3eab90c5 | ||
| d39a0020b1 | |||
|
|
70ae10137c | ||
|
|
e9e95871a1 | ||
|
|
aec5ec026a | ||
|
|
e12085be2c | ||
|
|
261e47bf7f | ||
|
|
311cf155c5 | ||
|
|
f14dc0a5c2 | ||
|
|
9980bc7e9a | ||
|
|
9935cb78cb | ||
|
|
9ade30325c | ||
|
|
effd3be10f | ||
| 412a03516b | |||
|
|
730fe0e2ac | ||
|
|
f830c5b264 | ||
| 95635719d6 | |||
|
|
e57f989c68 | ||
| 3a54f48c65 | |||
| c0f21612fc | |||
|
|
8007b2ae88 | ||
|
|
000f1e3851 | ||
|
|
7bcec7872f | ||
|
|
449dafcb89 | ||
|
|
af513debec | ||
|
|
5759629d34 | ||
|
|
0ab4a4f053 | ||
|
|
163be72b86 | ||
|
|
20b07aeea8 | ||
|
|
168923d3da | ||
|
|
b177e8de2c | ||
|
|
cd3adf1a96 | ||
|
|
8a987b89c5 | ||
|
|
552bc8fba8 | ||
|
|
7b89add86e |
18
.editorconfig
Normal file
18
.editorconfig
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# 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
Normal file
69
.github/workflows/phpunit.yml
vendored
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
---
|
||||||
|
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
|
||||||
|
name: PHPUnit
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master, main]
|
||||||
|
pull_request:
|
||||||
|
branches: [master, main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
statuses: write
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: true
|
||||||
|
matrix:
|
||||||
|
php: [ '8.0', '8.1', '8.2', '8.3' ]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup PHP
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: ${{ matrix.php }}
|
||||||
|
tools: composer
|
||||||
|
extensions: json, dom, curl, libxml, mbstring
|
||||||
|
coverage: xdebug
|
||||||
|
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,4 +12,5 @@ Homestead.yaml
|
|||||||
npm-debug.log
|
npm-debug.log
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
.env
|
.env
|
||||||
.phpunit.result.cache
|
*.cache
|
||||||
|
composer.lock
|
||||||
|
|||||||
12
.travis.yml
12
.travis.yml
@@ -7,14 +7,14 @@ env:
|
|||||||
matrix:
|
matrix:
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
include:
|
include:
|
||||||
- php: 7.1
|
- php: 8.0
|
||||||
- php: 7.1
|
- php: 8.0
|
||||||
env: setup=lowest
|
env: setup=lowest
|
||||||
- php: 7.2
|
- php: 8.1
|
||||||
- php: 7.2
|
- php: 8.1
|
||||||
env: setup=lowest
|
env: setup=lowest
|
||||||
- php: 7.3
|
- php: 8.2
|
||||||
- php: 7.3
|
- php: 8.2
|
||||||
env: setup=lowest
|
env: setup=lowest
|
||||||
|
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|||||||
@@ -1,34 +1,54 @@
|
|||||||
{
|
{
|
||||||
"name": "distinctm/laravel-data-sync",
|
"name": "nullthoughts/laravel-data-sync",
|
||||||
"description": "Laravel utility to keep records synced between environments through source control",
|
"description": "Laravel utility to keep records synced between environments through source control",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "distinctm",
|
"name": "nullthoughts",
|
||||||
"email": "jani@marketdistinctly.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"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"orchestra/testbench": "^3.7"
|
"orchestra/testbench": "^7|^8",
|
||||||
|
"rector/rector": "^1",
|
||||||
|
"ergebnis/composer-normalize": "^2",
|
||||||
|
"vimeo/psalm": "^5",
|
||||||
|
"psalm/plugin-laravel": "^2",
|
||||||
|
"laravel/pint": "^1"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4" : {
|
"psr-4": {
|
||||||
"distinctm\\LaravelDataSync\\": "src/"
|
"nullthoughts\\LaravelDataSync\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4" : {
|
"psr-4": {
|
||||||
"distinctm\\LaravelDataSync\\Tests\\": "tests/"
|
"nullthoughts\\LaravelDataSync\\Tests\\": "tests/",
|
||||||
|
"nullthoughts\\LaravelDataSync\\Tests\\Fakes\\": "tests/fakes/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
"distinctm\\LaravelDataSync\\DataSyncBaseServiceProvider"
|
"nullthoughts\\LaravelDataSync\\DataSyncBaseServiceProvider"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"ergebnis/composer-normalize": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4630
composer.lock
generated
4630
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'path' => base_path('sync'),
|
'path' => base_path('sync'),
|
||||||
'order' => [
|
'order' => [
|
||||||
//
|
//
|
||||||
],
|
],
|
||||||
|
|||||||
27
phpunit.xml
27
phpunit.xml
@@ -1,25 +1,16 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit backupGlobals="false"
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
|
||||||
backupStaticAttributes="false"
|
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
|
||||||
bootstrap="vendor/autoload.php"
|
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
|
||||||
colors="true"
|
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
||||||
convertErrorsToExceptions="true"
|
<coverage processUncoveredFiles="true">
|
||||||
convertNoticesToExceptions="true"
|
<include>
|
||||||
convertWarningsToExceptions="true"
|
<directory suffix=".php">./src</directory>
|
||||||
processIsolation="false"
|
</include>
|
||||||
stopOnFailure="false">
|
</coverage>
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="Unit">
|
<testsuite name="Unit">
|
||||||
<directory suffix="Test.php">./tests/Unit</directory>
|
<directory suffix="Test.php">./tests/Unit</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
|
||||||
<testsuite name="Feature">
|
|
||||||
<directory suffix="Test.php">./tests/Feature</directory>
|
|
||||||
</testsuite>
|
|
||||||
</testsuites>
|
</testsuites>
|
||||||
<filter>
|
|
||||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
|
||||||
<directory suffix=".php">./src</directory>
|
|
||||||
</whitelist>
|
|
||||||
</filter>
|
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
17
psalm.xml
Normal file
17
psalm.xml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?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>
|
||||||
38
readme.md
38
readme.md
@@ -1,6 +1,6 @@
|
|||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://packagist.org/packages/distinctm/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/distinctm/laravel-data-sync/d/total.svg" alt="Total Downloads"></a>
|
<a href="https://packagist.org/packages/nullthoughts/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/nullthoughts/laravel-data-sync/d/total.svg" alt="Total Downloads"></a>
|
||||||
<a href="https://packagist.org/packages/distinctm/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/distinctm/laravel-data-sync/v/stable.svg" alt="Latest Stable Version"></a>
|
<a href="https://packagist.org/packages/nullthoughts/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/nullthoughts/laravel-data-sync/v/stable.svg" alt="Latest Stable Version"></a>
|
||||||
<a href="https://travis-ci.com/nullthoughts/laravel-data-sync"><img src="https://api.travis-ci.com/nullthoughts/laravel-data-sync.svg?branch=master" alt="Travis CI Build Status: Master"></a>
|
<a href="https://travis-ci.com/nullthoughts/laravel-data-sync"><img src="https://api.travis-ci.com/nullthoughts/laravel-data-sync.svg?branch=master" alt="Travis CI Build Status: Master"></a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@@ -8,10 +8,38 @@
|
|||||||
|
|
||||||
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
|
||||||
composer require distinctm/laravel-data-sync
|
composer require nullthoughts/laravel-data-sync
|
||||||
```
|
```
|
||||||
|
|
||||||
Or add this line in your `composer.json`, inside of the `require` section:
|
Or add this line in your `composer.json`, inside of the `require` section:
|
||||||
@@ -19,14 +47,14 @@ Or add this line in your `composer.json`, inside of the `require` section:
|
|||||||
``` json
|
``` json
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"distinctm/laravel-data-sync": "^1.0",
|
"nullthoughts/laravel-data-sync": "^1.0",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
then run ` composer install `
|
then run ` composer install `
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
- Run `php artisan vendor:publish --provider="distinctm\LaravelDataSync\DataSyncBaseServiceProvider" --tag="data-sync-config"` to publish config file. Specify directory for sync data files (default is a new sync directory in the project root)
|
- Run `php artisan vendor:publish --provider="nullthoughts\LaravelDataSync\DataSyncBaseServiceProvider" --tag="data-sync-config"` to publish config file. Specify directory for sync data files (default is a new sync directory in the project root)
|
||||||
- Create a JSON file for each model, using the model name as the filename. Example: Product.json would update the Product model
|
- Create a JSON file for each model, using the model name as the filename. Example: Product.json would update the Product model
|
||||||
- Use nested arrays in place of hardcoded IDs for relationships
|
- Use nested arrays in place of hardcoded IDs for relationships
|
||||||
- Run `php artisan data:sync` (or `php artisan data:sync --model={model}` with the model flag to specify a model)
|
- Run `php artisan data:sync` (or `php artisan data:sync --model={model}` with the model flag to specify a model)
|
||||||
|
|||||||
16
rector.php
Normal file
16
rector.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?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,12 +1,14 @@
|
|||||||
# Dev
|
# Dev
|
||||||
- Check if null return from json decode (invalid JSON)
|
|
||||||
- Check if updateOrCreate returns anything
|
- Check if null return from json decode (invalid JSON)
|
||||||
|
- 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
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Console\Commands;
|
namespace nullthoughts\LaravelDataSync\Console\Commands;
|
||||||
|
|
||||||
use distinctm\LaravelDataSync\Updater;
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
|
use nullthoughts\LaravelDataSync\Updater;
|
||||||
|
|
||||||
class Sync extends Command
|
class Sync extends Command
|
||||||
{
|
{
|
||||||
@@ -11,7 +11,8 @@ class Sync extends Command
|
|||||||
|
|
||||||
protected $description = 'Update Models with respective sync data files';
|
protected $description = 'Update Models with respective sync data files';
|
||||||
|
|
||||||
public function handle()
|
/** @psalm-api */
|
||||||
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$path = $this->option('path');
|
$path = $this->option('path');
|
||||||
$model = $this->option('model');
|
$model = $this->option('model');
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\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()
|
public function boot(): void
|
||||||
{
|
{
|
||||||
if ($this->app->runningInConsole()) {
|
if ($this->app->runningInConsole()) {
|
||||||
$this->registerPublishing();
|
$this->registerPublishing();
|
||||||
@@ -16,11 +18,11 @@ class DataSyncBaseServiceProvider extends ServiceProvider
|
|||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->commands([
|
$this->commands([
|
||||||
Console\Commands\Sync::class,
|
Sync::class,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function registerPublishing()
|
protected function registerPublishing(): void
|
||||||
{
|
{
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__.'/../config/data-sync.php' => config_path('data-sync.php'),
|
__DIR__.'/../config/data-sync.php' => config_path('data-sync.php'),
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Exceptions;
|
namespace nullthoughts\LaravelDataSync\Exceptions;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Throwable;
|
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);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Exceptions;
|
namespace nullthoughts\LaravelDataSync\Exceptions;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Exceptions;
|
namespace nullthoughts\LaravelDataSync\Exceptions;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Exceptions;
|
namespace nullthoughts\LaravelDataSync\Exceptions;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Throwable;
|
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);
|
||||||
|
|
||||||
|
|||||||
142
src/Updater.php
142
src/Updater.php
@@ -1,36 +1,70 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync;
|
namespace nullthoughts\LaravelDataSync;
|
||||||
|
|
||||||
use distinctm\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
|
||||||
use distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
|
|
||||||
use distinctm\LaravelDataSync\Exceptions\NoCriteriaException;
|
|
||||||
use distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
class Updater
|
class Updater
|
||||||
{
|
{
|
||||||
|
private string $directory;
|
||||||
|
|
||||||
|
private Collection $files;
|
||||||
|
|
||||||
|
private bool $remote;
|
||||||
|
|
||||||
|
private string $disk;
|
||||||
|
|
||||||
|
private string $baseNamespace = '\\App\\';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 string $disk
|
||||||
*
|
*
|
||||||
* @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
||||||
*/
|
*/
|
||||||
public function __construct($path = null, $model = null)
|
public function __construct($path = null, $model = null, $remote = false, $disk = 's3')
|
||||||
{
|
{
|
||||||
$directory = $this->getDirectory($path);
|
$this->remote = $remote;
|
||||||
$this->files = $this->getFiles($directory, $model);
|
$this->disk = $disk;
|
||||||
|
|
||||||
|
$this->directory = $this->getDirectory($path);
|
||||||
|
$this->files = $this->getFiles($this->directory, $model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override the default namespace for the class.
|
||||||
|
*
|
||||||
|
* @psalm-api
|
||||||
|
*/
|
||||||
|
public function setNamespace(string $namespace): void
|
||||||
|
{
|
||||||
|
$this->baseNamespace = $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()
|
||||||
{
|
{
|
||||||
@@ -39,7 +73,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 $e) {
|
} catch (\ErrorException) {
|
||||||
$model = pathinfo($file, PATHINFO_FILENAME);
|
$model = pathinfo($file, PATHINFO_FILENAME);
|
||||||
|
|
||||||
throw new ErrorUpdatingModelException(ucwords($model));
|
throw new ErrorUpdatingModelException(ucwords($model));
|
||||||
@@ -50,11 +84,11 @@ 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
|
|
||||||
*
|
|
||||||
* @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
|
*
|
||||||
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||||
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
||||||
*/
|
*/
|
||||||
protected function syncModel(string $file)
|
protected function syncModel(string $file)
|
||||||
{
|
{
|
||||||
@@ -79,29 +113,28 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Get directory path for sync files.
|
* Get directory path for sync files.
|
||||||
*
|
*
|
||||||
* @param $path
|
|
||||||
*
|
|
||||||
* @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
|
*
|
||||||
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
|
||||||
*/
|
*/
|
||||||
protected function getDirectory($path)
|
protected function getDirectory(?string $path)
|
||||||
{
|
{
|
||||||
$directory = $path ?? config('data-sync.path', base_path('sync'));
|
$directory = $path ?? config('data-sync.path', base_path('sync'));
|
||||||
|
|
||||||
if (!file_exists($directory)) {
|
if ($this->directoryMissingLocally($directory) || $this->directoryMissingRemotely($directory)) {
|
||||||
throw new FileDirectoryNotFoundException();
|
throw new FileDirectoryNotFoundException;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $directory;
|
$this->directory = $directory;
|
||||||
|
|
||||||
|
return $this->directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get list of files in directory.
|
* Get list of files in directory.
|
||||||
*
|
*
|
||||||
* @param string $directory
|
* @param string|null $model
|
||||||
* @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)
|
||||||
@@ -110,10 +143,17 @@ class Updater
|
|||||||
return Collection::wrap($directory.'/'.$model.'.json');
|
return Collection::wrap($directory.'/'.$model.'.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
return collect(File::files($directory))
|
$files = ($this->remote) ? Storage::disk($this->disk)->files($directory) : File::files($directory);
|
||||||
|
|
||||||
|
return collect($files)
|
||||||
->filter(function ($file) {
|
->filter(function ($file) {
|
||||||
return pathinfo($file, PATHINFO_EXTENSION) == 'json';
|
return pathinfo($file, PATHINFO_EXTENSION) == 'json';
|
||||||
})->map(function ($path) {
|
})->map(function ($path) {
|
||||||
|
|
||||||
|
if (is_string($path)) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
return $path->getPathname();
|
return $path->getPathname();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -121,7 +161,6 @@ 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
|
||||||
*/
|
*/
|
||||||
@@ -146,11 +185,10 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Filter record criteria.
|
* Filter record criteria.
|
||||||
*
|
*
|
||||||
* @param stdClass $record
|
|
||||||
*
|
|
||||||
* @throws \distinctm\LaravelDataSync\Exceptions\NoCriteriaException
|
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
|
*
|
||||||
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException
|
||||||
*/
|
*/
|
||||||
protected function getCriteria(stdClass $record)
|
protected function getCriteria(stdClass $record)
|
||||||
{
|
{
|
||||||
@@ -159,7 +197,7 @@ class Updater
|
|||||||
});
|
});
|
||||||
|
|
||||||
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) {
|
||||||
@@ -170,7 +208,6 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Filter record values.
|
* Filter record values.
|
||||||
*
|
*
|
||||||
* @param stdClass $record
|
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
@@ -192,27 +229,28 @@ 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 '\\App\\'.Str::studly(pathinfo($name, PATHINFO_FILENAME));
|
return $this->baseNamespace.Str::studly(pathinfo($name, PATHINFO_FILENAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses JSON from file and returns collection.
|
* Parses JSON from file and returns collection.
|
||||||
*
|
*
|
||||||
* @param string $file
|
|
||||||
*
|
|
||||||
* @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
|
*
|
||||||
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||||
|
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
|
||||||
*/
|
*/
|
||||||
protected function getRecords(string $file)
|
protected function getRecords(string $file)
|
||||||
{
|
{
|
||||||
$records = collect(json_decode(File::get($file)));
|
$fetchedFile = ($this->remote) ? Storage::disk($this->disk)->get($file) : File::get($file);
|
||||||
|
|
||||||
|
$records = collect(json_decode($fetchedFile));
|
||||||
|
|
||||||
if ($records->isEmpty()) {
|
if ($records->isEmpty()) {
|
||||||
throw new NoRecordsInvalidJSONException($file);
|
throw new NoRecordsInvalidJSONException($file);
|
||||||
@@ -224,8 +262,7 @@ 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)
|
||||||
@@ -236,8 +273,6 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Return ID for nested key-value pairs.
|
* Return ID for nested key-value pairs.
|
||||||
*
|
*
|
||||||
* @param string $key
|
|
||||||
* @param stdClass $values
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -259,7 +294,6 @@ class Updater
|
|||||||
/**
|
/**
|
||||||
* Detect nested objects and resolve them.
|
* Detect nested objects and resolve them.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Support\Collection $record
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -273,4 +307,22 @@ class Updater
|
|||||||
return [$key => $value];
|
return [$key => $value];
|
||||||
})->toArray();
|
})->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Illuminate\Config\Repository $directory
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function directoryMissingLocally($directory)
|
||||||
|
{
|
||||||
|
return ! $this->remote && ! file_exists($directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Illuminate\Config\Repository $directory
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function directoryMissingRemotely($directory)
|
||||||
|
{
|
||||||
|
return $this->remote && ! Storage::disk($this->disk)->exists($directory);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\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
|
||||||
{
|
{
|
||||||
@@ -10,7 +11,7 @@ class Roles extends Model
|
|||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
public function supervisor()
|
public function supervisor(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Supervisor::class);
|
return $this->belongsTo(Supervisor::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\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
|
||||||
{
|
{
|
||||||
@@ -10,7 +11,7 @@ class Supervisor extends Model
|
|||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
public function roles()
|
public function roles(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Roles::class);
|
return $this->hasMany(Roles::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Tests;
|
namespace nullthoughts\LaravelDataSync\Tests;
|
||||||
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
class TestCase extends \Orchestra\Testbench\TestCase
|
class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
{
|
{
|
||||||
protected function getEnvironmentSetUp($app)
|
/** @psalm-suppress PropertyNotSetInConstructor */
|
||||||
|
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', function (Blueprint $table) {
|
Schema::create('supervisors', static function (Blueprint $table): void {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
});
|
});
|
||||||
|
|
||||||
Schema::create('roles', function (Blueprint $table) {
|
Schema::create('roles', static function (Blueprint $table): void {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('slug');
|
$table->string('slug');
|
||||||
$table->unsignedInteger('supervisor_id')->nullable();
|
$table->unsignedInteger('supervisor_id')->nullable();
|
||||||
|
|||||||
164
tests/Unit/UpdaterRemoteTest.php
Normal file
164
tests/Unit/UpdaterRemoteTest.php
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace nullthoughts\LaravelDataSync\Tests\Unit;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
||||||
|
use nullthoughts\LaravelDataSync\Tests\Fakes\UpdaterFake;
|
||||||
|
use nullthoughts\LaravelDataSync\Tests\Roles;
|
||||||
|
use nullthoughts\LaravelDataSync\Tests\Supervisor;
|
||||||
|
use nullthoughts\LaravelDataSync\Tests\TestCase;
|
||||||
|
|
||||||
|
class UpdaterRemoteTest extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Storage::fake('s3');
|
||||||
|
Storage::disk('s3')->put('test-data/roles.json', File::get(__DIR__.'/../test-data/roles.json'));
|
||||||
|
foreach (File::directories(__DIR__.'/../test-data/') as $directory) {
|
||||||
|
$files = File::files($directory);
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
Storage::disk('s3')->put('test-data/'.basename($directory).'/'.$file->getRelativePathname(), File::get($file->getPathname()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_adds_roles_to_the_database_in_remote()
|
||||||
|
{
|
||||||
|
$updater = new UpdaterFake('test-data', 'roles', true, 's3');
|
||||||
|
|
||||||
|
$updater->run();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_default_to_configuration_in_remote()
|
||||||
|
{
|
||||||
|
config()->set('data-sync.path', 'test-data');
|
||||||
|
|
||||||
|
$updater = new UpdaterFake(null, null, true, 's3');
|
||||||
|
|
||||||
|
$updater->run();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_update_an_existing_record_in_remote()
|
||||||
|
{
|
||||||
|
config()->set('data-sync.path', 'test-data');
|
||||||
|
(new UpdaterFake(null, null, true, 's3'))->run();
|
||||||
|
|
||||||
|
config()->set('data-sync.path', 'test-data/valid');
|
||||||
|
(new UpdaterFake(null, null, true, 's3'))->run();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
||||||
|
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
||||||
|
$this->assertDatabaseHas('roles', ['category' => 'changed']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_can_update_the_relationship_in_remote()
|
||||||
|
{
|
||||||
|
$supervisor = Supervisor::create([
|
||||||
|
'name' => 'CEO',
|
||||||
|
]);
|
||||||
|
|
||||||
|
config()->set('data-sync.path', 'test-data/relationship');
|
||||||
|
(new UpdaterFake(null, null, true, 's3'))->run();
|
||||||
|
|
||||||
|
$this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
|
||||||
|
$this->assertTrue($supervisor->is(Roles::first()->supervisor));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*
|
||||||
|
* @group current
|
||||||
|
*/
|
||||||
|
public function exception_is_thrown_if_the_directory_does_not_exists(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
new UpdaterFake(null, null, true, 's3');
|
||||||
|
|
||||||
|
$this->fail('exception was thrown');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function invalid_json_throws_an_exception_in_remote(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$updater = new UpdaterFake('test-data/invalid-json', null, true, 's3');
|
||||||
|
$updater->run();
|
||||||
|
|
||||||
|
$this->fail('exception was thrown');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->assertStringContainsString('No records or invalid JSON for', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function the_json_must_contain_a_key_with_an_underscore_in_remote(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$updater = new UpdaterFake('test-data/no-criteria', null, true, 's3');
|
||||||
|
$updater->run();
|
||||||
|
|
||||||
|
$this->fail('exception was thrown');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->assertEquals('No criteria/attributes detected', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function order_of_imports_can_be_defined_in_config_in_remote(): void
|
||||||
|
{
|
||||||
|
config()->set('data-sync.order', [
|
||||||
|
'Supervisor',
|
||||||
|
'Roles',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$updater = new UpdaterFake('test-data/ordered', null, true, 's3');
|
||||||
|
$updater->run();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
|
||||||
|
$this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote(): void
|
||||||
|
{
|
||||||
|
config()->set('data-sync.order', [
|
||||||
|
'Roles',
|
||||||
|
'Supervisor',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->expectException(ErrorUpdatingModelException::class);
|
||||||
|
|
||||||
|
$updater = new UpdaterFake('test-data/ordered', null, true, 's3');
|
||||||
|
$updater->run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function it_ignores_non_json_files_in_remote(): void
|
||||||
|
{
|
||||||
|
$updater = new UpdaterFake('test-data/not-json', null, true, 's3');
|
||||||
|
$updater->run();
|
||||||
|
|
||||||
|
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Tests;
|
namespace nullthoughts\LaravelDataSync\Tests\Unit;
|
||||||
|
|
||||||
use distinctm\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
|
||||||
use distinctm\LaravelDataSync\Tests\fakes\UpdaterFake;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
|
||||||
|
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
|
||||||
|
use nullthoughts\LaravelDataSync\Tests\Roles;
|
||||||
|
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()
|
public function it_adds_roles_to_the_database(): void
|
||||||
{
|
{
|
||||||
$updater = new UpdaterFake(__DIR__.'/../test-data', 'roles');
|
$updater = new UpdaterFake($this->testDataPath, 'roles');
|
||||||
|
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
@@ -23,9 +26,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', __DIR__.'/../test-data');
|
config()->set('data-sync.path', $this->testDataPath);
|
||||||
|
|
||||||
$updater = new UpdaterFake();
|
$updater = new UpdaterFake;
|
||||||
|
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
@@ -37,11 +40,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', __DIR__.'/../test-data');
|
config()->set('data-sync.path', $this->testDataPath);
|
||||||
(new UpdaterFake())->run();
|
(new UpdaterFake)->run();
|
||||||
|
|
||||||
config()->set('data-sync.path', __DIR__.'/../test-data/valid');
|
config()->set('data-sync.path', $this->testDataPath.'/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']);
|
||||||
@@ -49,24 +52,24 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_can_update_the_relationship()
|
public function it_can_update_the_relationship(): void
|
||||||
{
|
{
|
||||||
$supervisor = Supervisor::create([
|
$supervisor = Supervisor::create([
|
||||||
'name' => 'CEO',
|
'name' => 'CEO',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
config()->set('data-sync.path', __DIR__.'/../test-data/relationship', 'roles');
|
config()->set('data-sync.path', $this->testDataPath.'/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()
|
public function exception_is_thrown_if_the_directory_does_not_exists(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
new UpdaterFake();
|
new UpdaterFake;
|
||||||
|
|
||||||
$this->fail('exception was thrown');
|
$this->fail('exception was thrown');
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@@ -75,23 +78,23 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function invalid_json_throws_an_exception()
|
public function invalid_json_throws_an_exception(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$updater = new UpdaterFake(__DIR__.'/../test-data/invalid-json');
|
$updater = new UpdaterFake($this->testDataPath.'/invalid-json');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->fail('exception was thrown');
|
$this->fail('exception was thrown');
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->assertContains('No records or invalid JSON for', $e->getMessage());
|
$this->assertStringContainsString('No records or invalid JSON for', $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function the_json_must_contain_a_key_with_an_underscore()
|
public function the_json_must_contain_a_key_with_an_underscore(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$updater = new UpdaterFake(__DIR__.'/../test-data/no-criteria');
|
$updater = new UpdaterFake($this->testDataPath.'/no-criteria');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->fail('exception was thrown');
|
$this->fail('exception was thrown');
|
||||||
@@ -101,22 +104,21 @@ class UpdaterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function order_of_imports_can_be_defined_in_config()
|
public function order_of_imports_can_be_defined_in_config(): void
|
||||||
{
|
{
|
||||||
config()->set('data-sync.order', [
|
config()->set('data-sync.order', [
|
||||||
'Supervisor',
|
'Supervisor',
|
||||||
'Roles',
|
'Roles',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$updater = new UpdaterFake(__DIR__.'/../test-data/ordered');
|
$updater = new UpdaterFake($this->testDataPath.'/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']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
public function exception_is_thrown_if_imports_are_in_incorrect_order(): void
|
||||||
public function exception_is_thrown_if_imports_are_in_incorrect_order()
|
|
||||||
{
|
{
|
||||||
config()->set('data-sync.order', [
|
config()->set('data-sync.order', [
|
||||||
'Roles',
|
'Roles',
|
||||||
@@ -125,14 +127,14 @@ class UpdaterTest extends TestCase
|
|||||||
|
|
||||||
$this->expectException(ErrorUpdatingModelException::class);
|
$this->expectException(ErrorUpdatingModelException::class);
|
||||||
|
|
||||||
$updater = new UpdaterFake(__DIR__.'/../test-data/ordered');
|
$updater = new UpdaterFake($this->testDataPath.'/ordered');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function it_ignores_non_json_files()
|
public function it_ignores_non_json_files(): void
|
||||||
{
|
{
|
||||||
$updater = new UpdaterFake(__DIR__.'/../test-data/not-json');
|
$updater = new UpdaterFake($this->testDataPath.'/not-json');
|
||||||
$updater->run();
|
$updater->run();
|
||||||
|
|
||||||
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
|
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace distinctm\LaravelDataSync\Tests\Fakes;
|
namespace nullthoughts\LaravelDataSync\Tests\Fakes;
|
||||||
|
|
||||||
use distinctm\LaravelDataSync\Updater;
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use nullthoughts\LaravelDataSync\Updater;
|
||||||
|
|
||||||
class UpdaterFake extends Updater
|
class UpdaterFake extends Updater
|
||||||
{
|
{
|
||||||
protected function getModel(string $name)
|
protected function getModel(string $name)
|
||||||
{
|
{
|
||||||
return '\\distinctm\\LaravelDataSync\\Tests\\'.Str::studly(
|
return '\\nullthoughts\\LaravelDataSync\\Tests\\'.Str::studly(
|
||||||
pathinfo($name, PATHINFO_FILENAME)
|
pathinfo($name, PATHINFO_FILENAME)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1 @@
|
|||||||
[
|
[]
|
||||||
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -2,4 +2,4 @@
|
|||||||
{
|
{
|
||||||
"string": "update-student-records"
|
"string": "update-student-records"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
"name": "CEO"
|
"name": "CEO"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
"name": "CEO"
|
"name": "CEO"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2,4 +2,4 @@
|
|||||||
{
|
{
|
||||||
"_name": "CEO"
|
"_name": "CEO"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
"name": "CEO"
|
"name": "CEO"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -11,4 +11,4 @@
|
|||||||
"_slug": "destroy-ferrari",
|
"_slug": "destroy-ferrari",
|
||||||
"category": "cars"
|
"category": "cars"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -11,4 +11,4 @@
|
|||||||
"_slug": "destroy-ferrari",
|
"_slug": "destroy-ferrari",
|
||||||
"category": "changed"
|
"category": "changed"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user