Skip to content

Commit

Permalink
✨ Add isExporting and isImporting methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieutu committed Sep 26, 2017
1 parent 2891596 commit 79e5ad3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/JsonExportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public function getJsonExportableRelations(): array;
public function exportToJson($jsonOptions = 0): string;

public function exportToCollection(): Collection;

public function isExporting(): bool;
}
2 changes: 2 additions & 0 deletions src/Contracts/JsonImportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public function getJsonImportableRelations(): array;
public function getJsonImportableAttributes(): array;

public static function importFromJson($objectsToCreate): void;

public function isImporting(): bool;
}
5 changes: 5 additions & 0 deletions src/Traits/JsonExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function getJsonExportableRelations(): array
{
return $this->jsonExportableRelations ?? RelationsInModelFinder::hasOneOrMany($this);
}

public function isExporting(): bool
{
return collect(debug_backtrace())->contains('class', ExporterHelper::class);
}
}
5 changes: 5 additions & 0 deletions src/Traits/JsonImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function getJsonImportableRelations(): array
?? $this->jsonExportableRelations
?? RelationsInModelFinder::hasOneOrMany($this);
}

public function isImporting(): bool
{
return collect(debug_backtrace())->contains('class', ImporterHelper::class);
}
}
25 changes: 25 additions & 0 deletions tests/JsonExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ public function testExportToCollection()
);
}

public function testIsExportingMethod()
{
$this->setDatabase();

$foo = (new class extends Foo {
protected $fillable = ['author'];
protected $table = 'foos';

public function getAuthorAttribute($value)
{
if ($this->isExporting()) {
throw new \Exception("Is exporting ok !");
}

return $value;
}
})->firstOrFail();

$this->assertEquals('Mathieu TUDISCO', $foo->author);

$this->expectExceptionMessage('Is exporting ok !');
$foo->exportToJson();
}


protected function setDatabase()
{
(new Foo)->create(['author' => 'Mathieu TUDISCO', 'username' => '@mathieutu'])
Expand Down
29 changes: 29 additions & 0 deletions tests/JsonImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ public function testImportWithCustomRelationsAndAttributes()
$this->assertBazsAreImported();
}

public function testIsImportingMethod()
{
$import = [
'author' => 'Mathieu',
];

$foo = new class extends Foo {
protected $fillable = ['author'];
protected $table = 'foos';

public function setAuthorAttribute()
{
if ($this->isImporting()) {
throw new \Exception("Is importing ok !");
}

$this->attributes['author'] = 'not importing';
}
};

$foo->author = 'test';
$this->assertEquals('not importing', $foo->author);

$this->expectExceptionMessage('Is importing ok !');

$foo->setJsonImportableAttributesForTests(array_keys($import))
->instanceImportForTests($import);
}

public function testImportNonImportableObjects()
{
$baz = [
Expand Down

0 comments on commit 79e5ad3

Please sign in to comment.