Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Ensure progressbar tests can run
Browse files Browse the repository at this point in the history
Since zend-progressbar is updated, it was time to test if the
progressbar tests could run. The first hurdle was getting extensions
installed; the second was discovering the tests as written could not
work.

To fix that, I separated them into discrete APC and UploadProgress
tests. From there, I attempted to install the extensions and run each.

As it turns out APC is uninstallable starting in PHP 5.5, so the APC
tests cannot run regardless. I decided not to remove the support for
now, on the off chance that people have figured out a workaround.

The UploadProgress tests, however, required changes to the mock adapter
to allow forcing error situations to occur. I can now verify they
actually run and pass.
  • Loading branch information
weierophinney committed Mar 2, 2016
1 parent 4c0fbc7 commit 91ed961
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ before_install:
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$ZEND_SERVICEMANAGER_VERSION" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-progressbar ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"require-dev": {
"zendframework/zend-filter": "^2.6.1",
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-progressbar": "^2.5.2",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-session": "^2.6.2",
"zendframework/zend-validator": "^2.6",
"zendframework/zend-progressbar": "~2.5",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
Expand Down
12 changes: 6 additions & 6 deletions src/Transfer/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function isUploaded($files = null)
*/
public static function getProgress($id = null)
{
if (!static::isApcAvailable() && !static::isUploadProgressAvailable()) {
if (!self::isApcAvailable() && !self::isUploadProgressAvailable()) {
throw new Exception\PhpEnvironmentException('Neither APC nor UploadProgress extension installed');
}

Expand Down Expand Up @@ -332,18 +332,18 @@ public static function getProgress($id = null)
}

if (!empty($id)) {
if (static::isApcAvailable()) {
if (self::isApcAvailable()) {
$call = call_user_func(static::$callbackApc, ini_get('apc.rfc1867_prefix') . $id);
if (is_array($call)) {
$status = $call + $status;
}
} elseif (static::isUploadProgressAvailable()) {
} elseif (self::isUploadProgressAvailable()) {
$call = call_user_func(static::$callbackUploadProgress, $id);
if (is_array($call)) {
$status = $call + $status;
$status['total'] = $status['bytes_total'];
$status['current'] = $status['bytes_uploaded'];
$status['rate'] = $status['speed_average'];
$status['total'] = isset($status['bytes_total']) ? $status['bytes_total'] : $status['total'];
$status['current'] = isset($status['bytes_uploaded']) ? $status['bytes_uploaded'] : $status['current'];
$status['rate'] = isset($status['speed_average']) ? $status['speed_average'] : $status['rate'];
if ($status['total'] == $status['current']) {
$status['done'] = true;
}
Expand Down
34 changes: 27 additions & 7 deletions test/Transfer/Adapter/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ public function testNoUploadInProgress()
$this->assertContains('No upload in progress', $status);
}

public function testUploadProgressFailure()
public function testUploadProgressFailureForAPC()
{
if (!Adapter\Http::isApcAvailable() && !Adapter\Http::isUploadProgressAvailable()) {
$this->markTestSkipped('Whether APC nor UploadExtension available');
if (! Adapter\Http::isApcAvailable()) {
$this->markTestSkipped('APC extension is unavailable');
}

$_GET['progress_key'] = 'mykey';
Expand All @@ -255,10 +255,30 @@ public function testUploadProgressFailure()
'rate' => 10,
'id' => 'mykey',
'done' => false,
'message' => '100B - 100B'
], $status);
'message' => '100B - 100B',
], $status);
}

public function testUploadProgressFailureForUploadProgressExtension()
{
if (! Adapter\Http::isUploadProgressAvailable()) {
$this->markTestSkipped('uploadprogress extension is unavailable');
}

$_GET['progress_key'] = 'mykey';
$this->adapter->switchApcToUP();

$status = HttpTestMockAdapter::getProgress();
$this->assertEquals([
'total' => 100,
'current' => 90,
'rate' => 10,
'id' => 'mykey',
'done' => false,
'message' => '90B - 100B',
], $status);

$this->adapter->forceUPFailure();
$status = HttpTestMockAdapter::getProgress($status);
$this->assertEquals([
'total' => 100,
Expand All @@ -270,8 +290,8 @@ public function testUploadProgressFailure()
'cancel_upload' => true,
'message' => 'The upload has been canceled',
'done' => true,
'id' => 'mykey'
], $status);
'id' => 'mykey',
], $status);
}

public function testUploadProgressAdapter()
Expand Down
37 changes: 31 additions & 6 deletions test/Transfer/Adapter/HttpTestMockAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
*/
class HttpTestMockAdapter extends Adapter\Http
{
private static $uploadProgressShouldFail;

public function __construct()
{
self::$callbackApc = ['ZendTest\File\Transfer\Adapter\HttpTestMockAdapter', 'apcTest'];
static::$callbackApc = [HttpTestMockAdapter::class, 'apcTest'];
self::$uploadProgressShouldFail = false;
parent::__construct();
}

Expand All @@ -41,15 +44,32 @@ public static function isApcAvailable()

public static function apcTest($id)
{
if (! is_array($id)) {
return [
'total' => 100,
'current' => 100,
'rate' => 10,
];
}

return [
'total' => 100,
'current' => 100,
'rate' => 10,
'bytes_total' => 100,
'bytes_uploaded' => 100,
'speed_average' => 10,
'cancel_upload' => true,
];
}

public static function uPTest($id)
{
if (! self::$uploadProgressShouldFail) {
return [
'total' => 100,
'current' => 90,
'rate' => 10,
];
}

return [
'bytes_total' => 100,
'bytes_uploaded' => 100,
Expand All @@ -60,7 +80,12 @@ public static function uPTest($id)

public function switchApcToUP()
{
self::$callbackApc = null;
self::$callbackUploadProgress = ['ZendTest\File\Transfer\Adapter\HttpTestMockAdapter', 'uPTest'];
static::$callbackApc = null;
static::$callbackUploadProgress = [HttpTestMockAdapter::class, 'uPTest'];
}

public function forceUPFailure()
{
self::$uploadProgressShouldFail = true;
}
}

0 comments on commit 91ed961

Please sign in to comment.