WeekNumTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\DateTime;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. class WeekNumTest extends AllSetupTeardown
  5. {
  6. /**
  7. * @dataProvider providerWEEKNUM
  8. *
  9. * @param mixed $expectedResult
  10. */
  11. public function testWEEKNUM($expectedResult, string $formula): void
  12. {
  13. $this->mightHaveException($expectedResult);
  14. $sheet = $this->getSheet();
  15. $sheet->getCell('B1')->setValue('1954-11-23');
  16. $sheet->getCell('A1')->setValue("=WEEKNUM($formula)");
  17. self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  18. }
  19. public function providerWEEKNUM(): array
  20. {
  21. return require 'tests/data/Calculation/DateTime/WEEKNUM.php';
  22. }
  23. /**
  24. * @dataProvider providerWEEKNUM1904
  25. *
  26. * @param mixed $expectedResult
  27. */
  28. public function testWEEKNUM1904($expectedResult, string $formula): void
  29. {
  30. $this->mightHaveException($expectedResult);
  31. self::setMac1904();
  32. $sheet = $this->getSheet();
  33. $sheet->getCell('B1')->setValue('1954-11-23');
  34. $sheet->getCell('A1')->setValue("=WEEKNUM($formula)");
  35. self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue());
  36. }
  37. public function providerWEEKNUM1904(): array
  38. {
  39. return require 'tests/data/Calculation/DateTime/WEEKNUM1904.php';
  40. }
  41. /**
  42. * @dataProvider providerWeekNumArray
  43. */
  44. public function testWeekNumArray(array $expectedResult, string $dateValues, string $methods): void
  45. {
  46. $calculation = Calculation::getInstance();
  47. $formula = "=WEEKNUM({$dateValues}, {$methods})";
  48. $result = $calculation->_calculateFormulaValue($formula);
  49. self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
  50. }
  51. public function providerWeekNumArray(): array
  52. {
  53. return [
  54. 'row vector #1' => [[[1, 25, 29]], '{"2022-01-01", "2022-06-12", "2023-07-22"}', '1'],
  55. 'column vector #1' => [[[1], [13], [26]], '{"2023-01-01"; "2023-04-01"; "2023-07-01"}', '1'],
  56. 'matrix #1' => [[[1, 53], [1, 53]], '{"2021-01-01", "2021-12-31"; "2023-01-01", "2023-12-31"}', '1'],
  57. 'row vector #2' => [[[25, 24]], '"2022-06-12"', '{1, 2}'],
  58. 'column vector #2' => [[[13], [14]], '"2023-04-01"', '{1; 2}'],
  59. 'matrix #2' => [[[53, 53], [53, 52]], '"2021-12-31"', '{1, 2; 16, 21}'],
  60. ];
  61. }
  62. }