CallableLeftCurryTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/callableleftcurry.php";
  25. final class CallableLeftCurryTest extends TestCase
  26. {
  27. /**
  28. * @dataProvider provider
  29. * @param $callback_test
  30. * @param $curry_params
  31. * @param $call_params
  32. * @param $expected
  33. */
  34. public function testCallableLeftCurry($callback_test, $curry_params, $call_params, $expected)
  35. {
  36. $params = array_merge([$callback_test], $curry_params);
  37. $curried = call_user_func_array('callableLeftCurry', $params);
  38. $result = call_user_func_array($curried, $call_params);
  39. $this->assertEquals($expected, $result);
  40. }
  41. static public function provider()
  42. {
  43. $obj = new CurryTestHelperObj('oldval');
  44. return [[['Tests\Unit\CallableLeftCurryTest', 'callback_test'],
  45. ['curried'],
  46. ['called'],
  47. 'called|curried'],
  48. [['Tests\Unit\CallableLeftCurryTest', 'callback_test'],
  49. ['curried1', 'curried2'],
  50. ['called1', 'called2'],
  51. 'called1|called2|curried1|curried2'],
  52. [['Tests\Unit\CallableLeftCurryTest', 'callback_testObj'],
  53. [$obj],
  54. ['newval1'],
  55. 'oldval|newval1'],
  56. // Confirm object identity is retained...
  57. [['Tests\Unit\CallableLeftCurryTest', 'callback_testObj'],
  58. [$obj],
  59. ['newval2'],
  60. 'newval1|newval2']];
  61. }
  62. static function callback_test()
  63. {
  64. $args = func_get_args();
  65. return implode("|", $args);
  66. }
  67. static function callback_testObj($val, $obj)
  68. {
  69. $old = $obj->val;
  70. $obj->val = $val;
  71. return "$old|$val";
  72. }
  73. }
  74. class CurryTestHelperObj
  75. {
  76. public $val = '';
  77. function __construct($val)
  78. {
  79. $this->val = $val;
  80. }
  81. }