Skip to content

Commit

Permalink
Created a class for opening a file resource
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Oct 30, 2018
1 parent 58f8b3e commit cf82fa9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
12 changes: 5 additions & 7 deletions app/Jobs/TranscribeAudioFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace App\Jobs;

use App\Configurations\IbmWatsonConfiguration;
use App\Services\FileOpenerService;
use App\Traits\InteractsWithLocalFileSystem;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Dispatcher;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -45,17 +44,16 @@ public function __construct(string $oldFilename, string $filename)
/**
* @param IbmWatsonConfiguration $ibmWatsonConfiguration
* @param Client $httpClient
* @param FilesystemManager $filesystemManager
* @param Dispatcher $commandDispatcher
* @param FileOpenerService $fileOpenerService
*
* @throws FileNotFoundException
* @throws GuzzleException
*/
public function handle(
IbmWatsonConfiguration $ibmWatsonConfiguration,
Client $httpClient,
FilesystemManager $filesystemManager,
Dispatcher $commandDispatcher
Dispatcher $commandDispatcher,
FileOpenerService $fileOpenerService
) {
$response = $httpClient->request(
Request::METHOD_POST,
Expand All @@ -68,7 +66,7 @@ public function handle(
'headers' => [
'Content-Type' => 'audio/flac',
],
'body' => $filesystemManager->disk()->get($this->getFilePath($this->filename)),
'body' => $fileOpenerService->openForReading($this->getFilePath($this->filename)),
]
);

Expand Down
16 changes: 16 additions & 0 deletions app/Services/FileOpenerService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Services;

class FileOpenerService
{
/**
* @param string $filename
*
* @return bool|resource
*/
public function openForReading($filename)
{
return \fopen($filename, 'r');
}
}
48 changes: 47 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 14 additions & 25 deletions tests/Unit/Jobs/TranscribeAudioFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
use App\Configurations\IbmWatsonConfiguration;
use App\Jobs\ProcessTranscribedText;
use App\Jobs\TranscribeAudioFile;
use App\Services\FileOpenerService;
use GuzzleHttp\Client;
use Illuminate\Bus\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Http\Response;
use PHPUnit_Framework_MockObject_MockObject;
use Psr\Http\Message\MessageInterface;
Expand Down Expand Up @@ -40,28 +39,23 @@ class TranscribeAudioFileTest extends TestCase
*/
private $httpClient;

/**
* @var FilesystemManager|PHPUnit_Framework_MockObject_MockObject
*/
private $fileSystemManager;

/**
* @var Filesystem|PHPUnit_Framework_MockObject_MockObject
*/
private $filesystem;

/**
* @var Dispatcher|PHPUnit_Framework_MockObject_MockObject
*/
private $commandDispatcher;

/**
* @var StreamInterface
* @var StreamInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $streamInterface;

/**
* @var Response
* @var FileOpenerService|PHPUnit_Framework_MockObject_MockObject
*/
private $fileOpenerService;

/**
* @var Response|PHPUnit_Framework_MockObject_MockObject
*/
private $httpMessage;

Expand All @@ -74,8 +68,6 @@ protected function setUp()
{
parent::setUp();

$this->filesystem = $this->getMockForConcreteClass(Filesystem::class);

$this->streamInterface = $this->getMockForConcreteClass(StreamInterface::class);
$this->streamInterface
->method($this->methodName([$this->streamInterface, 'getContents']))
Expand All @@ -88,12 +80,9 @@ protected function setUp()

$this->ibmWatsonConfiguration = $this->getMockForConcreteClass(IbmWatsonConfiguration::class);

$this->httpClient = $this->getMockForConcreteClass(Client::class);
$this->fileOpenerService = $this->getMockForConcreteClass(FileOpenerService::class);

$this->fileSystemManager = $this->getMockForConcreteClass(FilesystemManager::class);
$this->fileSystemManager
->method($this->methodName([$this->fileSystemManager, 'disk']))
->willReturn($this->filesystem);
$this->httpClient = $this->getMockForConcreteClass(Client::class);

$this->commandDispatcher = $this->getMockForConcreteClass(Dispatcher::class);

Expand All @@ -110,8 +99,8 @@ public function test_that_the_command_transcribes_a_file_using_the_ibm_watson_co
$this->SUT->handle(
$this->ibmWatsonConfiguration,
$this->httpClient,
$this->fileSystemManager,
$this->commandDispatcher
$this->commandDispatcher,
$this->fileOpenerService
);
}

Expand All @@ -129,8 +118,8 @@ public function test_that_the_command_calls_the_process_transcribed_text_command
$this->SUT->handle(
$this->ibmWatsonConfiguration,
$this->httpClient,
$this->fileSystemManager,
$this->commandDispatcher
$this->commandDispatcher,
$this->fileOpenerService
);
}
}

0 comments on commit cf82fa9

Please sign in to comment.