Skip to content

Commit

Permalink
Reduce boilerplate in the test class by setting up mocks in setup()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Stenvall committed Mar 14, 2018
1 parent ca9e93a commit 7c518d7
Showing 1 changed file with 79 additions and 108 deletions.
187 changes: 79 additions & 108 deletions tests/Cache/ClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,168 +4,139 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Cache\CacheManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
use Illuminate\Cache\Console\ClearCommand;
use Illuminate\Contracts\Cache\Repository;

class ClearCommandTest extends TestCase
{

/**
* @var ClearCommandTestStub
*/
private $command;

/**
* @var CacheManager|m\Mock
*/
private $cacheManager;

/**
* @var Filesystem|m\Mock
*/
private $files;

/**
* @var Repository|m\Mock
*/
private $cacheRepository;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$this->cacheManager = m::mock('Illuminate\Cache\CacheManager');
$this->files = m::mock('Illuminate\Filesystem\Filesystem');
$this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->command = new ClearCommandTestStub($this->cacheManager, $this->files);

$app = new Application;
$app['path.storage'] = __DIR__;
$this->command->setLaravel($app);
}

public function tearDown()
{
m::close();
}

public function testClearWithNoStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('exists')->andReturn(true);
$files->shouldReceive('files')->andReturn([]);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command);
$this->runCommand($this->command);
}

public function testClearWithStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('exists')->andReturn(true);
$files->shouldReceive('files')->andReturn([]);
$this->cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command, ['store' => 'foo']);
$this->runCommand($this->command, ['store' => 'foo']);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testClearWithInvalidStoreArgument()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);
$this->files->shouldReceive('files')->andReturn([]);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('InvalidArgumentException');
$this->cacheRepository->shouldReceive('flush')->never();

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('InvalidArgumentException');
$cacheRepository->shouldReceive('flush')->never();

$this->runCommand($command, ['store' => 'bar']);
$this->runCommand($this->command, ['store' => 'bar']);
}

public function testClearWithTagsOption()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('exists')->andReturn(true);
$files->shouldReceive('files')->andReturn([]);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command, ['--tags' => 'foo,bar']);
$this->runCommand($this->command, ['--tags' => 'foo,bar']);
}

public function testClearWithStoreArgumentAndTagsOption()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn([]);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$files->shouldReceive('exists')->andReturn(true);
$files->shouldReceive('files')->andReturn([]);

$cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($cacheRepository);
$cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();

$this->runCommand($command, ['store' => 'redis', '--tags' => 'foo']);
$this->runCommand($this->command, ['store' => 'redis', '--tags' => 'foo']);
}

public function testClearWillClearRealTimeFacades()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

$files->shouldReceive('exists')->andReturn(true);
$files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
$files->shouldReceive('delete')->with('/facade-XXXX.php')->once();
$this->files->shouldReceive('exists')->andReturn(true);
$this->files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
$this->files->shouldReceive('delete')->with('/facade-XXXX.php')->once();

$this->runCommand($command);
$this->runCommand($this->command);
}

public function testClearWillNotClearRealTimeFacadesIfCacheDirectoryDoesntExist()
{
$command = new ClearCommandTestStub(
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
$files = m::mock('Illuminate\Filesystem\Filesystem')
);

$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');

$app = new Application;
$app['path.storage'] = __DIR__;
$command->setLaravel($app);
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
$cacheRepository->shouldReceive('flush')->once();
$this->cacheManager->shouldReceive('store')->once()->with(null)->andReturn($this->cacheRepository);
$this->cacheRepository->shouldReceive('flush')->once();

// No files should be looped over and nothing should be deleted if the cache directory doesn't exist
$files->shouldReceive('exists')->andReturn(false);
$files->shouldNotReceive('files');
$files->shouldNotReceive('delete');
$this->files->shouldReceive('exists')->andReturn(false);
$this->files->shouldNotReceive('files');
$this->files->shouldNotReceive('delete');

$this->runCommand($command);
$this->runCommand($this->command);
}

protected function runCommand($command, $input = [])
Expand Down

0 comments on commit 7c518d7

Please sign in to comment.