BooleanUtilsTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\Test\Unit;
  7. use \Magento\Framework\Stdlib\BooleanUtils;
  8. class BooleanUtilsTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var BooleanUtils
  12. */
  13. protected $object;
  14. protected function setUp()
  15. {
  16. $this->object = new BooleanUtils();
  17. }
  18. public function testConstructor()
  19. {
  20. $object = new BooleanUtils(['yep'], ['nope']);
  21. $this->assertTrue($object->toBoolean('yep'));
  22. $this->assertFalse($object->toBoolean('nope'));
  23. }
  24. /**
  25. * @param mixed $input
  26. * @param bool $expected
  27. *
  28. * @dataProvider toBooleanDataProvider
  29. */
  30. public function testToBoolean($input, $expected)
  31. {
  32. $actual = $this->object->toBoolean($input);
  33. $this->assertSame($expected, $actual);
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function toBooleanDataProvider()
  39. {
  40. return [
  41. 'boolean "true"' => [true, true],
  42. 'boolean "false"' => [false, false],
  43. 'boolean string "true"' => ['true', true],
  44. 'boolean string "false"' => ['false', false],
  45. 'boolean numeric "1"' => [1, true],
  46. 'boolean numeric "0"' => [0, false],
  47. 'boolean string "1"' => ['1', true],
  48. 'boolean string "0"' => ['0', false]
  49. ];
  50. }
  51. /**
  52. * @param mixed $input
  53. *
  54. * @dataProvider toBooleanExceptionDataProvider
  55. * @expectedException \InvalidArgumentException
  56. * @expectedExceptionMessage Boolean value is expected
  57. */
  58. public function testToBooleanException($input)
  59. {
  60. $this->object->toBoolean($input);
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function toBooleanExceptionDataProvider()
  66. {
  67. return [
  68. 'boolean string "on"' => ['on'],
  69. 'boolean string "off"' => ['off'],
  70. 'boolean string "yes"' => ['yes'],
  71. 'boolean string "no"' => ['no'],
  72. 'boolean string "TRUE"' => ['TRUE'],
  73. 'boolean string "FALSE"' => ['FALSE'],
  74. 'empty string' => [''],
  75. 'null' => [null]
  76. ];
  77. }
  78. }