From 450985159f5c2e6798cc80e45fc24e9bff7f5632 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 28 Jan 2018 15:06:36 -0500 Subject: [PATCH] Add method to attach result at specific index --- src/Result.php | 23 ++++++++++++++++++ tests/Unit/Result/AttachAtIndexTest.php | 32 +++++++++++++++++++++++++ tests/Unit/Result/AttachTest.php | 16 +++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 tests/Unit/Result/AttachAtIndexTest.php create mode 100644 tests/Unit/Result/AttachTest.php diff --git a/src/Result.php b/src/Result.php index fe67e6a..e572d3d 100644 --- a/src/Result.php +++ b/src/Result.php @@ -277,9 +277,32 @@ public function addRows(array $rows) return $this; } + /** + * Append this result to the list of results. + * + * @return self + */ public function attach() { $this->wye()->addResult($this); return $this; } + + /** + * Attach this result at the specified index. Without an index the result + * will be appended to the list of results. + * + * @param integer|null $index + * @return self + */ + public function attachAtIndex($index = null) + { + if (is_null($index)) { + return $this->attach(); + } + + $this->getWye()->addResultAtIndex($this, $index); + + return $this; + } } diff --git a/tests/Unit/Result/AttachAtIndexTest.php b/tests/Unit/Result/AttachAtIndexTest.php new file mode 100644 index 0000000..c1445cc --- /dev/null +++ b/tests/Unit/Result/AttachAtIndexTest.php @@ -0,0 +1,32 @@ +attach(); + $new_result = Wye::makeResult()->attachAtIndex(); + + $this->assertSame([$result, $new_result], Wye::getResults()); + } + + public function testAddsResultAtIndex() + { + $result = Wye::makeResult()->attach(); + $new_result = Wye::makeResult()->attachAtIndex(3); + + $this->assertSame([0 => $result, 3 => $new_result], Wye::getResults()); + } + + public function testOverwritesResultAtIndex() + { + $result = Wye::makeResult()->attach(); + $new_result = Wye::makeResult()->attachAtIndex(0); + + $this->assertSame([$new_result], Wye::getResults()); + } +} diff --git a/tests/Unit/Result/AttachTest.php b/tests/Unit/Result/AttachTest.php new file mode 100644 index 0000000..5971047 --- /dev/null +++ b/tests/Unit/Result/AttachTest.php @@ -0,0 +1,16 @@ +attach(); + $new_result = Wye::makeResult()->attach(); + + $this->assertSame([$result, $new_result], Wye::getResults()); + } +}