Skip to content

Commit 1f58220

Browse files
authored
Adding tests on appveyor (#20)
* 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
1 parent 968b644 commit 1f58220

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

appveyor.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
build: false
2+
platform:
3+
- x86
4+
- x64
5+
6+
clone_folder: c:\projects\php-http\multipart-stream-builder
7+
8+
cache:
9+
- c:\tools\php -> appveyor.yml
10+
11+
init:
12+
- SET PATH=c:\php;%PATH%
13+
- SET COMPOSER_NO_INTERACTION=1
14+
- SET PHP=1
15+
16+
17+
install:
18+
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
19+
- cd c:\php
20+
- IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/php-7.0.0-nts-Win32-VC14-x86.zip
21+
- IF %PHP%==1 7z x php-7.0.0-nts-Win32-VC14-x86.zip -y >nul
22+
- IF %PHP%==1 del /Q *.zip
23+
- IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat
24+
- IF %PHP%==1 copy /Y php.ini-development php.ini
25+
- IF %PHP%==1 echo max_execution_time=1200 >> php.ini
26+
- IF %PHP%==1 echo date.timezone="UTC" >> php.ini
27+
- IF %PHP%==1 echo extension_dir=ext >> php.ini
28+
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
29+
- IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini
30+
- IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini
31+
- appveyor DownloadFile https://getcomposer.org/composer.phar
32+
- cd c:\projects\php-http\multipart-stream-builder
33+
- mkdir %APPDATA%\Composer
34+
- composer update --prefer-dist --no-progress --ansi
35+
36+
test_script:
37+
- cd c:\projects\php-http\multipart-stream-builder
38+
- vendor\bin\phpunit.bat --verbose
39+

src/MultipartStreamBuilder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function prepareHeaders($name, StreamInterface $stream, $filename, array
121121
if (!$this->hasHeader($headers, 'content-disposition')) {
122122
$headers['Content-Disposition'] = sprintf('form-data; name="%s"', $name);
123123
if ($hasFilename) {
124-
$headers['Content-Disposition'] .= sprintf('; filename="%s"', pathinfo($filename, PATHINFO_BASENAME));
124+
$headers['Content-Disposition'] .= sprintf('; filename="%s"', $this->basename($filename));
125125
}
126126
}
127127

@@ -228,4 +228,31 @@ public function setMimetypeHelper(MimetypeHelper $mimetypeHelper)
228228

229229
return $this;
230230
}
231+
232+
/**
233+
* Gets the filename from a given path.
234+
*
235+
* PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
236+
*
237+
* @author Drupal 8.2
238+
*
239+
* @param string $path
240+
*
241+
* @return string
242+
*/
243+
private function basename($path)
244+
{
245+
$separators = '/';
246+
if (DIRECTORY_SEPARATOR != '/') {
247+
// For Windows OS add special separator.
248+
$separators .= DIRECTORY_SEPARATOR;
249+
}
250+
// Remove right-most slashes when $uri points to directory.
251+
$path = rtrim($path, $separators);
252+
// Returns the trailing part of the $uri starting after one of the directory
253+
// separators.
254+
$filename = preg_match('@[^'.preg_quote($separators, '@').']+$@', $path, $matches) ? $matches[0] : '';
255+
256+
return $filename;
257+
}
231258
}

tests/FunctionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ public function testSupportResources()
3333
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
3434
}
3535

36+
public function testResourceFilenameIsNotLocaleAware()
37+
{
38+
// Get current locale
39+
$originalLocale = setlocale(LC_ALL, "0");
40+
41+
// Set locale to something strange.
42+
setlocale(LC_ALL, 'C');
43+
44+
$resource = fopen(__DIR__.'/Resources/httplug.png', 'r');
45+
$builder = new MultipartStreamBuilder();
46+
$builder->addResource('image', $resource, ['filename'=> 'äa.png']);
47+
48+
$multipartStream = (string) $builder->build();
49+
$this->assertTrue(0 < preg_match('|filename="([^"]*?)"|si', $multipartStream, $matches), 'Could not find any filename in output.');
50+
$this->assertEquals('äa.png', $matches[1]);
51+
52+
// Reset the locale
53+
setlocale(LC_ALL, $originalLocale);
54+
}
55+
3656
public function testHeaders()
3757
{
3858
$builder = new MultipartStreamBuilder();

0 commit comments

Comments
 (0)