Skip to content

Instantly share code, notes, and snippets.

@Patabugen
Last active July 26, 2022 16:06
Show Gist options
  • Save Patabugen/6ac4895aaf96e3bc8b354ee53a45c4bf to your computer and use it in GitHub Desktop.
Save Patabugen/6ac4895aaf96e3bc8b354ee53a45c4bf to your computer and use it in GitHub Desktop.
Default shouldRun expectations in Laravel Actions
<?php
/**
* An example of overriding the shouldRun method to add the default expectations to the Mock,
* instead of redefining it in all your tests.
*
* You can still call MakeEntryLine::shouldRun()->andReturn(...)
* to override the default expectations.
*/
namespace App\Actions\Csv;
use App\Models\Entry;
use App\Csv\EntryHeaders;
use App\Csv\EntryLine;
use Lorisleiva\Actions\Concerns\AsAction;
class MakeEntryLine
{
use AsAction;
public function handle(Entry $entry)
{
return new EntryLine(new EntryHeaders, $entry);
}
public static function shouldRun()
{
return static::mock()->shouldReceive('handle')
->andReturn(new EntryLine(new EntryHeaders, new Entry));
}
}
<?php
/**
* Another example of overriding shouldRun(), however since this one calls getMock() it
* would be harder to tweak with `SaveCsvFile::shouldRun()->...` since it does not return
* the original Expectation.
*
* `SaveCsvFile::mock()->shouldReceive('handle')...` will always be available.
*
**/
namespace App\Actions\Csv;
use App\Helpers\CsvWriter;
use App\Csv\CsvHeaders;
use App\Csv\CsvLine;
use App\Traits\HasProgressBar;
use Lorisleiva\Actions\Concerns\AsAction;
class SaveCsvFile
{
use AsAction;
use HasProgressBar;
protected CsvHeaders $headers;
protected string $path;
protected $fileHandle;
protected CsvWriter $csv;
public function handle(CsvHeaders $headers, iterable $lines, string $file)
{
$saveCsvFile = self::make()->headers($headers)->path($file);
$saveCsvFile->start(count($lines));
$pb = $this->progressBar()->create('📁 Writing to file', count($lines));
$pb->start();
foreach ($lines as $line) {
$this->insertOne($line);
$pb->advance();
}
$this->finish();
$pb->finish();
}
public function start(): self
{
throw_if(!isset($this->path), 'RuntimeException', 'Path not set - please call ->path(<string>)');
throw_if(!isset($this->headers), 'RuntimeException', 'Headers not set - please call ->headers(<CsvHeaders>)');
$this->insertOne($this->headers);
return $this;
}
public function finish(): self
{
fclose($this->fileHandle);
return $this;
}
public function insertOne(CsvLine|CsvHeaders $line): self
{
$this->writer()->insertOne($line->toArray());
return $this;
}
public function writer(): CsvWriter
{
if (!isset($this->csv)) {
$this->fileHandle = fopen($this->path, 'w+');
$this->csv = CsvWriter::createFromStream($this->fileHandle);
}
return $this->csv;
}
public function path(string $path): self
{
$this->path = $path;
return $this;
}
public function headers(CsvHeaders $headers): self
{
$this->headers = $headers;
return $this;
}
public static function shouldRun()
{
return static::mock()->shouldReceive('handle')
->getMock()->shouldReceive('headers', 'path', 'start', 'finish')
->withAnyArgs()->andReturnSelf();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment