Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Fix tests for array vs Object.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 3, 2020
1 parent ca411db commit f99a1cf
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 112 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.idea/
/.idea/
/vendor
/plugins
/tmp
/nbproject
/config/Migrations/schema-dump-default.lock
/clover.xml
composer.lock
/composer.lock
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"stan": "phpstan analyse src/ && psalm --show-info=false",
"stan-test": "phpstan analyse tests/",
"psalm": "psalm --show-info=false",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.11 vimeo/psalm:^3.0 && mv composer.backup composer.json",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12 vimeo/psalm:^3.0 && mv composer.backup composer.json",
"rector": "rector process src/",
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:^0.4.11 && mv composer.backup composer.json",
"test": "phpunit",
Expand Down
12 changes: 8 additions & 4 deletions src/FileStorage/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ public function entityToFileObject(EntityInterface $entity): FileInterface
}

if ($entity->has('file')) {
/**
* @var $uploadedFile \Psr\Http\Message\UploadedFileInterface
*/
/** @var $uploadedFile \Psr\Http\Message\UploadedFileInterface|array */
$uploadedFile = $entity->get('file');
$file = $file->withFile($uploadedFile->getStream()->getMetadata('uri'));
if (!is_array($uploadedFile)) {
$filename = $uploadedFile->getStream()->getMetadata('uri');
} else {
$filename = $uploadedFile['tmp_name'];
}

$file = $file->withFile($filename);
}

return $file;
Expand Down
62 changes: 37 additions & 25 deletions src/Model/Behavior/FileStorageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace Burzum\FileStorage\Model\Behavior;

use ArrayAccess;
use ArrayObject;
use Burzum\FileStorage\FileStorage\DataTransformer;
use Burzum\FileStorage\FileStorage\DataTransformerInterface;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Phauthentic\Infrastructure\Storage\FileInterface;
use Phauthentic\Infrastructure\Storage\FileStorage;
Expand Down Expand Up @@ -101,16 +101,24 @@ public function getStorageAdapter($configName)
/**
* Checks if a file upload is present.
*
* @param \Cake\Datasource\EntityInterface|array $entity
* @param \Cake\Datasource\EntityInterface|\ArrayObject $entity
* @return bool
*/
protected function isFileUploadPresent($entity)
{
$field = $this->getConfig('fileField');
if ($this->getConfig('ignoreEmptyFile') === true) {
if (!isset($entity[$field]) || $entity[$field]->getError() === UPLOAD_ERR_NO_FILE) {
if (!isset($entity[$field])) {
return false;
}

/** @var \Psr\Http\Message\UploadedFileInterface|array $file */
$file = $entity[$field];
if (!is_array($file)) {
return $file->getError() !== UPLOAD_ERR_NO_FILE;
}

return $file['error'] !== UPLOAD_ERR_NO_FILE;
}

return true;
Expand All @@ -119,11 +127,11 @@ protected function isFileUploadPresent($entity)
/**
* beforeMarshal callback
*
* @param \Cake\Event\Event $event
* @param \ArrayAccess $data
* @param \Cake\Event\EventInterface $event
* @param \ArrayObject $data
* @return void
*/
public function beforeMarshal(Event $event, ArrayAccess $data)
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
{
if ($this->isFileUploadPresent($data)) {
$this->getFileInfoFromUpload($data);
Expand All @@ -133,11 +141,12 @@ public function beforeMarshal(Event $event, ArrayAccess $data)
/**
* beforeSave callback
*
* @param \Cake\Event\Event $event
* @param \Cake\Event\EventInterface $event
* @param \Cake\Datasource\EntityInterface $entity
* @param \ArrayObject $options
* @return void
*/
public function beforeSave(Event $event, EntityInterface $entity)
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
if (!$this->isFileUploadPresent($entity)) {
return;
Expand All @@ -154,12 +163,12 @@ public function beforeSave(Event $event, EntityInterface $entity)
/**
* afterSave callback
*
* @param \Cake\Event\Event $event
* @param \Cake\Event\EventInterface $event
* @param \Cake\Datasource\EntityInterface $entity
* @param array $options
* @param \ArrayObject $options
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity, $options)
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
if (!$this->isFileUploadPresent($entity)) {
return;
Expand Down Expand Up @@ -190,7 +199,6 @@ public function afterSave(Event $event, EntityInterface $entity, $options)
'entity' => $entity,
'storageAdapter' => $this->getStorageAdapter($entity->get('adapter'))
], $this->getTable());

}

/**
Expand All @@ -215,12 +223,12 @@ protected function checkEntityBeforeSave(EntityInterface $entity)
/**
* afterDelete callback
*
* @param \Cake\Event\Event $event
* @param \Cake\Event\EventInterface $event
* @param \Cake\Datasource\EntityInterface $entity
* @param array $options
* @return bool
* @param \ArrayObject $options
* @return void
*/
public function afterDelete(Event $event, EntityInterface $entity, $options)
public function afterDelete(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
$this->dispatchEvent('FileStorage.afterDelete', [
'entity' => $entity,
Expand All @@ -243,15 +251,19 @@ public function afterDelete(Event $event, EntityInterface $entity, $options)
*/
protected function getFileInfoFromUpload(&$upload, $field = 'file')
{
/**
* @var $uploadedFile \Psr\Http\Message\UploadedFileInterface
*/
/** @var \Psr\Http\Message\UploadedFileInterface|array $uploadedFile */
$uploadedFile = $upload[$field];

$upload['filesize'] = $uploadedFile->getSize();
$upload['mime_type'] = $uploadedFile->getClientMediaType();
$upload['extension'] = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$upload['filename'] = $uploadedFile->getClientFilename();
if (!is_array($uploadedFile)) {
$upload['filesize'] = $uploadedFile->getSize();
$upload['mime_type'] = $uploadedFile->getClientMediaType();
$upload['extension'] = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$upload['filename'] = $uploadedFile->getClientFilename();
} else {
$upload['filesize'] = $uploadedFile['size'];
$upload['mime_type'] = $uploadedFile['type'];
$upload['extension'] = pathinfo($uploadedFile['name'], PATHINFO_EXTENSION);
$upload['filename'] = $uploadedFile['name'];
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Entity/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getVariantPath(string $variant): ?string
protected function _getVariantUrls() {
$variants = (array)$this->get('variants');
$list = [
'original' => $this->get('path')
'original' => $this->get('url')
];

foreach ($variants as $name => $data) {
Expand Down
27 changes: 17 additions & 10 deletions src/View/Helper/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ class ImageHelper extends Helper
/**
* Generates an image url based on the image record data and the used Gaufrette adapter to store it
*
* @param \Burzum\FileStorage\Model\Entity\FileStorageEntityInterface $image FileStorage entity or whatever else table that matches this helpers needs without
* @param \Burzum\FileStorage\Model\Entity\FileStorageEntityInterface|null $image FileStorage entity or whatever else table that matches this helpers needs without
* the model, we just want the record fields
* @param string|null $version Image version string
* @param array $options HtmlHelper::image(), 2nd arg options array
* @return string
*/
public function display(FileStorageEntityInterface $image, ?string $version = null, array $options = []): string
public function display(?FileStorageEntityInterface $image, ?string $version = null, array $options = []): string
{
if ($image === null) {
return $this->fallbackImage($options, $image, $version);
return $this->fallbackImage($options, $version);
}

$url = $this->imageUrl($image, $version, $options);
if ($url !== null) {
return $this->Html->image($url, $options);
}

return $this->fallbackImage($options, $image, $version);
return $this->fallbackImage($options, $version);
}

/**
Expand All @@ -70,18 +70,26 @@ public function display(FileStorageEntityInterface $image, ?string $version = nu
public function imageUrl(FileStorageEntityInterface $image, ?string $variant = null, array $options = []): ?string
{
if ($variant === null) {
$url = $image->get('path');
$url = $image->get('url');
if ($url) {
return $url;
}
$path = $image->get('path');
} else {
$url = $image->getVariantPath($variant);
$url = $image->getVariantUrl($variant);
if ($url) {
return $url;
}
$path = $image->getVariantPath($variant);
}

if (empty($url)) {
if (!$path) {
throw VariantDoesNotExistException::withName($variant);
}

$options = array_merge($this->getConfig(), $options);
if (!empty($options['pathPrefix'])) {
$url = $options['pathPrefix'] . $url;
$url = $options['pathPrefix'] . $path;
}

return $this->normalizePath((string)$url);
Expand All @@ -91,11 +99,10 @@ public function imageUrl(FileStorageEntityInterface $image, ?string $variant = n
* Provides a fallback image if the image record is empty
*
* @param array $options
* @param array $image
* @param string|null $version
* @return string
*/
public function fallbackImage(array $options = [], array $image = [], ?string $version = null): string
public function fallbackImage(array $options = [], ?string $version = null): string
{
if (isset($options['fallback'])) {
if ($options['fallback'] === true) {
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/FileStorageTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ class FileStorageTestCase extends TestCase
*
* @var array
*/
public $fixtures = [
'plugin.Burzum\FileStorage.FileStorage',
protected $fixtures = [
'plugin.Burzum/FileStorage.FileStorage',
];

/**
* FileStorage Table instance.
*
* @var \Burzum\FileStorage\Model\Table\FileStorageTable
*/
public $FileStorage;
protected $FileStorage;

/**
* Path to the file fixtures, set in the setUp() method.
*
* @var string
*/
public string $fileFixtures;
protected string $fileFixtures;

/**
* Test file path
*
* @var string
*/
public string $testPath = '';
protected string $testPath = '';

/**
* Setup test folders and files
Expand Down
Loading

0 comments on commit f99a1cf

Please sign in to comment.