Use dataProviders in AttachmentTest.

This commit is contained in:
Jan Böhmer 2019-10-20 00:24:52 +02:00
parent 89acf91fe6
commit 40ca5bd0e4

View file

@ -39,119 +39,141 @@ use ReflectionClass;
class AttachmentTest extends TestCase class AttachmentTest extends TestCase
{ {
public function testIsExternal() public function externalDataProvider()
{ {
$attachment = new PartAttachment(); return [
['', false],
$this->setProtectedProperty($attachment, 'path', ''); ['%MEDIA%/foo/bar.txt', false],
$this->assertFalse($attachment->isExternal()); ['%BASE%/foo/bar.jpg', false],
['%FOOTPRINTS%/foo/bar.jpg', false],
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt'); ['%FOOTPRINTS3D%/foo/bar.jpg', false],
$this->assertFalse($attachment->isExternal()); ['%SECURE%/test.txt', false],
['%test%/foo/bar.ghp', true],
$this->setProtectedProperty($attachment, 'path', '%BASE%/foo/bar.jpg'); ['foo%MEDIA%/foo.jpg', true],
$this->assertFalse($attachment->isExternal()); ['foo%MEDIA%/%BASE%foo.jpg', true]
];
$this->setProtectedProperty($attachment, 'path', '%FOOTPRINTS%/foo/bar.jpg');
$this->assertFalse($attachment->isExternal());
$this->setProtectedProperty($attachment, 'path', '%FOOTPRINTS3D%/foo/bar.jpg');
$this->assertFalse($attachment->isExternal());
//Every other string is not a external attachment
$this->setProtectedProperty($attachment, 'path', '%test%/foo/bar.ghp');
$this->assertTrue($attachment->isExternal());
$this->setProtectedProperty($attachment, 'path', 'foo%MEDIA%/foo.jpg');
$this->assertTrue($attachment->isExternal());
$this->setProtectedProperty($attachment, 'path', 'foo%MEDIA%/%BASE%foo.jpg');
$this->assertTrue($attachment->isExternal());
} }
public function testGetExtension() /**
* @dataProvider externalDataProvider
*/
public function testIsExternal($path, $expected)
{ {
$attachment = new PartAttachment(); $attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt'); $this->setProtectedProperty($attachment, 'path', $path);
$this->assertEquals('txt', $attachment->getExtension()); $this->assertEquals($expected, $attachment->isExternal());
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg');
$this->assertEquals('jpeg', $attachment->getExtension());
//Test if we can override the filename
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg');
$this->setProtectedProperty($attachment, 'original_filename', 'test.txt');
$this->assertEquals('txt', $attachment->getExtension());
$this->setProtectedProperty($attachment, 'path', 'https://foo.bar');
$this->assertNull( $attachment->getExtension());
$this->setProtectedProperty($attachment, 'path', 'https://foo.bar/test.jpeg');
$this->assertNull( $attachment->getExtension());
} }
public function testIsPicture() public function extensionDataProvider()
{ {
$attachment = new PartAttachment(); return [
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt'); ['%MEDIA%/foo/bar.txt', null, 'txt'],
$this->assertFalse($attachment->isPicture()); ['%MEDIA%/foo/bar.JPeg', null, 'jpeg'],
['%MEDIA%/foo/bar.JPeg', 'test.txt', 'txt'],
$this->setProtectedProperty($attachment, 'path', 'https://test.de/picture.jpeg'); ['%MEDIA%/foo/bar', null, ''],
$this->assertTrue($attachment->isPicture()); ['%MEDIA%/foo.bar', 'bar', ''],
['http://google.de', null, null],
$this->setProtectedProperty($attachment, 'path', 'https://test.de'); ['https://foo.bar', null, null],
$this->assertTrue($attachment->isPicture()); ['https://foo.bar/test.jpeg', null, null],
['test', null, null],
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.jpeg'); ['test.txt', null, null],
$this->assertTrue($attachment->isPicture()); ];
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.webp');
$this->assertTrue($attachment->isPicture());
} }
public function testIsBuiltIn() /**
* @dataProvider extensionDataProvider
*/
public function testGetExtension($path, $originalFilename, $expected)
{ {
$attachment = new PartAttachment(); $attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt'); $this->setProtectedProperty($attachment, 'path', $path);
$this->assertFalse($attachment->isBuiltIn()); $this->setProtectedProperty($attachment, 'original_filename', $originalFilename);
$this->assertEquals($expected, $attachment->getExtension());
$this->setProtectedProperty($attachment, 'path', '%BASE%/foo/bar.txt');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', '/');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', 'https://google.de');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', '%FOOTPRINTS%/foo/bar.txt');
$this->assertTrue($attachment->isBuiltIn());
} }
public function testGetHost() public function pictureDataProvider()
{ {
$attachment = new PartAttachment(); return [
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt'); ['%MEDIA%/foo/bar.txt', false],
$this->assertNull($attachment->getHost()); ['https://test.de/picture.jpeg', true],
['https://test.de', true],
$this->setProtectedProperty($attachment, 'path', 'https://www.google.de/test.txt'); ['http://test.de/google.de', true],
$this->assertEquals('www.google.de', $attachment->getHost()); ['%MEDIA%/foo/bar.jpeg', true],
['%MEDIA%/foo/bar.webp', true],
['%MEDIA%/foo', false],
['%SECURE%/foo.txt/test', false],
];
} }
public function testGetFilename() /**
* @dataProvider pictureDataProvider
*/
public function testIsPicture($path, $expected)
{ {
$attachment = new PartAttachment(); $attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt'); $this->setProtectedProperty($attachment, 'path', $path);
$this->assertEquals('bar.txt', $attachment->getFilename()); $this->assertEquals($expected, $attachment->isPicture());
}
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.JPeg'); public function builtinDataProvider()
$this->setProtectedProperty($attachment, 'original_filename', 'test.txt'); {
$this->assertEquals('test.txt', $attachment->getFilename()); return [
['', false],
['%MEDIA%/foo/bar.txt', false],
['%BASE%/foo/bar.txt', false],
['/', false],
['https://google.de', false],
['%FOOTPRINTS%/foo/bar.txt', true]
];
}
$this->setProtectedProperty($attachment, 'path', 'https://www.google.de/test.txt'); /**
$this->assertNull($attachment->getFilename()); * @dataProvider builtinDataProvider
*/
public function testIsBuiltIn($path, $expected)
{
$attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', $path);
$this->assertEquals($expected, $attachment->isBuiltIn());
}
public function hostDataProvider()
{
return [
['%MEDIA%/foo/bar.txt', null],
['https://www.google.de/test.txt', 'www.google.de'],
['https://foo.bar/test?txt=test', 'foo.bar'],
];
}
/**
* @dataProvider hostDataProvider
*/
public function testGetHost($path, $expected)
{
$attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', $path);
$this->assertEquals($expected, $attachment->getHost());
}
public function filenameProvider()
{
return [
['%MEDIA%/foo/bar.txt', null, 'bar.txt'],
['%MEDIA%/foo/bar.JPeg', 'test.txt', 'test.txt'],
['https://www.google.de/test.txt', null, null]
];
}
/**
* @dataProvider filenameProvider
*/
public function testGetFilename($path, $original_filename, $expected)
{
$attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', $path);
$this->setProtectedProperty($attachment, 'original_filename', $original_filename);
$this->assertEquals($expected, $attachment->getFilename());
} }
public function testIsURL() public function testIsURL()