Arrays.wrap().phpt 907 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Arrays::wrap()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Arrays;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. test('Basics', function () {
  10. Assert::same([], Arrays::wrap([], '{', '}'));
  11. Assert::same(['STR'], Arrays::wrap(['STR']));
  12. Assert::same(['{STR'], Arrays::wrap(['STR'], '{'));
  13. Assert::same(['STR}'], Arrays::wrap(['STR'], '', '}'));
  14. Assert::same(['{STR}'], Arrays::wrap(['STR'], '{', '}'));
  15. });
  16. test('Type conversion', function () {
  17. $o = new class {
  18. public function __toString()
  19. {
  20. return 'toString';
  21. }
  22. };
  23. $cases = [
  24. [0, '0'],
  25. [1.1, '1.1'],
  26. [null, ''],
  27. [false, ''],
  28. [true, '1'],
  29. [$o, 'toString'],
  30. ];
  31. Assert::same(array_column($cases, 1), Arrays::wrap(array_column($cases, 0)));
  32. Assert::error(
  33. fn() => Arrays::wrap([[]]),
  34. PHP_VERSION_ID < 80000 ? E_NOTICE : E_WARNING,
  35. 'Array to string conversion',
  36. );
  37. });