Skip to content

Commit

Permalink
Merge pull request #27 from librarianphp/request-tests
Browse files Browse the repository at this point in the history
Adding tests for Request class
  • Loading branch information
erikaheidi authored Oct 17, 2024
2 parents e11224d + b02e525 commit 750b771
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"require-dev": {
"pestphp/pest": "^3.2",
"laravel/pint": "^1.1"
"laravel/pint": "^1.1",
"mockery/mockery": "^1.6"
},
"scripts": {
"lint": ["pint"],
Expand Down
136 changes: 135 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Parsed\ContentParser;

/**
* A ContentType is a node with either children nodes or content files
*/
class ContentType
{
public string $slug;
Expand Down
4 changes: 4 additions & 0 deletions src/Provider/ContentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function fetchAll(int $start = 0, int $limit = 20, bool $parse_markdown =
} catch (Exception $e) {
}
}

if (count($contentType->children)) {

}
}

$list = $this->orderBy($list, $orderBy);
Expand Down
30 changes: 25 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,38 @@

class Request
{
/**
* Parameters from Request
*/
protected array $params;

/**
* The full request string
*/
protected string $request_uri;

/**
* Request information
*/
protected ?array $request_info;

/**
* Full request path
*/
protected string $path;

/**
* @var string Requested route, such as "home", "index", "blog", etc
* only 1 level is supported
* The root of the path
*/
protected string $route;

/**
* @var string Slug if present (request path minus route)
* Parent route, used for content types
*/
protected string $parent;

/**
* Final portion of the request string, when present
*/
protected string $slug;

Expand All @@ -33,10 +49,9 @@ public function __construct(array $params, string $request_uri)
$this->request_info = parse_url($this->request_uri);
$this->path = $this->request_info['path'];

//make sure to get the first part only
$parts = explode('/', $this->path);

$this->route = $parts[1];
$this->parent = dirname($this->path);
$this->slug = str_replace('/' . $this->route . '/', '', $this->path);
}

Expand Down Expand Up @@ -69,4 +84,9 @@ public function getSlug(): string
{
return $this->slug;
}

public function getParent(): string
{
return $this->parent;
}
}
2 changes: 1 addition & 1 deletion tests/Feature/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
it('loads the full list of content when no limit is passed', function () {
$content = $this->app->content->fetchAll(0, 0);
expect($content)->toBeInstanceOf(ContentCollection::class)
->and($content->total())->toBeGreaterThan(2);
->and($content->total())->toBe(5);
});

it('loads tag list', function () {
Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

use Librarian\ContentType;
use Librarian\Provider\ContentServiceProvider;
use Librarian\Provider\LibrarianServiceProvider;
use Librarian\Provider\RouterServiceProvider;
use Librarian\Provider\TwigServiceProvider;
use Librarian\Request;
use Minicli\App;

beforeEach(function () {
$this->config = [
'debug' => true,
'templates_path' => __DIR__ . '/../resources',
'data_path' => __DIR__ . '/../resources',
'cache_path' => __DIR__ . '/../resources',
];

$request = new Request(['param1' => 'value1', 'param2' => 'value2'], '/docs/en/test0');
$router = Mockery::mock(RouterServiceProvider::class);
$router->shouldReceive('load');
$router->shouldReceive('getRequest')->andReturn($request);

$app = new App($this->config);
$app->addService('content', new ContentServiceProvider());
$app->addService('twig', new TwigServiceProvider());
$app->addService('librarian', new LibrarianServiceProvider());
$app->addService('router', $router);
$this->app = $app;
});

it('passes request through router', function () {
$request = $this->app->router->getRequest();
expect($request)->toBeInstanceOf(Request::class)
->and($request->getParams())->toBe(['param1' => 'value1', 'param2' => 'value2']);
});

it('loads content in nested structure', function () {
$request = $this->app->router->getRequest();
expect($request->getRoute())->toBe('docs')
->and($request->getParent())->toBe('/docs/en')
->and($request->getSlug())->toBe('en/test0');

$contentType = $this->app->content->getContentType($request->getParent());
expect($contentType)->toBeInstanceOf(ContentType::class)
->and($contentType->title)->toBe('English Docs');
});

0 comments on commit 750b771

Please sign in to comment.