Skip to content

Commit

Permalink
Connect document copies with their collection (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeto143 authored Jan 12, 2023
1 parent 46ff235 commit c95a8d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function has(string $offset)
public function with(array $fields)
{
$document = new static();
$document->collection = $this->collection;
$document->document = [];

foreach ($fields as $field) {
Expand All @@ -108,6 +109,7 @@ public function with(array $fields)
public function without(array $fields)
{
$document = new static();
$document->collection = $this->collection;
$document->document = [];

foreach ($this->document as $key => $value) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Xenus\Tests\Tests;

use Xenus\Document;
use Xenus\Tests\Support\SetupCollectionTest;

class DocumentTest extends \PHPUnit\Framework\TestCase
{
use SetupCollectionTest;

public function test_has_method()
{
$document = new Document([
Expand Down Expand Up @@ -85,6 +88,8 @@ public function test_with_method()
protected $withId = true;
};

$document->connect($this->cities);

$document = $document->fill([
'name' => 'Antoine', 'city' => 'Paris'
]);
Expand All @@ -100,6 +105,10 @@ public function test_with_method()
$this->assertEquals(
['name' => 'Antoine'], $document->with(['name', 'unknown'])->toArray()
);

$this->assertEquals(
$document->collection(), $document->with([])->collection()
);
}

public function test_without_method()
Expand All @@ -108,6 +117,8 @@ public function test_without_method()
protected $withId = true;
};

$document->connect($this->cities);

$document = $document->fill([
'name' => 'Antoine', 'city' => 'Paris'
]);
Expand All @@ -119,6 +130,10 @@ public function test_without_method()
$this->assertEquals(
['name' => 'Antoine', 'city' => 'Paris'], $document->without(['_id'])->toArray()
);

$this->assertEquals(
$document->collection(), $document->without([])->collection()
);
}

public function test_get_from_getter_method()
Expand Down

0 comments on commit c95a8d6

Please sign in to comment.