/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { parent::boot($router); Route::bind('items', function ($id) { return Item::forCurrentUser()->findOrFail($id); }); Route::bind('categories', function ($id) { return Category::forCurrentUser()->findOrFail($id); }); }
/** * @test */ public function it_can_show_an_item() { $this->logInUser(); $item = Item::forCurrentUser()->first(); $response = $this->call('GET', '/api/items/' . $item->id); $content = json_decode($response->getContent(), true); // dd($content); $this->checkItemKeysExist($content); $this->checkItemKeysExist($content['children'][0]); $this->checkItemKeysExist($content['breadcrumb'][0]); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); }
/** * Calculate what the index of an item should be if it is not specified, * in order to add it as the last child of the parent. * @param $new_index * @param $parent * @return mixed */ public function calculateIndex($new_index, $parent) { if (isset($new_index)) { return $new_index; } else { if ($parent) { if (count($parent->children) > 0) { return $parent->children->last()->index + 1; } else { return 0; } } else { return Item::forCurrentUser()->whereNull('parent_id')->max('index') + 1; } } }
/** * * @test * @return void */ public function it_can_remove_an_urgency_from_an_item() { DB::beginTransaction(); $this->logInUser(); $item = Item::forCurrentUser()->where('urgency', 1)->first(); $response = $this->call('PUT', '/api/items/' . $item->id, ['urgency' => false]); $content = json_decode($response->getContent(), true); //dd($content); $this->checkItemKeysExist($content); $this->assertEquals(null, $content['urgency']); $this->assertEquals(200, $response->getStatusCode()); DB::rollBack(); }
/** * @test * @return void */ public function it_can_calculate_the_next_time_for_a_recurring_item_that_has_a_not_before_time_in_the_past() { DB::beginTransaction(); $this->logInUser(); $item = Item::forCurrentUser()->whereNotNull('recurring_unit')->first(); //Check the recurring values are as expected $this->assertEquals('minute', $item->recurring_unit); $this->assertEquals(1, $item->recurring_frequency); //Make the not before time in the past $response = $this->call('PUT', '/api/items/' . $item->id, ['not_before' => '2016-03-01 13:30:05']); $content = json_decode($response->getContent(), true); //dd($content); $this->assertEquals('2016-03-01 13:30:05', $content['notBefore']); //Check it calculates the next time correctly, for when the instance of the recurring item in the past is completed $response = $this->call('PUT', '/api/items/' . $item->id, ['updatingNextTimeForRecurringItem' => true]); $content = json_decode($response->getContent(), true); //dd($content); $this->checkItemKeysExist($content); $expectedNextTime = Carbon::now(); //Make the expected seconds right for the test if ($expectedNextTime->second < 5) { $expectedNextTime->second = 5; } else { $expectedNextTime->minute++; $expectedNextTime->second = 5; } $this->assertEquals($expectedNextTime, $content['notBefore']); $this->assertEquals(200, $response->getStatusCode()); DB::rollBack(); }
/** * * @return mixed */ public function undoDeleteItem() { $item = Item::forCurrentUser()->onlyTrashed()->orderBy('deleted_at', 'desc')->first(); $item->restore(); $item = $this->transform($this->createItem($item, new ItemTransformer()))['data']; return response($item, Response::HTTP_OK); }
/** * For moving item to new parent. Make room for the new item. * @param $new_parent * @param $new_index */ public function moveIn($new_parent, $new_index) { if ($new_parent) { Item::where('parent_id', $new_parent->id)->where('index', '>=', $new_index)->increment('index'); } else { //Moving home Item::forCurrentUser()->whereNull('parent_id')->where('index', '>=', $new_index)->increment('index'); } }