BindTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace React\Partial;
  3. class BindTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function testBindWithNoArgs()
  6. {
  7. $add = $this->createAddFunction();
  8. $newAdd = bind($add);
  9. $this->assertSame(6, $newAdd(1, 5));
  10. }
  11. public function testBindWithOneArg()
  12. {
  13. $add = $this->createAddFunction();
  14. $addOne = bind($add, 1);
  15. $this->assertSame(6, $addOne(5));
  16. }
  17. public function testBindWithTwoArgs()
  18. {
  19. $add = $this->createAddFunction();
  20. $addOneAndFive = bind($add, 1, 5);
  21. $this->assertSame(6, $addOneAndFive());
  22. }
  23. public function testBindWithPlaceholder()
  24. {
  25. $add = $this->createAddFunction();
  26. $addFun = bind($add, …(), 10);
  27. $this->assertSame(20, $addFun(10));
  28. $this->assertSame(30, $addFun(20));
  29. }
  30. public function testBindWithMultiplePlaceholders()
  31. {
  32. $prod = $this->createProdFunction();
  33. $prodTwo = bind($prod, …(), 2, …());
  34. $this->assertSame(4, $prodTwo(1, 2));
  35. $this->assertSame(6, $prodTwo(1, 3));
  36. $this->assertSame(8, $prodTwo(2, 2));
  37. $this->assertSame(24, $prodTwo(3, 4));
  38. $this->assertSame(48, $prodTwo(3, 8));
  39. }
  40. public function testPlaceholderParameterPosition()
  41. {
  42. $substr = bind('substr', …(), 0, …());
  43. $this->assertSame('foo', $substr('foo', 3));
  44. $this->assertSame('fo', $substr('foo', 2));
  45. $this->assertSame('f', $substr('foo', 1));
  46. }
  47. /**
  48. * @expectedException InvalidArgumentException
  49. * @expectedExceptionMessage Cannot resolve parameter placeholder at position 0. Parameter stack is empty
  50. */
  51. public function testStringConversion()
  52. {
  53. $add = $this->createAddFunction();
  54. $addTwo = bind($add, …(), 2);
  55. $addTwo();
  56. }
  57. public function testAliasForUnicodePlaceholderFunction()
  58. {
  59. $this->assertSame(…(), placeholder());
  60. }
  61. private function createAddFunction()
  62. {
  63. return function ($a, $b) {
  64. return $a + $b;
  65. };
  66. }
  67. private function createProdFunction()
  68. {
  69. return function ($a, $b, $c) {
  70. return $a * $b * $c;
  71. };
  72. }
  73. }