phpseclib/tests/PhpseclibTestCase.php

184 lines
5.9 KiB
PHP
Raw Normal View History

<?php
2022-02-17 02:25:59 +00:00
/**
* @author Andreas Fischer <bantu@phpbb.com>
* @copyright 2013 Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
namespace phpseclib3\Tests;
use PHPUnit\Framework\TestCase;
abstract class PhpseclibTestCase extends TestCase
{
2017-11-27 08:30:14 +00:00
protected $tempFilesToUnlinkOnTearDown = [];
public function tearDown()
{
foreach ($this->tempFilesToUnlinkOnTearDown as $filename) {
if (!file_exists($filename) || unlink($filename)) {
unset($this->tempFilesToUnlinkOnTearDown[$filename]);
}
}
parent::tearDown();
}
/**
2015-03-29 16:07:17 +00:00
* Creates a temporary file on the local filesystem and returns its path.
* The $number_of_writes and $bytes_per_write parameters can be used to
* write $number_of_writes * $bytes_per_write times the character 'a' to the
* temporary file. All files created using this method will be deleted from
* the filesystem on tearDown(), i.e. after each test method was run.
*
* @param int $number_of_writes
* @param int $bytes_per_write
*
* @return string
*/
protected function createTempFile($number_of_writes = 0, $bytes_per_write = 0)
{
$filename = tempnam(sys_get_temp_dir(), 'phpseclib-test-');
$this->assertTrue(file_exists($filename));
$this->tempFilesToUnlinkOnTearDown[] = $filename;
if ($number_of_writes > 0 && $bytes_per_write > 0) {
$fp = fopen($filename, 'wb');
for ($i = 0; $i < $number_of_writes; ++$i) {
fwrite($fp, str_repeat('a', $bytes_per_write));
}
fclose($fp);
$this->assertSame($number_of_writes * $bytes_per_write, filesize($filename));
}
return $filename;
}
/**
2015-03-29 16:07:17 +00:00
* @param string $constant
* @param mixed $expected
*
* @return null
*/
2015-07-15 01:52:31 +00:00
protected static function ensureConstant($constant, $expected)
{
if (defined($constant)) {
$value = constant($constant);
if ($value !== $expected) {
if (extension_loaded('runkit')) {
if (!runkit_constant_redefine($constant, $expected)) {
self::markTestSkipped(sprintf(
"Failed to redefine constant %s to %s",
$constant,
$expected
));
}
} else {
self::markTestSkipped(sprintf(
"Skipping test because constant %s is %s instead of %s",
$constant,
$value,
$expected
));
}
}
} else {
define($constant, $expected);
}
}
protected static function getVar($obj, $var)
{
$reflection = new \ReflectionClass(get_class($obj));
// private variables are not inherited, climb hierarchy until located
while (true) {
try {
$prop = $reflection->getProperty($var);
break;
} catch (\ReflectionException $e) {
$reflection = $reflection->getParentClass();
if (!$reflection) {
throw $e;
}
}
}
$prop->setAccessible(true);
return $prop->getValue($obj);
}
2017-01-08 01:51:56 +00:00
protected static function setVar($obj, $var, $value)
{
$reflection = new \ReflectionClass(get_class($obj));
// private variables are not inherited, climb hierarchy until located
while (true) {
try {
$prop = $reflection->getProperty($var);
break;
} catch (\ReflectionException $e) {
$reflection = $reflection->getParentClass();
if (!$reflection) {
throw $e;
}
}
}
$prop->setAccessible(true);
$prop->setValue($obj, $value);
}
2017-11-27 08:30:14 +00:00
public static function callFunc($obj, $func, $params = [])
2017-01-08 01:51:56 +00:00
{
$reflection = new \ReflectionClass(get_class($obj));
2017-01-08 01:51:56 +00:00
$method = $reflection->getMethod($func);
$method->setAccessible(true);
return $method->invokeArgs($obj, $params);
}
2020-12-13 01:22:36 +00:00
// assertIsArray was not introduced until PHPUnit 8
public static function assertIsArray($actual, $message = '')
{
parent::assertInternalType('array', $actual, $message);
}
// assertIsString was not introduced until PHPUnit 8
public static function assertIsString($actual, $message = '')
{
parent::assertInternalType('string', $actual, $message);
}
2020-12-13 01:22:36 +00:00
// assertIsResource was not introduced until PHPUnit 8
public static function assertIsResource($actual, $message = '')
{
parent::assertInternalType('resource', $actual, $message);
}
// assertIsObject was not introduced until PHPUnit 8
public static function assertIsObject($actual, $message = '')
{
parent::assertInternalType('object', $actual, $message);
}
// assertContains is deprecated for strings in PHPUnit 8
public static function assertStringContainsString($needle, $haystack, $message = '')
{
parent::assertContains($needle, $haystack, $message);
}
// assertNotContains is deprecated for strings in PHPUnit 8
public static function assertStringNotContainsString($needle, $haystack, $message = '')
{
parent::assertNotContains($needle, $haystack, $message);
}
2022-03-09 00:59:30 +00:00
/**
* assertRegExp() was deprecated in favor of assertMatchesRegularExpression().
*
* @param string $pattern
* @param string $string
* @param string $message
* @return void
*/
public static function assertMatchesRegularExpression($pattern, $string, $message = '')
{
2023-02-05 23:33:16 +00:00
parent::assertRegExp($pattern, $string, $message);
2022-03-09 00:59:30 +00:00
}
}