Skip to content

Commit

Permalink
Fix autoload issue by putting Coconut_Job into Job.php
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoceleste committed Sep 4, 2019
1 parent c588c62 commit cd0d9d6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 67 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ Here is the PHP code to submit the config file:
```php
<?php

$job = Coconut_Job::create(array(
require_once('vendor/autoload.php');

$job = Coconut\Job::create(array(
'api_key' => 'k-api-key',
'conf' => 'coconut.conf',
'source' => 'http://yoursite.com/media/video.mp4',
Expand All @@ -67,10 +69,12 @@ You can also create a job without a config file. To do that you will need to giv
```php
<?php

require_once('vendor/autoload.php');

$vid = 1234;
$s3 = 's3://accesskey:secretkey@mybucket';

$job = Coconut_Job::create(array(
$job = Coconut\Job::create(array(
'api_key' => 'k-api-key',
'source' => 'http://yoursite.com/media/video.mp4',
'webhook' => 'http://mysite.com/webhook/coconut?videoId=' . $vid,
Expand All @@ -89,13 +93,13 @@ Other example usage:
```php
<?php
// Getting info about a job
$job = Coconut_Job::get(18370773);
$job = Coconut\Job::get(18370773);

// Retrieving metadata
Coconut_Job::getAllMetadata(18370773);
Coconut\Job::getAllMetadata(18370773);

// Retrieving the source file metadata only
Coconut_Job::getMetadataFor(18370773, 'source');
Coconut\Job::getMetadataFor(18370773, 'source');
?>
```

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "opencoconut/coconut",
"type": "library",
"version": "2.6.0",
"version": "2.8.0",
"description": "Coconut is a Cloud Video Encoding Service built for developers",
"keywords": ["video", "encoding", "transcoding", "web service", "h264", "cloud"],
"homepage": "http://coconut.co",
Expand All @@ -19,6 +19,6 @@
}
},
"require-dev": {
"phpunit/phpunit": "4.3"
"phpunit/phpunit": "7"
}
}
37 changes: 1 addition & 36 deletions src/Coconut.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,39 +142,4 @@ public static function config($options=array()) {

return join("\n", $new_conf);
}
}

class Coconut_Job {
public static function create($options=array()) {
$api_key = self::getApiKey($options);

return Coconut::submit(Coconut::config($options), $api_key);
}

public static function get($jid, $options=array()) {
$api_key = self::getApiKey($options);

return Coconut::get('/v1/jobs/' . $jid, $api_key);
}

public static function getAllMetadata($jid, $options=array()) {
$api_key = self::getApiKey($options);

return Coconut::get('/v1/metadata/jobs/' . $jid, $api_key);
}

public static function getMetadataFor($jid, $source_or_output, $options=array()) {
$api_key = self::getApiKey($options);

return Coconut::get('/v1/metadata/jobs/' . $jid . '/' . $source_or_output, $api_key);
}


private static function getApiKey($options=array()) {
$api_key = null;
if(isset($options['api_key'])) {
$api_key = $options['api_key'];
}
return $api_key;
}
}
}
38 changes: 38 additions & 0 deletions src/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Coconut;

class Job {
public static function create($options=array()) {
$api_key = self::getApiKey($options);

return Coconut::submit(Coconut::config($options), $api_key);
}

public static function get($jid, $options=array()) {
$api_key = self::getApiKey($options);

return Coconut::get('/v1/jobs/' . $jid, $api_key);
}

public static function getAllMetadata($jid, $options=array()) {
$api_key = self::getApiKey($options);

return Coconut::get('/v1/metadata/jobs/' . $jid, $api_key);
}

public static function getMetadataFor($jid, $source_or_output, $options=array()) {
$api_key = self::getApiKey($options);

return Coconut::get('/v1/metadata/jobs/' . $jid . '/' . $source_or_output, $api_key);
}


private static function getApiKey($options=array()) {
$api_key = null;
if(isset($options['api_key'])) {
$api_key = $options['api_key'];
}
return $api_key;
}
}
48 changes: 24 additions & 24 deletions tests/CoconutTest.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
<?php
use Coconut\Coconut;
use Coconut\Coconut_Job;

class CoconutTest extends PHPUnit_Framework_TestCase {
use PHPUnit\Framework\TestCase;

class CoconutTest extends TestCase {

/*
To run these tests, you need to set your API key with the
environment variable `COCONUT_API_KEY`
*/

public function testSubmitJob() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'webhook' => 'http://mysite.com/webhook',
'outputs' => array('mp4' => 's3://a:s@bucket/video.mp4')
));

$job = Coconut::submit($config);
$job = Coconut\Coconut::submit($config);
$this->assertEquals('processing', $job->{'status'});
$this->assertTrue($job->{'id'} > 0);
}

public function testSubmitBadConfig() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4'
));

$job = Coconut::submit($config);
$job = Coconut\Coconut::submit($config);
$this->assertEquals('error', $job->{'status'});
$this->assertEquals('config_not_valid', $job->{'error_code'});
}

public function testSubmitConfigWithAPIKey() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4'
));

$job = Coconut::submit($config, 'k-4d204a7fd1fc67fc00e87d3c326d9b75');
$job = Coconut\Coconut::submit($config, 'k-4d204a7fd1fc67fc00e87d3c326d9b75');
$this->assertEquals('error', $job->{'status'});
$this->assertEquals('authentication_failed', $job->{'error_code'});
}

public function testGenerateFullConfigWithNoFile() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'vars' => array(
'vid' => 1234,
'user' => 5098,
Expand Down Expand Up @@ -78,7 +78,7 @@ public function testGenerateConfigWithFile() {
fwrite($file, 'var s3 = s3://a:s@bucket/video' . "\n" . 'set webhook = http://mysite.com/webhook?vid=$vid&user=$user' . "\n" . '-> mp4 = $s3/$vid.mp4');
fclose($file);

$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'conf' => 'coconut.conf',
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'vars' => array('vid' => 1234, 'user' => 5098)
Expand All @@ -105,7 +105,7 @@ public function testSubmitFile() {
fwrite($file, 'var s3 = s3://a:s@bucket/video' . "\n" . 'set webhook = http://mysite.com/webhook?vid=$vid&user=$user' . "\n" . '-> mp4 = $s3/$vid.mp4');
fclose($file);

$job = Coconut_Job::create(array(
$job = Coconut\Job::create(array(
'conf' => 'coconut.conf',
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'vars' => array('vid' => 1234, 'user' => 5098)
Expand All @@ -118,7 +118,7 @@ public function testSubmitFile() {
}

public function testSetApiKeyInJobOptions() {
$job = Coconut_Job::create(array(
$job = Coconut\Job::create(array(
'api_key' => 'k-4d204a7fd1fc67fc00e87d3c326d9b75',
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4'
));
Expand All @@ -128,53 +128,53 @@ public function testSetApiKeyInJobOptions() {
}

public function testGetJobInfo() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'webhook' => 'http://mysite.com/webhook',
'outputs' => array('mp4' => 's3://a:s@bucket/video.mp4')
));

$job = Coconut::submit($config);
$info = Coconut_Job::get($job->{"id"});
$job = Coconut\Coconut::submit($config);
$info = Coconut\Job::get($job->{"id"});

$this->assertEquals($info->{"id"}, $job->{"id"});
}

public function testGetNotFoundJobReturnsNull() {
$info = Coconut_Job::get(1000);
$info = Coconut\Job::get(1000);
$this->assertNull($info);
}

public function testGetAllMetadata() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'webhook' => 'http://mysite.com/webhook',
'outputs' => array('mp4' => 's3://a:s@bucket/video.mp4')
));

$job = Coconut::submit($config);
$job = Coconut\Coconut::submit($config);
sleep(4);

$metadata = Coconut_Job::getAllMetadata($job->{"id"});
$metadata = Coconut\Job::getAllMetadata($job->{"id"});
$this->assertNotNull($metadata);
}

public function testGetMetadataForSource() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'webhook' => 'http://mysite.com/webhook',
'outputs' => array('mp4' => 's3://a:s@bucket/video.mp4')
));

$job = Coconut::submit($config);
$job = Coconut\Coconut::submit($config);
sleep(4);

$metadata = Coconut_Job::getMetadataFor($job->{"id"}, 'source');
$metadata = Coconut\Job::getMetadataFor($job->{"id"}, 'source');
$this->assertNotNull($metadata);
}

public function testSetAPIVersion() {
$config = Coconut::config(array(
$config = Coconut\Coconut::config(array(
'source' => 'https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4',
'webhook' => 'http://mysite.com/webhook?vid=$vid&user=$user',
'api_version' => 'beta',
Expand Down

0 comments on commit cd0d9d6

Please sign in to comment.