Skip to content

Commit

Permalink
fix(Testing): Fix missing UrlGenerator and add tests
Browse files Browse the repository at this point in the history
Caused by commit d432f7b
  • Loading branch information
pionl committed Jun 22, 2023
1 parent 11f31f4 commit 21c5a53
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Testing/Laravel/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\Laravel\Routing;

use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;

class UrlGenerator implements UrlGeneratorContract
{
private string $controllerNamespace = 'App\\Controllers';

public function current(): string
{
return 'http://localhost/current';
}

public function previous($fallback = false): string
{
return 'http://localhost/previous' . ($fallback !== '' ? '-fallback' : '');
}

public function to($path, $extra = [], $secure = null): string
{
return $this->createUrl(path: $path, parameters: $extra, secure: $secure);
}

public function secure($path, $parameters = []): string
{
return $this->to(path: $path, extra: $parameters);
}

public function asset($path, $secure = null): string
{
return $this->to(path: 'assets/' . $path, secure: $secure);
}

public function route($name, $parameters = [], $absolute = true): string
{
return $this->createUrl(path: 'route/' . $name, parameters: $parameters, absolute: $absolute);
}

public function action($action, $parameters = [], $absolute = true): string
{
$actionString = is_array($action) ? implode('-', $action) : $action;

return $this->createUrl(path: 'action/' . $actionString, parameters: $parameters, absolute: $absolute);
}

public function getRootControllerNamespace(): string
{
return $this->controllerNamespace;
}

public function setRootControllerNamespace($rootNamespace): self
{
$this->controllerNamespace = $rootNamespace;

return $this;
}

protected function createUrl(
string $path,
array $parameters = [],
mixed $secure = null,
bool $absolute = true
): string {
$paramsString = $parameters === []
? ''
: ('?' . http_build_query($parameters));
$fullPath = '/' . $path . $paramsString;

if ($absolute === false) {
return $fullPath;
}

if ($secure === true) {
return 'https://localhost' . $fullPath;
}

return 'http://localhost/' . $path . $paramsString;
}
}
192 changes: 192 additions & 0 deletions tests/Feature/Testing/Laravel/Routing/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Feature\Testing\Laravel\Routing;

use LaraStrict\Testing\Laravel\Routing\UrlGenerator;
use PHPUnit\Framework\TestCase;

class UrlGeneratorTest extends TestCase
{
private UrlGenerator $urlGenerator;

protected function setUp(): void
{
parent::setUp();
$this->urlGenerator = new UrlGenerator();
}

public function testCurrent(): void
{
$this->assertEquals(expected: 'http://localhost/current', actual: $this->urlGenerator->current());
}

public function testPrevious(): void
{
$this->assertEquals(
expected: 'http://localhost/previous-fallback',
actual: $this->urlGenerator->previous()
);
}

public function testPreviousFallbackEmptyString(): void
{
$this->assertEquals(expected: 'http://localhost/previous', actual: $this->urlGenerator->previous(''));
}

public function testPreviousFallbackCustom(): void
{
$this->assertEquals(
expected: 'http://localhost/previous-fallback',
actual: $this->urlGenerator->previous('s')
);
}

// test to function
public function testTo(): void
{
$this->assertEquals(expected: 'http://localhost/path', actual: $this->urlGenerator->to('path'));
}

// test to function with extra parameters
public function testToWithExtraParameters(): void
{
$this->assertEquals(
expected: 'http://localhost/path?foo=bar',
actual: $this->urlGenerator->to('path', [
'foo' => 'bar',
])
);
}

// test to function with extra parameters and secure=true
public function testToWithExtraParametersAndSecure(): void
{
$this->assertEquals(
expected: 'https://localhost/path?foo=bar',
actual: $this->urlGenerator->to('path', [
'foo' => 'bar',
], true)
);
}

// test secure function
public function testSecure(): void
{
$this->assertEquals(expected: 'http://localhost/path', actual: $this->urlGenerator->secure('path'));
}

// test secure function with extra parameters
public function testSecureWithExtraParameters(): void
{
$this->assertEquals(
expected: 'http://localhost/path?foo=bar',
actual: $this->urlGenerator->secure('path', [
'foo' => 'bar',
])
);
}

// test asset function
public function testAsset(): void
{
$this->assertEquals(expected: 'http://localhost/assets/path', actual: $this->urlGenerator->asset('path'));
}

// test asset function with secure=true
public function testAssetWithSecure(): void
{
$this->assertEquals(
expected: 'https://localhost/assets/path',
actual: $this->urlGenerator->asset('path', true)
);
}

// test route function
public function testRoute(): void
{
$this->assertEquals(expected: 'http://localhost/route/name', actual: $this->urlGenerator->route('name'));
}

// test route function with extra parameters
public function testRouteWithExtraParameters(): void
{
$this->assertEquals(
expected: 'http://localhost/route/name?foo=bar',
actual: $this->urlGenerator->route('name', [
'foo' => 'bar',
])
);
}

// test route function with extra parameters and absolute=false
public function testRouteWithExtraParametersAndAbsoluteFalse(): void
{
$this->assertEquals(
expected: '/route/name?foo=bar',
actual: $this->urlGenerator->route('name', [
'foo' => 'bar',
], false)
);
}

// test route function with absolute=false
public function testRouteWithAbsoluteFalse(): void
{
$this->assertEquals(expected: '/route/name', actual: $this->urlGenerator->route('name', [], false));
}

// test action function
public function testAction(): void
{
$this->assertEquals(
expected: 'http://localhost/action/action',
actual: $this->urlGenerator->action('action')
);
}

// test action function with extra parameters
public function testActionWithExtraParameters(): void
{
$this->assertEquals(
expected: 'http://localhost/action/action?foo=bar',
actual: $this->urlGenerator->action('action', [
'foo' => 'bar',
])
);
}

// test action function with extra parameters and absolute=false
public function testActionWithExtraParametersAndAbsoluteFalse(): void
{
$this->assertEquals(
expected: '/action/action?foo=bar',
actual: $this->urlGenerator->action('action', [
'foo' => 'bar',
], false)
);
}

// test action function with absolute=false
public function testActionWithAbsoluteFalse(): void
{
$this->assertEquals(expected: '/action/action', actual: $this->urlGenerator->action('action', [], false));
}

// test getRootControllerNamespace function
public function testGetRootControllerNamespace(): void
{
$this->assertEquals(expected: 'App\Controllers', actual: $this->urlGenerator->getRootControllerNamespace());
}

// test setRootControllerNamespace function
public function testSetRootControllerNamespace(): void
{
$this->urlGenerator->setRootControllerNamespace('App\Http\Controllers\New');
$this->assertEquals(
expected: 'App\Http\Controllers\New',
actual: $this->urlGenerator->getRootControllerNamespace()
);
}
}

0 comments on commit 21c5a53

Please sign in to comment.