gnu-social/tests/Core/CallableLeftCurryTest.php

93 lines
2.8 KiB
PHP
Raw Normal View History

2019-07-13 00:31:14 +09:00
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
namespace Tests\Unit;
if (!defined('INSTALLDIR')) {
define('INSTALLDIR', dirname(dirname(__DIR__)));
}
if (!defined('GNUSOCIAL')) {
define('GNUSOCIAL', true);
}
use PHPUnit\Framework\TestCase;
2019-08-23 21:36:02 +09:00
require_once INSTALLDIR . '/lib/util/callableleftcurry.php';
2019-07-13 00:31:14 +09:00
final class CallableLeftCurryTest extends TestCase
{
/**
* @dataProvider provider
2020-06-24 21:08:11 +09:00
*
2019-07-13 00:31:14 +09:00
* @param $callback_test
* @param $curry_params
* @param $call_params
* @param $expected
*/
public function testCallableLeftCurry($callback_test, $curry_params, $call_params, $expected)
{
$params = array_merge([$callback_test], $curry_params);
$curried = call_user_func_array('callableLeftCurry', $params);
$result = call_user_func_array($curried, $call_params);
2020-06-24 21:08:11 +09:00
static::assertSame($expected, $result);
2019-07-13 00:31:14 +09:00
}
2020-06-24 21:08:11 +09:00
public static function provider()
2019-07-13 00:31:14 +09:00
{
$obj = new CurryTestHelperObj('oldval');
return [[['Tests\Unit\CallableLeftCurryTest', 'callback_test'],
['curried'],
['called'],
2020-06-24 21:08:11 +09:00
'called|curried',],
2019-07-13 00:31:14 +09:00
[['Tests\Unit\CallableLeftCurryTest', 'callback_test'],
['curried1', 'curried2'],
['called1', 'called2'],
2020-06-24 21:08:11 +09:00
'called1|called2|curried1|curried2',],
2019-07-13 00:31:14 +09:00
[['Tests\Unit\CallableLeftCurryTest', 'callback_testObj'],
[$obj],
['newval1'],
2020-06-24 21:08:11 +09:00
'oldval|newval1',],
2019-07-13 00:31:14 +09:00
// Confirm object identity is retained...
[['Tests\Unit\CallableLeftCurryTest', 'callback_testObj'],
[$obj],
['newval2'],
2020-06-24 21:08:11 +09:00
'newval1|newval2',],];
2019-07-13 00:31:14 +09:00
}
2020-06-24 21:08:11 +09:00
public static function callback_test()
2019-07-13 00:31:14 +09:00
{
$args = func_get_args();
2020-06-24 21:08:11 +09:00
return implode('|', $args);
2019-07-13 00:31:14 +09:00
}
2020-06-24 21:08:11 +09:00
public static function callback_testObj($val, $obj)
2019-07-13 00:31:14 +09:00
{
$old = $obj->val;
$obj->val = $val;
2020-06-24 21:08:11 +09:00
return "{$old}|{$val}";
2019-07-13 00:31:14 +09:00
}
}
class CurryTestHelperObj
{
public $val = '';
2020-06-24 21:08:11 +09:00
public function __construct($val)
2019-07-13 00:31:14 +09:00
{
$this->val = $val;
}
}