Skip to content

Commit

Permalink
feat: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Aug 19, 2023
1 parent 57b42c9 commit 23dbe1a
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 4 deletions.
14 changes: 10 additions & 4 deletions includes/Media/AudioHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,20 @@ public function validateParam( $name, $value ): bool {
* @return false|float|int Integer seconds or false for a bad format.
*/
public function parseTimeString( $time ) {
$parts = explode( ":", $time );
if ( $parts === false ) {
$parts = explode( ':', $time );

if ( $parts === false || empty( $parts[0] ?? '' ) || !is_numeric( $parts[0] ?? null ) ) {
return false;
}
$parts = array_reverse( $parts );

$magnitude = [ 1, 60, 3600, 86400 ];
$seconds = 0;

foreach ( $parts as $index => $part ) {
$seconds += $part * $magnitude[$index];
$seconds += (int)$part * $magnitude[$index];
}

return $seconds;
}

Expand Down Expand Up @@ -293,7 +296,10 @@ protected function getFFProbeResult( $file, string $select = 'v:0' ): array {
}

if ( $file === false ) {
return [];
return [
'stream' => false,
'format' => false,
];
}

$probe = new FFProbe( $path, $file );
Expand Down
280 changes: 280 additions & 0 deletions tests/phpunit/Media/AudioHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
<?php

declare( strict_types=1 );

namespace MediaWiki\Extension\EmbedVideo\Tests\Media;

use MediaWiki\Extension\EmbedVideo\Media\AudioHandler;
use MediaWiki\Extension\EmbedVideo\Media\FFProbe\FormatInfo;
use MediaWiki\Extension\EmbedVideo\Media\FFProbe\StreamInfo;
use MediaWiki\Extension\EmbedVideo\Media\TransformOutput\AudioTransformOutput;
use UnregisteredLocalFile;

class AudioHandlerTest extends \MediaWikiIntegrationTestCase {

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getParamMap
* @return void
*/
public function testParamMap(): void {
$handler = new AudioHandler();

$this->assertIsArray( $handler->getParamMap() );
$this->assertNotEmpty( $handler->getParamMap() );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::validateParam
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::parseTimeString
* @return void
*/
public function testValidateParam(): void {
$handler = new AudioHandler();

$test = [
[ 'width', 0, false ],
[ 'width', 100, true ],
[ 'width', -100, false ],
[ 'width', '-100', false ],

[ 'start', '1:30', true ],
[ 'start', '', false ],

[ 'end', '1:30', true ],
[ 'end', '', false ],

[ 'autoplay', null, true ],
[ 'loop', null, true ],
[ 'nocontrols', null, true ],

[ 'gif', null, false ],
];

foreach ( $test as $test ) {
$this->assertEquals( $test[2], $handler->validateParam( $test[0], $test[1] ) );
}
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::makeParamString
* @return void
*/
public function testMakeParamString(): void {
$handler = new AudioHandler();

$this->assertEmpty( $handler->makeParamString( [] ) );
$this->assertEmpty( $handler->makeParamString( [ 'foo' ] ) );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::parseParamString
* @return void
*/
public function testParseParamString(): void {
$handler = new AudioHandler();

$this->assertEmpty( $handler->parseParamString( '' ) );
$this->assertEmpty( $handler->parseParamString( 'foo' ) );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsWidth(): void {
$handler = new AudioHandler();

$params = [
'width' => 500,
];

$handler->normaliseParams( null, $params );

$this->assertEquals( 500, $params['width'] );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsDefaultWidth(): void {
$this->overrideConfigValues( [
'EmbedVideoDefaultWidth' => 123,
] );

$handler = new AudioHandler();

$params = [];

$handler->normaliseParams( null, $params );

$this->assertEquals( 123, $params['width'] );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsStart(): void {
$handler = new AudioHandler();

$params = [
'start' => '1:30',
];

$handler->normaliseParams( null, $params );

$this->assertArrayHasKey( 'start', $params );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsInvalidStart(): void {
$handler = new AudioHandler();

$params = [
'start' => 'foo',
];

$handler->normaliseParams( null, $params );

$this->assertArrayNotHasKey( 'start', $params );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsEnd(): void {
$handler = new AudioHandler();

$params = [
'end' => '1:30',
];

$handler->normaliseParams( null, $params );

$this->assertArrayHasKey( 'end', $params );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsInvalidEnd(): void {
$handler = new AudioHandler();

$params = [
'end' => 'fobar',
];

$handler->normaliseParams( null, $params );

$this->assertArrayNotHasKey( 'end', $params );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::doTransform
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams
* @return void
*/
public function testNormaliseParamsDoTransform(): void {
$handler = new AudioHandler();

$file = UnregisteredLocalFile::newFromPath( '/tmp', 'video/mp4' );

$transform = $handler->doTransform( $file, '', '', [] );

$this->assertInstanceOf( AudioTransformOutput::class, $transform );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult
* @return void
*/
public function testGetDimensionString(): void {
$handler = $this->getMockBuilder( AudioHandler::class )
->onlyMethods( [ 'getFFProbeResult' ] )
->getMock();

$handler->expects( $this->once() )
->method( 'getFFProbeResult' )
->willReturn( [
'stream' => new StreamInfo( [] ),
'format' => new FormatInfo( [
'duration' => 10,
] ),
] );

$file = UnregisteredLocalFile::newFromPath( '/tmp', 'video/mp4' );

$this->assertIsString( $handler->getDimensionsString( $file ) );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult
* @return void
*/
public function testGetDimensionStringEmpty(): void {
$handler = $this->getMockBuilder( AudioHandler::class )
->onlyMethods( [ 'getFFProbeResult' ] )
->getMock();

$handler->expects( $this->once() )
->method( 'getFFProbeResult' )
->willReturn( [
'stream' => false,
'format' => false,
] );

$file = UnregisteredLocalFile::newFromPath( '/tmp', 'video/mp4' );

$this->assertEmpty( $handler->getDimensionsString( $file ) );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult
* @return void
*/
public function testGetSizeAndMetadataEmpty(): void {
$handler = $this->getMockBuilder( AudioHandler::class )
->onlyMethods( [ 'getFFProbeResult' ] )
->getMock();

$handler->expects( $this->once() )
->method( 'getFFProbeResult' )
->willReturn( [
'stream' => false,
'format' => false,
] );

$this->assertEquals( [ 'metadata' => [] ], $handler->getSizeAndMetadata( null, null ) );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString
* @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult
* @return void
*/
public function testGetSizeAndMetadata(): void {
$handler = $this->getMockBuilder( AudioHandler::class )
->onlyMethods( [ 'getFFProbeResult' ] )
->getMock();

$handler->expects( $this->once() )
->method( 'getFFProbeResult' )
->willReturn( [
'stream' => false,
'format' => new FormatInfo( [
'bit_rate' => 100,
] ),
] );

$this->assertEquals( [ 'metadata' => [], 'bits' => 100 ], $handler->getSizeAndMetadata( null, null ) );
}
}
60 changes: 60 additions & 0 deletions tests/phpunit/Media/VideoHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare( strict_types=1 );

namespace MediaWiki\Extension\EmbedVideo\Tests\Media;

use MediaWiki\Extension\EmbedVideo\Media\VideoHandler;

class VideoHandlerTest extends \MediaWikiIntegrationTestCase {

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\VideoHandler::getParamMap
* @return void
*/
public function testParamMap(): void {
$handler = new VideoHandler();

$this->assertIsArray( $handler->getParamMap() );
$this->assertNotEmpty( $handler->getParamMap() );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\Media\VideoHandler::validateParam
* @covers \MediaWiki\Extension\EmbedVideo\Media\VideoHandler::parseTimeString
* @return void
*/
public function testValidateParam(): void {
$handler = new VideoHandler();

$test = [
[ 'width', 0, false ],
[ 'width', 100, true ],
[ 'width', -100, false ],
[ 'width', '-100', false ],

[ 'start', '1:30', true ],
[ 'start', '', false ],

[ 'end', '1:30', true ],
[ 'end', '', false ],

[ 'autoplay', null, true ],
[ 'loop', null, true ],
[ 'nocontrols', null, true ],
[ 'poster', null, true ],
[ 'gif', null, true ],
[ 'muted', null, true ],
[ 'title', null, true ],
[ 'description', null, true ],
[ 'lazy', null, true ],
[ 'autoresize', null, true ],

[ 'explode', null, false ],
];

foreach ( $test as $test ) {
$this->assertEquals( $test[2], $handler->validateParam( $test[0], $test[1] ) );
}
}
}

0 comments on commit 23dbe1a

Please sign in to comment.