Skip to content

Commit b1e51e1

Browse files
committed
add unit tests
1 parent 6299ffd commit b1e51e1

File tree

11 files changed

+99
-12
lines changed

11 files changed

+99
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ tests/temp
55
.idea
66
.phpunit.result.cache
77
.php-cs-fixer.cache
8+
migrations

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/programic/laravel-task.svg?style=flat-square)](https://packagist.org/packages/programic/laravel-task)
44
![](https://github.com/programic/laravel-task/workflows/Run%20Tests/badge.svg?branch=master)
5-
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/programic/laravel-task/run-php-tests?label=Tests)
5+
[![Tests](https://github.com/programic/laravel-task/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/programic/laravel-task/actions/workflows/tests.yml)
66
[![Total Downloads](https://img.shields.io/packagist/dt/programic/laravel-task.svg?style=flat-square)](https://packagist.org/packages/programic/laravel-task)
77

88
This package allows you to automate tasks as in migrations

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@
1212
"role": "Developer"
1313
}
1414
],
15-
"require": {
16-
17-
},
1815
"require-dev": {
19-
16+
"ext-pdo_sqlite": "*",
17+
"orchestra/testbench": "^7.1",
18+
"nunomaduro/collision": "^6.1"
2019
},
2120
"autoload": {
2221
"psr-4": {
2322
"Programic\\Tasks\\": "src/"
2423
}
2524
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"Programic\\Tasks\\Tests\\": "tests"
28+
}
29+
},
2630
"extra": {
2731
"laravel": {
2832
"providers": [

migrations/.gitkeep

Whitespace-only changes.

src/Commands/MakeTaskCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ class MakeTaskCommand extends Command
2828
/**
2929
* Execute the console command.
3030
*
31-
* @return mixed
32-
* @throws Exception
31+
* @return void
3332
*/
3433
public function handle()
3534
{
@@ -40,7 +39,7 @@ public function handle()
4039
$stub = File::get(__DIR__ . '/../../stubs/task.php.stub');
4140
$stub = str_replace('TASK_NAME', $className, $stub);
4241

43-
File::put(base_path() . '/tasks/' . $fileName, $stub);
42+
File::put(Task::getDirectory() . $fileName, $stub);
4443

4544
$this->line('<info>Task created:</info> ' . $fileName);
4645
}

src/Facades/Task.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @method static \Programic\Tasks\Tasks when(bool|\Closure $bool, \Closure $callback)
1010
* @method static \Programic\Tasks\Tasks fresh(\Closure $callback)
1111
* @method static \Programic\Tasks\Tasks noFresh(\Closure $callback)
12-
*
12+
* @method static \Programic\Tasks\Tasks getDirectory(): string
1313
*
1414
* @see \Illuminate\Database\Schema\Builder
1515
*/

src/Tasks.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Programic\Tasks;
44

55
use Illuminate\Contracts\Foundation\Application;
6+
use Programic\Tasks\Traits\Helpers;
67
use Symfony\Component\Console\Input\ArgvInput;
78
use Symfony\Component\Console\Output\ConsoleOutput;
89

910
class Tasks
1011
{
12+
use Helpers;
13+
1114
protected Application $app;
1215

1316
public function __construct(Application $app)

src/Traits/Helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Programic\Tasks\Traits;
4+
5+
use Illuminate\Support\Facades\App;
6+
7+
trait Helpers {
8+
9+
/**
10+
* @return string
11+
*/
12+
public function getDirectory()
13+
{
14+
if (App::runningUnitTests()) {
15+
return __DIR__ . '/../../migrations/';
16+
}
17+
18+
return base_path() . '/tasks/';
19+
}
20+
}

tests/CommandTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Programic\Tasks\Tests;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Support\Str;
7+
use Programic\Tasks\Facades\Task;
8+
9+
use function PHPUnit\Framework\assertFileExists;
10+
11+
class CommandTest extends TestCase
12+
{
13+
/** @test */
14+
public function it_can_create_task_migration()
15+
{
16+
$taskName = 'runTask';
17+
$this->artisan('make:task ' . $taskName)
18+
->assertSuccessful()
19+
->expectsOutputToContain('Task created:');
20+
21+
$taskName = Str::snake($taskName);
22+
$fileName = Carbon::now()->format('Y_m_d_His') . '_' . $taskName . '.php';
23+
24+
assertFileExists(Task::getDirectory() . $fileName);
25+
}
26+
}

0 commit comments

Comments
 (0)