Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Facades/ApiFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
namespace Sysvale\ApiFiles\Facades;

use Illuminate\Support\Facades\Facade;
use Tests\Fakes\ApiFilesFake;

class ApiFiles extends Facade
{
protected static function getFacadeAccessor()
{
return 'apifiles';
}

public static function fake()
{
static::swap(new ApiFilesFake);
}
}
72 changes: 72 additions & 0 deletions tests/Fakes/ApiFIlesFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Tests\Fakes;

use Closure;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\ExpectationFailedException;

class ApiFilesFake
{
protected static $content;
protected static $sent_files = [];

public static function setContents($content)
{
self::$content = $content;
}

public static function get($api_file_id)
{
return new class(self::$content) {
protected $content;

public function __construct($content)
{
$this->content = $content;
}
public function getStatusCode()
{
return 200;
}
public function getBody()
{
return $this;
}
public function getContents()
{
return $this->content;
}
};
}

public static function send($path)
{
self::$sent_files[] = [
'path' => $path,
'content' => file_get_contents($path)
];

return (object) [
'statusCode' => 200,
'id' => array_key_last(self::$sent_files) . '-api_file.fake'
];
}

public static function assertSent($filename, $callback = null)
{
$files = array_column(self::$sent_files, 'path');

PHPUnit::assertContains($filename, $files, "The file $filename was not sent to ApiFiles");

if ($callback instanceof Closure) {
foreach (self::$sent_files as $file) {
if ($callback($file)) {
return true;
}
}
}

throw new ExpectationFailedException("The file $filename was not sent to ApiFiles");
}
}