Skip to content

Commit

Permalink
Add unit tests in Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 9, 2024
1 parent 0ff6451 commit d6f345d
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 58 deletions.
20 changes: 12 additions & 8 deletions src/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ public function errors()
*/
public function firstError()
{
if (! $this->errors)
$result = null;

if ($this->errors)
{
return null;
}
/** @var string[][] */
$values = array_values($this->errors);

/** @var string[][] */
$values = array_values($this->errors);
$result = $values[0][0];
}

return (string) $values[0][0];
return $result;
}

/**
Expand Down Expand Up @@ -93,12 +95,14 @@ public function rules($data)
*/
public function setError($key, $text)
{
if (! isset($this->errors[$key]))
$exists = array_key_exists($key, $this->errors);

if (! $exists)
{
$this->errors[$key] = array();
}

array_push($this->errors[$key], $text);
$this->errors[$key][] = $text;

return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Checks/PageCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public function valid(array $data = null)
{
$valid = parent::valid($data);

// @codeCoverageStartIgnore
if (! $data || ! $valid)
{
return false;
}
// @codeCoverageEndIgnore

/** @var string */
$name = $data['name'];
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/FieldHelper.php → src/Helpers/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @author Rougin Gutib <[email protected]>
*/
class FieldHelper
class DataHelper
{
/**
* @param string[] $fields
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/LinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class LinkHelper extends Staticka
/**
* @var array<string, string>
*/
protected $server;
protected $server = array();

/**
* @param string $base
* @param array<string, string> $server
*/
public function __construct($base, $server = array())
{
parent::__construct($base);

$this->server = $server;

parent::__construct($base);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Routes/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Staticka\Depots\PageDepot;
use Staticka\Expresso\Checks\PageCheck;
use Staticka\Expresso\Helpers\FieldHelper;
use Staticka\Expresso\Helpers\DataHelper;
use Staticka\Expresso\Plate;

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ public function show($id, PageDepot $page, Plate $plate)

$item['page'] = $data;

$item['data'] = FieldHelper::toJson($fields, $data);
$item['data'] = DataHelper::toJson($fields, $data);

/** @var \Psr\Http\Message\ResponseInterface */
return $plate->view('editor', $item);
Expand Down
45 changes: 1 addition & 44 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,7 @@ class AppTest extends Testcase
*/
public function doSetUp()
{
$path = __DIR__ . '/Fixture';

$app = new Express;

$app->setAppUrl('http://localhost:3977');
$app->setSiteUrl('http://localhost:3978');
$app->setRootPath($path);

$app->setBuildPath($path . '/build');
$app->setConfigPath($path . '/config');
$app->setPagesPath($path . '/pages');
$app->setBuildPath($path . '/build');
$app->setTimezone('Asia/Manila');

$plates = __DIR__ . '/../app/plates';
$app->setPlatesPath($plates);

$this->app = $app->setFields(array());
$this->app = $this->setApp();
}

/**
Expand All @@ -53,18 +36,6 @@ public function test_fixed_app_path()
$this->assertEquals($expected, $actual);
}

/**
* @return void
*/
public function test_pages_page()
{
$this->setRequest('GET', '/pages');

$this->app->run();

$this->expectOutputRegex('/Create New Page/');
}

/**
* @return void
*/
Expand All @@ -76,18 +47,4 @@ public function test_welcome_page()

$this->expectOutputRegex('/Welcome to Expresso!/');
}

/**
* @param string $method
* @param string $uri
*
* @return void
*/
protected function setRequest($method, $uri)
{
$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['REQUEST_URI'] = $uri;
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_PORT'] = '8000';
}
}
43 changes: 43 additions & 0 deletions tests/Routes/BuildTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Staticka\Expresso\Routes;

use Staticka\Expresso\Testcase;

/**
* @package Staticka
*
* @author Rougin Gutib <[email protected]>
*/
class BuildTest extends Testcase
{
/**
* @var \Staticka\Expresso\Express
*/
protected $app;

/**
* @return void
*/
public function doSetUp()
{
$this->app = $this->setApp();
}

/**
* @return void
*/
public function test_build_without_staticka_yml()
{
$expected = '"staticka.yml" not yet created';

/** @var string */
$expected = json_encode($expected);

$this->setRequest('POST', '/build');

$this->app->run();

$this->expectOutputString($expected);
}
}
Loading

0 comments on commit d6f345d

Please sign in to comment.