ParamBagTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php namespace League\Fractal\Test;
  2. use League\Fractal\ParamBag;
  3. use PHPUnit\Framework\TestCase;
  4. class ParamBagTest extends TestCase
  5. {
  6. public function testOldFashionedGet()
  7. {
  8. $params = new ParamBag(['one' => 'potato', 'two' => 'potato2']);
  9. $this->assertSame('potato', $params->get('one'));
  10. $this->assertSame('potato2', $params->get('two'));
  11. }
  12. public function testGettingValuesTheOldFashionedWayArray()
  13. {
  14. $params = new ParamBag(['one' => ['potato', 'tomato']]);
  15. $this->assertSame(['potato', 'tomato'], $params->get('one'));
  16. }
  17. public function testArrayAccess()
  18. {
  19. $params = new ParamBag(['foo' => 'bar', 'baz' => 'ban']);
  20. $this->assertInstanceOf('ArrayAccess', $params);
  21. $this->assertArrayHasKey('foo', $params);
  22. $this->assertTrue(isset($params['foo']));
  23. $this->assertSame('bar', $params['foo']);
  24. $this->assertSame('ban', $params['baz']);
  25. $this->assertNull($params['totallymadeup']);
  26. }
  27. /**
  28. * @expectedException \LogicException
  29. * @expectedExceptionMessage Modifying parameters is not permitted
  30. */
  31. public function testArrayAccessSetFails()
  32. {
  33. $params = new ParamBag(['foo' => 'bar']);
  34. $params['foo'] = 'someothervalue';
  35. }
  36. /**
  37. * @expectedException \LogicException
  38. * @expectedExceptionMessage Modifying parameters is not permitted
  39. */
  40. public function testArrayAccessUnsetFails()
  41. {
  42. $params = new ParamBag(['foo' => 'bar']);
  43. unset($params['foo']);
  44. }
  45. public function testObjectAccess()
  46. {
  47. $params = new ParamBag(['foo' => 'bar', 'baz' => 'ban']);
  48. $this->assertSame('bar', $params->foo);
  49. $this->assertSame('ban', $params->baz);
  50. $this->assertNull($params->totallymadeup);
  51. $this->assertTrue(isset($params->foo));
  52. }
  53. /**
  54. * @expectedException \LogicException
  55. * @expectedExceptionMessage Modifying parameters is not permitted
  56. */
  57. public function testObjectAccessSetFails()
  58. {
  59. $params = new ParamBag(['foo' => 'bar']);
  60. $params->foo = 'someothervalue';
  61. }
  62. /**
  63. * @expectedException \LogicException
  64. * @expectedExceptionMessage Modifying parameters is not permitted
  65. */
  66. public function testObjectAccessUnsetFails()
  67. {
  68. $params = new ParamBag(['foo' => 'bar']);
  69. unset($params->foo);
  70. }
  71. }