/tests/wpunit/functions/PriceTest.php

https://github.com/strangerstudios/paid-memberships-pro · PHP · 153 lines · 74 code · 23 blank · 56 comment · 0 complexity · 3a8e1bca3945d5a9b44caad7f9453fd1 MD5 · raw file

  1. <?php
  2. namespace PMPro\Tests\Functions;
  3. use PHP_CodeSniffer\Generators\HTML;
  4. use PMPro\Test_Support\TestCases\TestCase;
  5. /**
  6. * @group pmpro-functions
  7. */
  8. class PriceTest extends TestCase {
  9. protected $original_precision;
  10. protected $original_serialize_precision;
  11. /**
  12. *
  13. */
  14. public function setUp() : void {
  15. parent::setUp();
  16. $this->original_precision = ini_get( 'precision' );
  17. $this->original_serialize_precision = ini_get( 'serialize_precision' );
  18. ini_set( 'precision', 14 );
  19. ini_set( 'serialize_precision', 17 );
  20. }
  21. /**
  22. *
  23. */
  24. public function tearDown() : void {
  25. ini_set( 'precision', $this->original_precision );
  26. ini_set( 'serialize_precision', $this->original_serialize_precision );
  27. parent::tearDown();
  28. }
  29. /**
  30. * @covers ::pmpro_round_price()
  31. */
  32. public function test_precision_issue_with_float() {
  33. $this->assertEquals( '19.899999999999999', json_encode( 19.9 ) );
  34. $this->assertEquals( '19.890000000000001', json_encode( 19.89 ) );
  35. }
  36. /**
  37. * @covers ::pmpro_round_price()
  38. */
  39. public function test_pmpro_round_price_with_float_has_precision_issue() {
  40. $this->assertEquals( 19.9, pmpro_round_price( 19.9 ) );
  41. $this->assertEquals( '19.899999999999999', json_encode( pmpro_round_price( 19.9 ) ) );
  42. }
  43. /**
  44. * @covers ::pmpro_round_price_as_string()
  45. * @covers ::pmpro_get_price_info()
  46. * @covers ::pmpro_round_price()
  47. */
  48. public function test_pmpro_round_price_as_string_with_non_numeric_amount() {
  49. $this->assertEquals( '0', pmpro_round_price_as_string( 'non-numeric' ) );
  50. }
  51. /**
  52. * @covers ::pmpro_round_price_as_string()
  53. * @covers ::pmpro_get_price_info()
  54. * @covers ::pmpro_round_price()
  55. */
  56. public function test_pmpro_round_price_as_string_with_integer() {
  57. $this->assertEquals( '5.00', pmpro_round_price_as_string( 5 ) );
  58. $this->assertEquals( '"5.00"', json_encode( pmpro_round_price_as_string( 5 ) ) );
  59. }
  60. /**
  61. * @covers ::pmpro_round_price_as_string()
  62. * @covers ::pmpro_get_price_info()
  63. * @covers ::pmpro_round_price()
  64. */
  65. public function test_pmpro_round_price_as_string_with_float() {
  66. $this->assertEquals( '19.90', pmpro_round_price_as_string( 19.9 ) );
  67. $this->assertEquals( '"19.90"', json_encode( pmpro_round_price_as_string( 19.9 ) ) );
  68. }
  69. /**
  70. * @covers ::pmpro_get_price_info()
  71. * @covers ::pmpro_round_price()
  72. */
  73. public function test_pmpro_get_price_info_with_non_numeric_amount() {
  74. $this->assertFalse( pmpro_get_price_info( 'non-numeric' ) );
  75. }
  76. /**
  77. * @covers ::pmpro_get_price_info()
  78. * @covers ::pmpro_round_price()
  79. */
  80. public function test_pmpro_get_price_info_with_integer() {
  81. $currency_info = pmpro_get_currency();
  82. $expected = [
  83. // The amount represented as a float.
  84. 'amount' => 5,
  85. // The flat amount represent (example: 1.99 would be 199).
  86. 'amount_flat' => 500,
  87. // The amount as a string.
  88. 'amount_string' => '5.00',
  89. 'parts' => [
  90. // The whole number part of the amount (example: 1.99 would be 1).
  91. 'number' => 5,
  92. // The decimal part of the amount (example: 1.99 would be 99, 1.00 would be 0).
  93. 'decimal' => 0,
  94. // The decimal part of the amount as a string (example: 1.99 would be 99, 1.00 would be 00).
  95. 'decimal_string' => '00',
  96. ],
  97. // The currency information.
  98. 'currency' => $currency_info,
  99. ];
  100. $actual = pmpro_get_price_info( 5 );
  101. $this->assertEquals( $expected, $actual );
  102. }
  103. /**
  104. * @covers ::pmpro_get_price_info()
  105. * @covers ::pmpro_round_price()
  106. */
  107. public function test_pmpro_get_price_info_with_float() {
  108. $currency_info = pmpro_get_currency();
  109. $expected = [
  110. // The amount represented as a float.
  111. 'amount' => 19.9,
  112. // The flat amount represent (example: 1.99 would be 199).
  113. 'amount_flat' => 1990,
  114. // The amount as a string.
  115. 'amount_string' => '19.90',
  116. 'parts' => [
  117. // The whole number part of the amount (example: 1.99 would be 1).
  118. 'number' => 19,
  119. // The decimal part of the amount (example: 1.99 would be 99, 1.00 would be 0).
  120. 'decimal' => 90,
  121. // The decimal part of the amount as a string (example: 1.99 would be 99, 1.00 would be 00).
  122. 'decimal_string' => '90',
  123. ],
  124. // The currency information.
  125. 'currency' => $currency_info,
  126. ];
  127. $actual = pmpro_get_price_info( 19.9 );
  128. $this->assertEquals( $expected, $actual );
  129. }
  130. }