1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/tests/Unit/TaskSortingTest.php

64 lines
1.8 KiB
PHP
Raw Normal View History

2021-01-05 05:41:43 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2021-01-05 05:41:43 +01:00
*/
2021-01-05 05:41:43 +01:00
namespace Tests\Unit;
use Tests\TestCase;
/**
* @test
*/
class TaskSortingTest extends TestCase
{
public $collection;
protected function setUp() :void
2021-01-05 05:41:43 +01:00
{
parent::setUp();
$this->collection = collect([
['id' => 1, 'name' =>'pizza', 'order' => 9999],
['id' => 2, 'name' =>'pineapple', 'order' => 9999],
['id' => 3, 'name' =>'ethereum', 'order' => 9999],
['id' => 4, 'name' =>'bitcoin', 'order' => 9999],
['id' => 5, 'name' =>'zulu', 'order' => 9999],
['id' => 6, 'name' =>'alpha', 'order' => 9999],
['id' => 7, 'name' =>'ninja', 'order' => 9999],
]);
}
public function testSorting()
{
$index = 3;
$item = $this->collection->where('id', 7)->first();
$new_collection = $this->collection->reject(function ($task) use ($item) {
2021-01-05 05:41:43 +01:00
return $item['id'] == $task['id'];
});
$sorted_tasks = $new_collection->filter(function ($task, $key) use ($index) {
2021-01-05 05:41:43 +01:00
return $key < $index;
})->push($item)->merge($new_collection->filter(function ($task, $key) use ($index) {
2021-01-05 05:41:43 +01:00
return $key >= $index;
}))->map(function ($item, $key) {
2021-01-05 05:41:43 +01:00
$item['order'] = $key;
2021-01-05 05:41:43 +01:00
return $item;
});
$index_item = $sorted_tasks->splice($index, 1)->all();
$this->assertEquals($sorted_tasks->first()['name'], 'pizza');
$this->assertEquals($sorted_tasks->last()['name'], 'alpha');
$this->assertEquals($index_item[0]['name'], 'ninja');
2021-01-05 05:41:43 +01:00
}
}