Skip to content

Commit

Permalink
Add method to attach result at specific index
Browse files Browse the repository at this point in the history
  • Loading branch information
stratedge committed Jan 28, 2018
1 parent 179afec commit 4509851
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Result/AttachAtIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Unit\Result;

use Stratedge\Wye\Wye;

class AttachAtIndexTest extends \Tests\TestCase
{
public function testAppendsWithoutIndex()
{
$result = Wye::makeResult()->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());
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Result/AttachTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Unit\Result;

use Stratedge\Wye\Wye;

class AttachTest extends \Tests\TestCase
{
public function testAppendsResult()
{
$result = Wye::makeResult()->attach();
$new_result = Wye::makeResult()->attach();

$this->assertSame([$result, $new_result], Wye::getResults());
}
}

0 comments on commit 4509851

Please sign in to comment.