CallableLeftCurryTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('GNUSOCIAL')) {
  21. define('GNUSOCIAL', true);
  22. }
  23. use PHPUnit\Framework\TestCase;
  24. require_once INSTALLDIR . '/lib/util/callableleftcurry.php';
  25. final class CallableLeftCurryTest extends TestCase
  26. {
  27. /**
  28. * @dataProvider provider
  29. *
  30. * @param $callback_test
  31. * @param $curry_params
  32. * @param $call_params
  33. * @param $expected
  34. */
  35. public function testCallableLeftCurry($callback_test, $curry_params, $call_params, $expected)
  36. {
  37. $params = array_merge([$callback_test], $curry_params);
  38. $curried = call_user_func_array('callableLeftCurry', $params);
  39. $result = call_user_func_array($curried, $call_params);
  40. static::assertSame($expected, $result);
  41. }
  42. public static function provider()
  43. {
  44. $obj = new CurryTestHelperObj('oldval');
  45. return [[['Tests\Unit\CallableLeftCurryTest', 'callback_test'],
  46. ['curried'],
  47. ['called'],
  48. 'called|curried',],
  49. [['Tests\Unit\CallableLeftCurryTest', 'callback_test'],
  50. ['curried1', 'curried2'],
  51. ['called1', 'called2'],
  52. 'called1|called2|curried1|curried2',],
  53. [['Tests\Unit\CallableLeftCurryTest', 'callback_testObj'],
  54. [$obj],
  55. ['newval1'],
  56. 'oldval|newval1',],
  57. // Confirm object identity is retained...
  58. [['Tests\Unit\CallableLeftCurryTest', 'callback_testObj'],
  59. [$obj],
  60. ['newval2'],
  61. 'newval1|newval2',],];
  62. }
  63. public static function callback_test()
  64. {
  65. $args = func_get_args();
  66. return implode('|', $args);
  67. }
  68. public static function callback_testObj($val, $obj)
  69. {
  70. $old = $obj->val;
  71. $obj->val = $val;
  72. return "{$old}|{$val}";
  73. }
  74. }
  75. class CurryTestHelperObj
  76. {
  77. public $val = '';
  78. public function __construct($val)
  79. {
  80. $this->val = $val;
  81. }
  82. }