Skip to content

Commit

Permalink
Adding tests on appveyor (#20)
Browse files Browse the repository at this point in the history
* Adding tests on appveyor

* typo

* Updating test command

* Updating test command

* Updated tests to avoid specify filename

* Specify filename

* throwing in some swedish characters

* and some greek

* test with ä

* Applied fix with pathinfo

* Better tests

* disable weird locale

* Using pathinfo

* Updated tests

* Added function to get basename

* style fix
  • Loading branch information
Nyholm committed Dec 22, 2016
1 parent 968b644 commit 1f58220
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
39 changes: 39 additions & 0 deletions appveyor.yml
@@ -0,0 +1,39 @@
build: false
platform:
- x86
- x64

clone_folder: c:\projects\php-http\multipart-stream-builder

cache:
- c:\tools\php -> appveyor.yml

init:
- SET PATH=c:\php;%PATH%
- SET COMPOSER_NO_INTERACTION=1
- SET PHP=1


install:
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
- cd c:\php
- IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/php-7.0.0-nts-Win32-VC14-x86.zip
- IF %PHP%==1 7z x php-7.0.0-nts-Win32-VC14-x86.zip -y >nul
- IF %PHP%==1 del /Q *.zip
- IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat
- IF %PHP%==1 copy /Y php.ini-development php.ini
- IF %PHP%==1 echo max_execution_time=1200 >> php.ini
- IF %PHP%==1 echo date.timezone="UTC" >> php.ini
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
- IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini
- IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini
- appveyor DownloadFile https://getcomposer.org/composer.phar
- cd c:\projects\php-http\multipart-stream-builder
- mkdir %APPDATA%\Composer
- composer update --prefer-dist --no-progress --ansi

test_script:
- cd c:\projects\php-http\multipart-stream-builder
- vendor\bin\phpunit.bat --verbose

29 changes: 28 additions & 1 deletion src/MultipartStreamBuilder.php
Expand Up @@ -121,7 +121,7 @@ private function prepareHeaders($name, StreamInterface $stream, $filename, array
if (!$this->hasHeader($headers, 'content-disposition')) {
$headers['Content-Disposition'] = sprintf('form-data; name="%s"', $name);
if ($hasFilename) {
$headers['Content-Disposition'] .= sprintf('; filename="%s"', pathinfo($filename, PATHINFO_BASENAME));
$headers['Content-Disposition'] .= sprintf('; filename="%s"', $this->basename($filename));
}
}

Expand Down Expand Up @@ -228,4 +228,31 @@ public function setMimetypeHelper(MimetypeHelper $mimetypeHelper)

return $this;
}

/**
* Gets the filename from a given path.
*
* PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
*
* @author Drupal 8.2
*
* @param string $path
*
* @return string
*/
private function basename($path)
{
$separators = '/';
if (DIRECTORY_SEPARATOR != '/') {
// For Windows OS add special separator.
$separators .= DIRECTORY_SEPARATOR;
}
// Remove right-most slashes when $uri points to directory.
$path = rtrim($path, $separators);
// Returns the trailing part of the $uri starting after one of the directory
// separators.
$filename = preg_match('@[^'.preg_quote($separators, '@').']+$@', $path, $matches) ? $matches[0] : '';

return $filename;
}
}
20 changes: 20 additions & 0 deletions tests/FunctionTest.php
Expand Up @@ -33,6 +33,26 @@ public function testSupportResources()
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
}

public function testResourceFilenameIsNotLocaleAware()
{
// Get current locale
$originalLocale = setlocale(LC_ALL, "0");

// Set locale to something strange.
setlocale(LC_ALL, 'C');

$resource = fopen(__DIR__.'/Resources/httplug.png', 'r');
$builder = new MultipartStreamBuilder();
$builder->addResource('image', $resource, ['filename'=> 'äa.png']);

$multipartStream = (string) $builder->build();
$this->assertTrue(0 < preg_match('|filename="([^"]*?)"|si', $multipartStream, $matches), 'Could not find any filename in output.');
$this->assertEquals('äa.png', $matches[1]);

// Reset the locale
setlocale(LC_ALL, $originalLocale);
}

public function testHeaders()
{
$builder = new MultipartStreamBuilder();
Expand Down

0 comments on commit 1f58220

Please sign in to comment.