Skip to content

Commit

Permalink
Create UploadTest and deprecate parts of Upload class.php (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 authored Feb 7, 2025
1 parent 7eb38a5 commit 9deaeed
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
28 changes: 28 additions & 0 deletions application/libraries/Ilch/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ class Upload extends \Ilch\Controller\Base

/**
* @var string $ending
* @deprecated
*/
protected $ending;

/**
* @var string $name
* @deprecated
*/
protected $name;

/**
* @var string $fileName
* @deprecated
*/
protected $fileName;

Expand All @@ -45,6 +48,7 @@ class Upload extends \Ilch\Controller\Base

/**
* @var string $types
* @deprecated
*/
protected $types;

Expand All @@ -60,16 +64,19 @@ class Upload extends \Ilch\Controller\Base

/**
* @var string $path
* @deprecated
*/
protected $mediaExtImage;

/**
* @var string $size
* @deprecated
*/
protected $size;

/**
* __construct
* @deprecated
*/
public function __construct()
{
Expand All @@ -78,6 +85,7 @@ public function __construct()

/**
* Resets
* @deprecated
*/
public function reset(): Upload
{
Expand Down Expand Up @@ -117,6 +125,7 @@ public function getFile(): string
* @param string $ending
*
* @return Upload Ending
* @deprecated Just use setFile instead.
*/
public function setEnding(string $ending): Upload
{
Expand All @@ -127,6 +136,7 @@ public function setEnding(string $ending): Upload

/**
* @return string
* @deprecated Use getExtension instead.
*/
public function getEnding(): string
{
Expand All @@ -135,10 +145,22 @@ public function getEnding(): string
return $this->ending;
}

/**
* Get the extension of file. This gets determined from the value of file.
*
* @return string
* @since 2.2.9
*/
public function getExtension(): string
{
return strtolower(pathinfo($this->file, PATHINFO_EXTENSION));
}

/**
* @param string $name
*
* @return Upload Name
* @deprecated Just use setFile instead.
*/
public function setName(string $name): Upload
{
Expand All @@ -148,6 +170,8 @@ public function setName(string $name): Upload
}

/**
* Get the name of the file. This gets determined from the value of file.
*
* @return string
*/
public function getName(): string
Expand All @@ -161,6 +185,7 @@ public function getName(): string
* @param string $fileName
*
* @return Upload fileName
* @deprecated
*/
public function setFileName(string $fileName): Upload
{
Expand All @@ -171,6 +196,7 @@ public function setFileName(string $fileName): Upload

/**
* @return string
* @deprecated
*/
public function getFileName(): string
{
Expand Down Expand Up @@ -201,6 +227,7 @@ public function getUrl(): string
* @param string $types
*
* @return Upload types
* @deprecated Use setAllowedExtensions instead.
*/
public function setTypes(string $types): Upload
{
Expand All @@ -211,6 +238,7 @@ public function setTypes(string $types): Upload

/**
* @return string
* @deprecated Use getAllowedExtensions instead.
*/
public function getTypes(): string
{
Expand Down
79 changes: 79 additions & 0 deletions tests/libraries/ilch/UploadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* @package ilch_phpunit
*/

namespace Ilch;

use PHPUnit\Ilch\TestCase;

class UploadTest extends TestCase
{
protected Upload $upload;

protected function setUp(): void
{
parent::setUp();
$this->upload = new Upload();
}

protected function tearDown(): void
{
unset($this->upload);
parent::tearDown();
}

public function testProperties()
{
$this->upload->setFile('file.png')
->setEnding('png')
->setFileName('file')
->setUrl('https://test/file.png')
->setUrlThumb('https://test/file_thumb.png')
->setTypes('png jpg jpeg')
->setAllowedExtensions('png jpg jpeg')
->setPath('file');

self::assertSame('file.png', $this->upload->getFile());
self::assertSame('png', $this->upload->getEnding());
self::assertSame('file', $this->upload->getFileName());
self::assertSame('https://test/file.png', $this->upload->getUrl());
self::assertSame('https://test/file_thumb.png', $this->upload->getUrlThumb());
self::assertSame('png jpg jpeg', $this->upload->getTypes());
self::assertSame('png jpg jpeg', $this->upload->getAllowedExtensions());
self::assertSame('file', $this->upload->getPath());
}

public function testIsAllowedExtension()
{
$this->upload->setFile('file.png')
->setAllowedExtensions('png jpg jpeg');

self::assertTrue($this->upload->isAllowedExtension());
}

public function testIsAllowedExtensionFalse()
{
$this->upload->setFile('file.php')
->setAllowedExtensions('png jpg jpeg');

self::assertFalse($this->upload->isAllowedExtension());
}

public function testReturnBytes()
{
self::assertSame(1048576, $this->upload->returnBytes('1M'));
self::assertSame(1024, $this->upload->returnBytes('1K'));
self::assertSame(1073741824, $this->upload->returnBytes('1G'));
self::assertSame(1048576, $this->upload->returnBytes('1m'));
self::assertSame(1024, $this->upload->returnBytes('1k'));
self::assertSame(1073741824, $this->upload->returnBytes('1g'));

self::assertSame(2097152, $this->upload->returnBytes('2M'));
self::assertSame(2048, $this->upload->returnBytes('2K'));
self::assertSame(2147483648, $this->upload->returnBytes('2G'));

self::assertSame('1024', $this->upload->returnBytes('1024'));
}
}

0 comments on commit 9deaeed

Please sign in to comment.