BindRightTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace React\Partial;
  3. class BindRightTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function testBindWithNoArgs()
  6. {
  7. $div = $this->createDivFunction();
  8. $newDiv = bind_right($div);
  9. $this->assertSame(2, $newDiv(4, 2));
  10. }
  11. public function testBindWithOneArg()
  12. {
  13. $div = $this->createDivFunction();
  14. $divOne = bind_right($div, 4);
  15. $this->assertSame(0.5, $divOne(2));
  16. }
  17. public function testBindWithTwoArgs()
  18. {
  19. $div = $this->createDivFunction();
  20. $divTwo = bind_right($div, 2, 4);
  21. $this->assertSame(0.5, $divTwo());
  22. }
  23. public function testBindWithPlaceholder()
  24. {
  25. $div = $this->createDivFunction();
  26. $divFun = bind_right($div, …(), 4);
  27. $this->assertSame(5, $divFun(20));
  28. $this->assertSame(10, $divFun(40));
  29. }
  30. public function testBindWithMultiplePlaceholders()
  31. {
  32. $div = $this->createDivFunction();
  33. $divTwo = bind_right($div, …(), 2, …());
  34. $this->assertSame(1, $divTwo(4, 2));
  35. $this->assertSame(1, $divTwo(10, 5));
  36. $this->assertSame(25, $divTwo(100, 2));
  37. }
  38. public function testPlaceholderParameterPosition()
  39. {
  40. $substr = bind_right('substr', …(), 0, …());
  41. $this->assertSame('foo', $substr('foo', 3));
  42. $this->assertSame('fo', $substr('foo', 2));
  43. $this->assertSame('f', $substr('foo', 1));
  44. }
  45. /**
  46. * @expectedException InvalidArgumentException
  47. * @expectedExceptionMessage Cannot resolve parameter placeholder at position 0. Parameter stack is empty
  48. */
  49. public function testStringConversion()
  50. {
  51. $div = $this->createDivFunction();
  52. $divTwo = bind_right($div, …(), 2);
  53. $divTwo();
  54. }
  55. public function testAliasForUnicodePlaceholderFunction()
  56. {
  57. $this->assertSame(…(), placeholder());
  58. }
  59. private function createDivFunction()
  60. {
  61. return function () {
  62. $args = func_get_args();
  63. $value = array_shift($args);
  64. foreach ($args as $arg) {
  65. $value /= $arg;
  66. }
  67. return $value;
  68. };
  69. }
  70. }