Skip to content

Commit

Permalink
Merge branch 'singleton-routing' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 30, 2022
2 parents 8bcd484 + ed566a6 commit 904cbd6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Expand Up @@ -52,6 +52,6 @@ class {{ class }} extends Controller
*/
public function destroy({{ parentModel }} ${{ parentModelVariable }})
{
//
abort(404);
}
}
Expand Up @@ -74,6 +74,6 @@ class {{ class }} extends Controller
*/
public function destroy({{ parentModel }} ${{ parentModelVariable }})
{
//
abort(404);
}
}
Expand Up @@ -46,6 +46,6 @@ class {{ class }} extends Controller
*/
public function destroy()
{
//
abort(404);
}
}
Expand Up @@ -66,6 +66,6 @@ class {{ class }} extends Controller
*/
public function destroy()
{
//
abort(404);
}
}
6 changes: 3 additions & 3 deletions src/Illuminate/Routing/ResourceRegistrar.php
Expand Up @@ -26,7 +26,7 @@ class ResourceRegistrar
*
* @var string[]
*/
protected $singletonResourceDefaults = ['show', 'edit', 'update', 'destroy'];
protected $singletonResourceDefaults = ['show', 'edit', 'update'];

/**
* The parameters set for this resource instance.
Expand Down Expand Up @@ -251,8 +251,8 @@ protected function getResourceMethods($defaults, $options)

if (isset($options['creatable'])) {
$methods = isset($options['apiSingleton'])
? array_merge(['store'], $methods)
: array_merge(['create', 'store'], $methods);
? array_merge(['store', 'destroy'], $methods)
: array_merge(['create', 'store', 'destroy'], $methods);

return $this->getResourceMethods(
$methods, array_values(Arr::except($options, ['creatable']))
Expand Down
43 changes: 36 additions & 7 deletions tests/Integration/Routing/RouteSingletonTest.php
Expand Up @@ -36,10 +36,10 @@ public function testSingletonDefaults()
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton update', $response->getContent());

$this->assertSame('http://localhost/avatar', route('avatar.destroy'));
$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton destroy', $response->getContent());
// $this->assertSame('http://localhost/avatar', route('avatar.destroy'));
// $response = $this->delete('/avatar');
// $this->assertEquals(404, $response->getStatusCode());
// $this->assertSame('singleton destroy', $response->getContent());
}

public function testCreatableSingleton()
Expand All @@ -55,6 +55,11 @@ public function testCreatableSingleton()
$response = $this->post('/avatar');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton store', $response->getContent());

$this->assertSame('http://localhost/avatar', route('avatar.destroy'));
$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton destroy', $response->getContent());
}

public function testApiSingleton()
Expand Down Expand Up @@ -130,9 +135,9 @@ public function testSingletonExcept()
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton update', $response->getContent());

$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton destroy', $response->getContent());
// $response = $this->delete('/avatar');
// $this->assertEquals(200, $response->getStatusCode());
// $this->assertSame('singleton destroy', $response->getContent());
}

public function testSingletonName()
Expand Down Expand Up @@ -169,6 +174,30 @@ public function testNestedSingleton()
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton update for 123', $response->getContent());

$response = $this->delete('/videos/123/thumbnail');
$this->assertEquals(405, $response->getStatusCode());
}

public function testCreatableNestedSingleton()
{
Route::singleton('videos.thumbnail', NestedSingletonTestController::class)->creatable();

$response = $this->get('/videos/123/thumbnail');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton show for 123', $response->getContent());

$response = $this->get('/videos/123/thumbnail/edit');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton edit for 123', $response->getContent());

$response = $this->put('/videos/123/thumbnail');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton update for 123', $response->getContent());

$response = $this->patch('/videos/123/thumbnail');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton update for 123', $response->getContent());

$response = $this->delete('/videos/123/thumbnail');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('singleton destroy for 123', $response->getContent());
Expand Down

0 comments on commit 904cbd6

Please sign in to comment.