Pest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use Laravel\SerializableClosure\SerializableClosure;
  3. use Laravel\SerializableClosure\Serializers;
  4. use Laravel\SerializableClosure\Support\ReflectionClosure;
  5. use Laravel\SerializableClosure\UnsignedSerializableClosure;
  6. /*
  7. |--------------------------------------------------------------------------
  8. | Test Case
  9. |--------------------------------------------------------------------------
  10. |
  11. | The closure you provide to your test functions is always bound to a specific PHPUnit test
  12. | case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
  13. | need to change it using the "uses()" function to bind a different classes or traits.
  14. |
  15. */
  16. uses()->afterEach(function () {
  17. $_ENV['SERIALIZER'] = null;
  18. SerializableClosure::setSecretKey(null);
  19. SerializableClosure::transformUseVariablesUsing(null);
  20. SerializableClosure::resolveUseVariablesUsing(null);
  21. })->in(__DIR__);
  22. /*
  23. |--------------------------------------------------------------------------
  24. | Expectations
  25. |--------------------------------------------------------------------------
  26. |
  27. | When you're writing tests, you often need to check that values meet certain conditions. The
  28. | "expect()" function gives you access to a set of "expectations" methods that you can use
  29. | to assert different things. Of course, you may extend the Expectation API at any time.
  30. |
  31. */
  32. expect()->extend('toBeCode', function ($expected) {
  33. $code = (new ReflectionClosure($this->value))->getCode();
  34. expect($code)->toBe($expected);
  35. return $this->value;
  36. });
  37. /*
  38. |--------------------------------------------------------------------------
  39. | Functions
  40. |--------------------------------------------------------------------------
  41. |
  42. | While Pest is very powerful out-of-the-box, you may have some testing code specific to your
  43. | project that you don't want to repeat in every file. Here you can also expose helpers as
  44. | global functions to help you to reduce the number of lines of code in your test files.
  45. |
  46. */
  47. /**
  48. * Returns the given closure after serialize/unserialize.
  49. *
  50. * @param \Closure $closure
  51. * @return \Closure
  52. */
  53. function s($closure)
  54. {
  55. switch (test()->serializer) {
  56. case Serializers\Native::class:
  57. $closure = new SerializableClosure($closure);
  58. break;
  59. case Serializers\Signed::class:
  60. SerializableClosure::setSecretKey('secret');
  61. $closure = new SerializableClosure($closure);
  62. break;
  63. case UnsignedSerializableClosure::class:
  64. $closure = SerializableClosure::unsigned($closure);
  65. break;
  66. default:
  67. throw new Exception('Please use the [serializers] dataset.');
  68. }
  69. return unserialize(serialize($closure))->getClosure();
  70. }