Skip to content

Commit

Permalink
feat: added file upload in application test case
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Sep 19, 2021
1 parent f5bdb6b commit 89dc1c4
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ php console app:setup

On your terminal:
```
php console db:create
php console migrations:run --seed
```
3\. Start a local server development
Expand Down
Empty file removed TODO
Empty file.
8 changes: 8 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
//register whoops error handler
Whoops::register();

//setup storages
$storage = Storage::path(absolute_path('storage'));

if (!$storage->isDir()) $storage->createDir();
if (!$storage->path(config('storage.logs'))->isDir()) $storage->createDir();
if (!$storage->path(config('storage.cache'))->isDir()) $storage->createDir();
if (!$storage->path(config('storage.sqlite'))->isDir()) $storage->createDir();

//errors display
if (config('errors.display') === true) {
ini_set('display_errors', 1);
Expand Down
21 changes: 5 additions & 16 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ class Application

public function __construct()
{
$this->request = new Request();
$this->response = new Response();

Whoops::register();

$routes = Storage::path(config('storage.routes'))->getFiles();

foreach ($routes as $route) {
require_once config('storage.routes') . $route;
}

//setup storages
if (!Storage::path(config('storage.logs'))->isDir()) Storage::path(config('storage.logs'))->createDir('', true);
if (!Storage::path(config('storage.cache'))->isDir()) Storage::path(config('storage.cache'))->createDir('', true);
if (!Storage::path(config('storage.uploads'))->isDir()) Storage::path(config('storage.uploads'))->createDir('', true);
if (!Storage::path(config('storage.sqlite'))->isDir()) Storage::path(config('storage.sqlite'))->createDir('', true);

$this->request = new Request();
$this->response = new Response();
}

public function run()
Expand All @@ -50,13 +44,8 @@ public function run()
}

catch (Exception $e) {
if (config('errors.log')) {
save_log('Exception: ' . $e);
}

if (config('errors.display')) {
die($e);
}
if (config('errors.log')) save_log('Exception: ' . $e);
if (config('errors.display')) die($e);

$this->response->view(config('errors.views.500'), [], 500);
}
Expand Down
4 changes: 2 additions & 2 deletions core/Console/App/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config['MAILER_HOST'] = fgets(STDIN);
if (strlen($config['MAILER_HOST']) <= 1) $config['MAILER_HOST'] = '127.0.0.1' . PHP_EOL;

$output->write('<info>Mailer host (default: 1025):</info> ');
$output->write('<info>Mailer port (default: 1025):</info> ');
$config['MAILER_PORT'] = fgets(STDIN);
if (strlen($config['MAILER_PORT']) <= 1) $config['MAILER_PORT'] = '1025' . PHP_EOL;

Expand All @@ -100,7 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

Config::saveEnv($config);

if (Storage::path(config('storage.lang'))->isFile(config('app.lang'))) {
if (!Storage::path(config('storage.lang'))->isFile(config('app.lang'))) {
Storage::path(config('storage.lang'))->copyFile('en.php', config('app.lang') . '.php');
}

Expand Down
4 changes: 1 addition & 3 deletions core/Console/Database/Migrations/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ protected function delete(OutputInterface $output, string $table)

protected function isMigrated(string $table): bool
{
if (!Connection::getInstance()->tableExists('migrations')) {
return false;
}
if (!Connection::getInstance()->tableExists('migrations')) return false;

return QueryBuilder::table('migrations')
->select('*')
Expand Down
6 changes: 2 additions & 4 deletions core/Console/Database/Migrations/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (!Connection::getInstance()->tableExists('migrations')) {
Migration::createTable('migrations')
->addprimaryKey('id')
->addPrimaryKey('id')
->addString('name')
->migrate();

Expand Down Expand Up @@ -84,9 +84,7 @@ protected function migrate(OutputInterface $output, string $table)

protected function isMigrated(string $table): bool
{
if (!Connection::getInstance()->tableExists('migrations')) {
return false;
}
if (!Connection::getInstance()->tableExists('migrations')) return false;

return QueryBuilder::table('migrations')
->select('*')
Expand Down
4 changes: 1 addition & 3 deletions core/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ function ($curl, $header) use (&$response_headers, $key) {

$i = null;

do {
curl_multi_exec($curl_multi, $i);
} while ($i);
do { curl_multi_exec($curl_multi, $i); } while ($i);

//retrieves response
foreach ($curl_array as $key => $curl) {
Expand Down
25 changes: 15 additions & 10 deletions core/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function inputs(?string $key = null, $default = null)
return empty($input) || is_null($input) ? $default : $input;
}

public function files(?string $key = null, $default = null)
{
$files = is_null($key) ? $_FILES : ($_FILES[$key] ?? '');
return empty($files) || is_null($files) ? $default : $files;
}

public function rawData()
{
return file_get_contents('php://input');
Expand Down Expand Up @@ -91,10 +97,9 @@ private function getMultipleFiles(string $input, array $allowed_extensions = [])
return $files;
}

public function files(string $input, bool $multiple = false, array $allowed_extensions = [])
public function getFiles(string $input, bool $multiple = false, array $allowed_extensions = [])
{
return $multiple
? $this->getMultipleFiles($input, $allowed_extensions)
return $multiple ? $this->getMultipleFiles($input, $allowed_extensions)
: $this->getSingleFile($input, $allowed_extensions);
}

Expand Down Expand Up @@ -153,7 +158,7 @@ public function has(string ...$items)
return $result;
}

public function hasQuerie(string ...$items)
public function hasQuery(string ...$items)
{
$result = false;

Expand Down Expand Up @@ -213,9 +218,7 @@ public function only(string ...$items)

foreach ($items as $item) {
if ($this->has($item)) {
$result = array_merge($result, [
$item => $this->{$item}
]);
$result = array_merge($result, [$item => $this->{$item}]);
}
}

Expand All @@ -231,9 +234,7 @@ public function except(string ...$items)
foreach ($items as $item) {
foreach ($this->all() as $key => $input) {
if ($item !== $key) {
$result = array_merge($result, [
$key => $input
]);
$result = array_merge($result, [$key => $input]);
}
}
}
Expand All @@ -253,6 +254,10 @@ public function all()
$all = array_merge($all, $this->queries());
}

if (!is_null($this->files())) {
$all = array_merge($all, $this->files());
}

return $all;
}

Expand Down
3 changes: 2 additions & 1 deletion core/Support/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ public function getError()
}
}

public function save(string $destination, ?string $filename = null): bool
public function save(?string $destination = null, ?string $filename = null): bool
{
$destination = $destination ?? config('storage.uploads');
$this->filename = $filename ?? $this->getOriginalFilename();

//create destination directory if not exists
Expand Down
6 changes: 6 additions & 0 deletions core/Testing/ApplicationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public function actingAs($user)
return $this;
}

public function createFileUpload(string $filename, ?string $mime_type = null, ?string $name = null)
{
$this->headers = ['Content-Type' => 'multipart/form-data'];
return curl_file_create($filename, $mime_type, $name);
}

public function get(string $uri, array $headers = [])
{
$this->client = Client::get($this->url($uri), $this->setHeaders($headers));
Expand Down
6 changes: 6 additions & 0 deletions tests/Application/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

/**
* @copyright 2021 - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

use App\Database\Models\User;
use Core\Testing\ApplicationTestCase;
use Core\Testing\Traits\RefreshDatabase;
Expand Down

0 comments on commit 89dc1c4

Please sign in to comment.