NumberHelperTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * NumberHelperTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\View\Helper\NumberHelper;
  22. use Cake\View\View;
  23. /**
  24. * NumberHelperTestObject class
  25. */
  26. class NumberHelperTestObject extends NumberHelper
  27. {
  28. public function attach(NumberMock $cakeNumber)
  29. {
  30. $this->_engine = $cakeNumber;
  31. }
  32. public function engine()
  33. {
  34. return $this->_engine;
  35. }
  36. }
  37. /**
  38. * NumberMock class
  39. */
  40. class NumberMock
  41. {
  42. }
  43. /**
  44. * NumberHelperTest class
  45. */
  46. class NumberHelperTest extends TestCase
  47. {
  48. /**
  49. * setUp method
  50. *
  51. * @return void
  52. */
  53. public function setUp()
  54. {
  55. parent::setUp();
  56. $this->View = new View();
  57. $this->_appNamespace = Configure::read('App.namespace');
  58. static::setAppNamespace();
  59. }
  60. /**
  61. * tearDown method
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. parent::tearDown();
  68. static::setAppNamespace($this->_appNamespace);
  69. unset($this->View);
  70. }
  71. /**
  72. * Provider for method proxying.
  73. *
  74. * @return array
  75. */
  76. public function methodProvider()
  77. {
  78. return [
  79. ['precision'],
  80. ['toReadableSize'],
  81. ['toPercentage'],
  82. ['currency'],
  83. ['format'],
  84. ['addFormat'],
  85. ['formatDelta'],
  86. ['defaultCurrency'],
  87. ['ordinal'],
  88. ];
  89. }
  90. /**
  91. * test CakeNumber class methods are called correctly
  92. *
  93. * @dataProvider methodProvider
  94. * @return void
  95. */
  96. public function testNumberHelperProxyMethodCalls($method)
  97. {
  98. $number = $this->getMockBuilder(__NAMESPACE__ . '\NumberMock')
  99. ->setMethods([$method])
  100. ->getMock();
  101. $helper = new NumberHelperTestObject($this->View, ['engine' => __NAMESPACE__ . '\NumberMock']);
  102. $helper->attach($number);
  103. $number->expects($this->at(0))
  104. ->method($method)
  105. ->with(12.3);
  106. $helper->{$method}(12.3, ['options']);
  107. }
  108. /**
  109. * test engine override
  110. *
  111. * @return void
  112. */
  113. public function testEngineOverride()
  114. {
  115. $Number = new NumberHelperTestObject($this->View, ['engine' => 'TestAppEngine']);
  116. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Number->engine());
  117. Plugin::load('TestPlugin');
  118. $Number = new NumberHelperTestObject($this->View, ['engine' => 'TestPlugin.TestPluginEngine']);
  119. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Number->engine());
  120. Plugin::unload('TestPlugin');
  121. }
  122. }