Skip to content

Commit

Permalink
Merge pull request alchemy-fr#17 from peter279k/test_enhancement
Browse files Browse the repository at this point in the history
Test enhancement
  • Loading branch information
jygaulier authored Nov 28, 2019
2 parents e521b11 + 8704564 commit b969d93
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 65 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ before_script:
script:
- vendor/bin/phpunit

matrix:
include:
- php: 5.3
dist: precise

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
"require-dev": {
"sami/sami": "^3.3",
"ext-curl": "*",
"phpunit/phpunit": "^4.0|^5.0"
"phpunit/phpunit": "^4.8|^5.5|^6.5|^7.0"
},
"autoload": {
"psr-0": {
"DataURI": "src"
"psr-4": {
"DataURI\\": "src/DataURI",
"DataURI\\Exception\\": "src/DataURI/Exception"
}
},
"autoload-dev": {
"psr-4": {
"DataURI\\Tests\\": "tests/"
}
}
}
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
</testsuites>

<filter>
<blacklist>
<directory>./vendor</directory>
</blacklist>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

Expand Down
2 changes: 0 additions & 2 deletions src/DataURI/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace DataURI;

use DataUri\Data;

/**
*
* @author Nicolas Le Goff
Expand Down
1 change: 0 additions & 1 deletion src/DataURI/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

use DataURI\Exception\InvalidDataException;
use DataURI\Exception\InvalidArgumentException;
use DataURI\Data;

/**
* @author Nicolas Le Goff
Expand Down
64 changes: 39 additions & 25 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,59 @@
* IN THE SOFTWARE.
*/

namespace DataURI\Tests;

use DataURI\Data;
use DataURI\Exception\TooLongDataException;
use PHPUnit\Framework\TestCase;

/**
*
* @author Nicolas Le Goff
* @author Phraseanet team
* @license http://opensource.org/licenses/MIT MIT
*/
class DataTest extends PHPUnit_Framework_TestCase
class DataTest extends TestCase
{

public function testTooLongException()
{
$i = 0;
$string = '';
while ($i < DataURI\Data::ATTS_TAG_LIMIT + 1) {
while ($i < Data::ATTS_TAG_LIMIT + 1) {
$string .= 'x';
$i ++;
}

try {
$dataURI = new DataURI\Data($string, null, array(), true);
$dataURI = new Data($string, null, array(), true);
$this->fail('An exception should have beeen raised');
} catch (DataURI\Exception\TooLongDataException $e) {
$this->assertEquals(DataURI\Data::ATTS_TAG_LIMIT + 1, $e->getLength());
} catch (TooLongDataException $e) {
$this->assertEquals(Data::ATTS_TAG_LIMIT + 1, $e->getLength());
}

$dataURI = new DataURI\Data($string);
$dataURI = new Data($string);

try {
$dataURI = new DataURI\Data($string, null, array(), true, DataURI\Data::LITLEN);
$dataURI = new Data($string, null, array(), true, Data::LITLEN);
$this->fail('An exception should have beeen raised');
} catch (DataURI\Exception\TooLongDataException $e) {
} catch (TooLongDataException $e) {

}
}

public function testGetData()
{
$dataString = 'Lorem ipsum dolor sit amet';
$dataURI = new DataURI\Data($dataString);
$dataURI = new Data($dataString);
$this->assertEquals($dataString, $dataURI->getData());
}

public function testGetMimeType()
{
$dataString = 'Lorem ipsum dolor sit amet';
$mimeType = 'text/plain';
$dataURI = new DataURI\Data($dataString, $mimeType);
$dataURI = new Data($dataString, $mimeType);
$this->assertEquals($mimeType, $dataURI->getMimeType());
}

Expand All @@ -74,55 +80,63 @@ public function testGetParameters()
$dataString = 'Lorem ipsum dolor sit amet';
$mimeType = 'text/plain';
$parameters = array('charset', 'utf-8');
$dataURI = new DataURI\Data($dataString, $mimeType, $parameters);
$dataURI = new Data($dataString, $mimeType, $parameters);
$this->assertEquals($parameters, $dataURI->getParameters());
$this->assertTrue(is_array($dataURI->getParameters()));
$this->assertInternalType('array', $dataURI->getParameters());
}

public function testIsBinaryData()
{
$dataString = 'Lorem ipsum dolor sit amet';
$dataURI = new DataURI\Data($dataString);
$dataURI = new Data($dataString);
$dataURI->setBinaryData(true);
$this->assertTrue($dataURI->isBinaryData());
}

public function testInit()
{
$dataString = 'Lorem ipsum dolor sit amet';
$dataURI = new DataURI\Data($dataString);
$dataURI = new Data($dataString);
$parameters = $dataURI->getParameters();
$this->assertTrue(array_key_exists('charset', $parameters));
$this->assertArrayHasKey('charset', $parameters);
$this->assertEquals('US-ASCII', $parameters['charset']);

$this->assertEquals('text/plain', $dataURI->getMimeType());
}

public function testAddParameters()
{
$dataString = 'Lorem ipsum dolor sit amet';
$dataURI = new DataURI\Data($dataString);
$dataURI = new Data($dataString);
$current = count($dataURI->getParameters());
$dataURI->addParameters('charset', 'iso-8859-7');
$this->assertEquals($current, count($dataURI->getParameters()));
$dataURI->addParameters('another-charset', 'iso-8859-7');
$this->assertGreaterThan($current, count($dataURI->getParameters()));
$this->assertTrue(array_key_exists('another-charset', $dataURI->getParameters()));
$this->assertArrayHasKey('another-charset', $dataURI->getParameters());
}

public function testBuildFromFile()
{
$file = __DIR__ . '/smile.png';
$dataURI = DataURI\Data::buildFromFile($file);
$dataURI = Data::buildFromFile($file);
$this->assertInstanceOf('DataURI\Data', $dataURI);
$this->assertEquals('image/png', $dataURI->getMimeType());
$this->assertEquals(file_get_contents($file), $dataURI->getData());
}

/**
* @expectedException DataURI\Exception\FileNotFoundException
*/
public function testBuildFromUrlShouldThrowFileNotFoundException()
{
$url = 'http://via.placeholder.com/x150.png';
Data::buildFromUrl($url);
}

public function testBuildFromUrl()
{
$url = 'http://via.placeholder.com/350x150.png';
$dataURI = DataURI\Data::buildFromUrl($url);
$dataURI = Data::buildFromUrl($url);
$this->assertInstanceOf('DataURI\Data', $dataURI);
$this->assertEquals('image/png', $dataURI->getMimeType());
$this->assertEquals(file_get_contents($url), $dataURI->getData());
Expand All @@ -136,27 +150,27 @@ public function testFileNotFound()
$filename = __DIR__ . '/unknown-file';

$dataString = 'Lorem ipsum dolor sit amet';
$dataURI = new DataURI\Data($dataString);
$dataURI = new Data($dataString);
$dataURI->write($filename);
}

/**
/**
* @expectedException \DataURI\Exception\FileNotFoundException
*/
public function testFileNotFoundFromFile()
{
$filename = __DIR__ . '/unknown-file';

DataURI\Data::buildFromFile($filename);
Data::buildFromFile($filename);
}

public function testWrite()
{
$filename = __DIR__ . '/test';
$this->createEmptyFile($filename);
$dataString = 'hello world';
$dataURI = new DataURI\Data($dataString);
$dataURI = DataURI\Data::buildFromFile($dataURI->write($filename));
$dataURI = new Data($dataString);
$dataURI = Data::buildFromFile($dataURI->write($filename));
$this->assertEquals($dataString, $dataURI->getData());
unlink($filename);
}
Expand Down
46 changes: 25 additions & 21 deletions tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,45 @@
* IN THE SOFTWARE.
*/

namespace DataURI\Tests;

use DataURI\Parser;
use DataURI\Dumper;
use PHPUnit\Framework\TestCase;

/**
*
* @author Nicolas Le Goff
* @author Phraseanet team
* @license http://opensource.org/licenses/MIT MIT
*/
class DumperTest extends PHPUnit_Framework_TestCase
class DumperTest extends TestCase
{

public function testDump()
public function dumpDataProvider()
{
$b64 = $this->binaryToBase64(__DIR__ . '/smile.png');

$tests = array(
"data:image/png;base64," . $b64,
"data:image/png;paramName=paramValue;base64," . $b64,
"data:text/plain;charset=utf-8,%23%24%25",
"data:application/vnd-xxx-query,select_vcount,fcol_from_fieldtable/local"
return array(
array("data:image/png;base64," . $b64),
array("data:image/png;paramName=paramValue;base64," . $b64),
array("data:text/plain;charset=utf-8,%23%24%25")
);
}

//#1
$dataURI = DataURI\Parser::parse($tests[0]);
$this->assertEquals($tests[0], DataURI\Dumper::dump($dataURI));

//#2
$dataURI = DataURI\Parser::parse($tests[1]);
$this->assertEquals($tests[1], DataURI\Dumper::dump($dataURI));

//#3
$dataURI = DataURI\Parser::parse($tests[2]);
$this->assertEquals($tests[2], DataURI\Dumper::dump($dataURI));
/**
* @dataProvider dumpDataProvider
*/
public function testDump($expectedValue)
{
$dataURI = Parser::parse($expectedValue);
$this->assertEquals($expectedValue, Dumper::dump($dataURI));
}

//#4
$dataURI = DataURI\Parser::parse($tests[3]);
$this->assertEquals($tests[3], rawurldecode(DataURI\Dumper::dump($dataURI)));
public function testDumpOnRawUrlDecodeString()
{
$dataURI = Parser::parse("data:application/vnd-xxx-query,select_vcount,fcol_from_fieldtable/local");
$this->assertEquals("data:application/vnd-xxx-query,select_vcount,fcol_from_fieldtable/local", rawurldecode(Dumper::dump($dataURI)));
}

private function binaryToBase64($file)
Expand Down
25 changes: 15 additions & 10 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
* IN THE SOFTWARE.
*/

namespace DataURI\Tests;

use DataURI\Parser;
use PHPUnit\Framework\TestCase;

/**
*
* @author Nicolas Le Goff
* @author Phraseanet team
* @license http://opensource.org/licenses/MIT MIT
*/
class ParserTest extends PHPUnit_Framework_TestCase
class ParserTest extends TestCase
{

public function testParse()
Expand All @@ -42,28 +47,28 @@ public function testParse()
"data:;base64," . $b64,
);

$dataURI = DataURI\Parser::parse($tests[0]);
$dataURI = Parser::parse($tests[0]);
$this->assertEquals('image/png', $dataURI->getMimeType());
$this->assertTrue($dataURI->isBinaryData());
$this->assertTrue(is_string($dataURI->getData()));
$this->assertInternalType('string', $dataURI->getData());
$this->assertEquals(0, count($dataURI->getParameters()));

$dataURI = DataURI\Parser::parse($tests[1]);
$dataURI = Parser::parse($tests[1]);
$this->assertEquals('image/png', $dataURI->getMimeType());
$this->assertTrue($dataURI->isBinaryData());
$this->assertTrue(is_string($dataURI->getData()));
$this->assertInternalType('string', $dataURI->getData());
$this->assertEquals(1, count($dataURI->getParameters()));

$dataURI = DataURI\Parser::parse($tests[2]);
$dataURI = Parser::parse($tests[2]);
$this->assertEquals('text/plain', $dataURI->getMimeType());
$this->assertFalse($dataURI->isBinaryData());
$this->assertEquals('#$%', $dataURI->getData());
$this->assertEquals(1, count($dataURI->getParameters()));

$dataURI = DataURI\Parser::parse($tests[4]);
$dataURI = Parser::parse($tests[4]);
$this->assertEquals('image/svg+xml', $dataURI->getMimeType());

$dataURI = DataURI\Parser::parse($tests[6]);
$dataURI = Parser::parse($tests[6]);
$this->assertEquals('text/plain', $dataURI->getMimeType());
}

Expand All @@ -73,7 +78,7 @@ public function testParse()
public function testInvalidDataException()
{
$invalidData = 'data:image/gif;base64,';
DataURI\Parser::parse($invalidData);
Parser::parse($invalidData);
}

/**
Expand All @@ -82,7 +87,7 @@ public function testInvalidDataException()
public function testInvalidArgumentException()
{
$invalidData = 'lorem:image:test,datas';
DataURI\Parser::parse($invalidData);
Parser::parse($invalidData);
}

private function binaryToBase64($file)
Expand Down

0 comments on commit b969d93

Please sign in to comment.