Skip to content

Commit

Permalink
Laravel 6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
hedii committed Sep 17, 2019
1 parent b12fd96 commit 3db965a
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea
/vendor
composer.lock
.phpunit.result.cache
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: php

php:
- 7.0
- 7.2
- 7.3

before_script:
- composer self-update
Expand All @@ -11,4 +12,4 @@ script:
- composer test

matrix:
fast_finish: true
fast_finish: true
4 changes: 2 additions & 2 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) 2016 Hedi Chaibi <hedi.chaibs@gmail.com>
Copyright (c) 2019 Hedi Chaibi <contact@hedichaibi.com>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ Copyright (c) 2016 Hedi Chaibi <hedi.chaibs@gmail.com>
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
> THE SOFTWARE.
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"authors": [
{
"name": "hedii",
"email": "hedi.chaibs@gmail.com",
"email": "contact@hedichaibi.com",
"homepage": "https://hedichaibi.com"
}
],
Expand All @@ -24,15 +24,15 @@
"email": "hedi.chaibs@gmail.com"
},
"require": {
"php": ">=7.0",
"illuminate/support": "^5.3",
"illuminate/filesystem": "^5.3",
"illuminate/console": "^5.3"
"php": "^7.2",
"illuminate/console": "^6.0",
"illuminate/filesystem": "^6.0",
"illuminate/support": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"orchestra/testbench": "^3.3",
"mockery/mockery": "^0.9.5"
"mockery/mockery": "^1.0",
"orchestra/testbench": "^4.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -47,10 +47,10 @@
"scripts": {
"test": "vendor/bin/phpunit"
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
},
"laravel": {
"providers": [
"Hedii\\ArtisanLogCleaner\\ArtisanLogCleanerServiceProvider"
Expand Down
4 changes: 2 additions & 2 deletions src/ArtisanLogCleanerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class ArtisanLogCleanerServiceProvider extends ServiceProvider
{
public function register()
public function register(): void
{
$this->commands([ClearLogs::class]);
}
}
}
8 changes: 3 additions & 5 deletions src/ClearLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ public function __construct(Filesystem $disk)

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
public function handle(): void
{
$files = $this->getLogFiles();

Expand All @@ -70,7 +68,7 @@ public function handle()
*
* @return \Illuminate\Support\Collection
*/
private function getLogFiles()
private function getLogFiles(): Collection
{
return collect(
$this->disk->allFiles(storage_path('logs'))
Expand All @@ -83,7 +81,7 @@ private function getLogFiles()
* @param \Illuminate\Support\Collection $files
* @return int
*/
private function delete(Collection $files)
private function delete(Collection $files): int
{
return $files->each(function ($file) {
$this->disk->delete($file);
Expand Down
121 changes: 68 additions & 53 deletions tests/ClearLogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,116 @@

class ClearLogsTest extends TestCase
{
public function test_it_should_delete_all_files_in_log_directory()
/** @test */
public function it_should_delete_all_files_in_log_directory(): void
{
$this->createLogFile(['file1.log', 'file2.log']);

if (! $this->artisan('log:clear')) {
$this->assertFileNotExists($this->logDirectory . '/file1.log');
$this->assertFileNotExists($this->logDirectory . '/file2.log');
} else {
$this->assertTrue(false, 'this test failed...');
}
$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileExists($this->logDirectory . '/file2.log');

$this->artisan('log:clear');

$this->assertFileNotExists($this->logDirectory . '/file1.log');
$this->assertFileNotExists($this->logDirectory . '/file2.log');
}

public function test_it_should_not_delete_dot_files_in_log_directory()
/** @test */
public function it_should_not_delete_dot_files_in_log_directory(): void
{
$this->createLogFile(['file1.log', 'file2.log']);

$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileExists($this->logDirectory . '/file2.log');

$this->artisan('log:clear');

if (! $this->artisan('log:clear')) {
$this->assertFileExists($this->logDirectory . '/.gitignore');
$this->assertFileNotExists($this->logDirectory . '/file1.log');
$this->assertFileNotExists($this->logDirectory . '/file2.log');
} else {
$this->assertTrue(false, 'this test failed...');
}
$this->assertFileExists($this->logDirectory . '/.gitignore');
$this->assertFileNotExists($this->logDirectory . '/file1.log');
$this->assertFileNotExists($this->logDirectory . '/file2.log');
}

public function test_it_should_keep_the_last_log_file_if_the_option_is_provided()
/** @test */
public function it_should_keep_the_last_log_file_if_the_option_is_provided(): void
{
touch($this->logDirectory . '/file1.log', time() - 3600);
touch($this->logDirectory . '/file2.log', time() - 4600);
touch($this->logDirectory . '/file3.log', time() - 5600);

if (! $this->artisan('log:clear', ['--keep-last' => true])) {
$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileNotExists($this->logDirectory . '/file2.log');
$this->assertFileNotExists($this->logDirectory . '/file3.log');
} else {
$this->assertTrue(false, 'this test failed...');
}
$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileExists($this->logDirectory . '/file2.log');
$this->assertFileExists($this->logDirectory . '/file3.log');

$this->artisan('log:clear', ['--keep-last' => true]);

$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileNotExists($this->logDirectory . '/file2.log');
$this->assertFileNotExists($this->logDirectory . '/file3.log');
}

public function test_it_should_keep_the_last_log_file_if_the_option_is_with_only_one_file()
/** @test */
public function it_should_keep_the_last_log_file_if_the_option_is_with_only_one_file(): void
{
touch($this->logDirectory . '/file1.log', time() - 3600);

if (! $this->artisan('log:clear', ['--keep-last' => true])) {
$this->assertFileExists($this->logDirectory . '/file1.log');
} else {
$this->assertTrue(false, 'this test failed...');
}
$this->assertFileExists($this->logDirectory . '/file1.log');

$this->artisan('log:clear', ['--keep-last' => true]);

$this->assertFileExists($this->logDirectory . '/file1.log');
}

public function test_it_should_return_zero_even_if_there_is_no_log_file()
/** @test */
public function it_should_return_zero_even_if_there_is_no_log_file(): void
{
$this->assertEquals(0, $this->artisan('log:clear'));
$this
->artisan('log:clear')
->assertExitCode(0);
}

public function test_it_should_return_zero_with_the_keep_last_option_even_if_there_is_no_log_file()
/** @test */
public function it_should_return_zero_with_the_keep_last_option_even_if_there_is_no_log_file(): void
{
$this->artisan('log:clear', ['--keep-last' => true]);
$this
->artisan('log:clear', ['--keep-last' => true])
->assertExitCode(0);
}

public function test_it_should_display_the_correct_message_when_no_log_file_has_been_deleted()
/** @test */
public function it_should_display_the_correct_message_when_no_log_file_has_been_deleted(): void
{
$command = $this->getMockedCommand();

$this->expectInfoMessage('There was no log file to delete in the log folder', $command);

$this->registerCommand($command);

$this->artisan('log:clear');
$this
->artisan('log:clear')
->expectsOutput('There was no log file to delete in the log folder')
->assertExitCode(0);
}

public function test_it_should_display_the_correct_message_when_one_log_file_has_been_deleted()
/** @test */
public function it_should_display_the_correct_message_when_one_log_file_has_been_deleted(): void
{
$this->createLogFile(['file1.log', 'file2.log']);
$command = $this->getMockedCommand();

$this->expectInfoMessage('1 log file has been deleted', $command);
$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileExists($this->logDirectory . '/file2.log');

$this->registerCommand($command);

$this->artisan('log:clear', ['--keep-last' => true]);
$this
->artisan('log:clear', ['--keep-last' => true])
->expectsOutput('1 log file has been deleted')
->assertExitCode(0);
}

public function test_it_should_display_the_correct_message_when_more_than_one_log_file_has_been_deleted()
/** @test */
public function it_should_display_the_correct_message_when_more_than_one_log_file_has_been_deleted(): void
{
$this->createLogFile(['file1.log', 'file2.log', 'file3.log']);
$command = $this->getMockedCommand();

$this->expectInfoMessage('2 log files have been deleted', $command);
$this->assertFileExists($this->logDirectory . '/file1.log');
$this->assertFileExists($this->logDirectory . '/file2.log');
$this->assertFileExists($this->logDirectory . '/file3.log');

$this->registerCommand($command);

$this->artisan('log:clear', ['--keep-last' => true]);
$this
->artisan('log:clear', ['--keep-last' => true])
->expectsOutput('2 log files have been deleted')
->assertExitCode(0);
}
}
51 changes: 6 additions & 45 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
namespace Hedii\ArtisanLogCleaner\Tests;

use Hedii\ArtisanLogCleaner\ArtisanLogCleanerServiceProvider;
use Hedii\ArtisanLogCleaner\ClearLogs;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Filesystem\Filesystem;
use Mockery;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
Expand All @@ -21,7 +17,7 @@ class TestCase extends Orchestra
/**
* Executed before each test.
*/
public function setUp()
public function setUp(): void
{
parent::setUp();

Expand All @@ -33,7 +29,7 @@ public function setUp()
/**
* Executed after each test.
*/
public function tearDown()
public function tearDown(): void
{
parent::tearDown();

Expand All @@ -46,52 +42,17 @@ public function tearDown()
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [ArtisanLogCleanerServiceProvider::class];
}

/**
* The mock of the command.
*
* @return \Mockery\MockInterface
*/
protected function getMockedCommand()
{
return Mockery::mock(ClearLogs::class . '[info]', [
new Filesystem()
]);
}

/**
* The command info expectation.
*
* @param string $message
* @param \Mockery\MockInterface $command
*/
protected function expectInfoMessage($message, $command)
{
$command->shouldReceive('info')
->once()
->with($message);
}

/**
* Register the mocked command.
*
* @param \Mockery\MockInterface $command
*/
protected function registerCommand($command)
{
$this->app[Kernel::class]->registerCommand($command);
}

/**
* Create fake log files in the test temporary directory.
*
* @param array|string $files
*/
protected function createLogFile($files)
protected function createLogFile($files): void
{
foreach ((array) $files as $file) {
touch($this->logDirectory . '/' . $file);
Expand All @@ -101,12 +62,12 @@ protected function createLogFile($files)
/**
* Delete all fake log files int the test temporary directory.
*/
private function deleteLogFiles()
private function deleteLogFiles(): void
{
foreach (glob($this->logDirectory . '/*') as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
}
}

0 comments on commit 3db965a

Please sign in to comment.