AllergiesTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * By adding type hints and enabling strict type checking, code can become
  4. * easier to read, self-documenting and reduce the number of potential bugs.
  5. * By default, type declarations are non-strict, which means they will attempt
  6. * to change the original type to match the type specified by the
  7. * type-declaration.
  8. *
  9. * In other words, if you pass a string to a function requiring a float,
  10. * it will attempt to convert the string value to a float.
  11. *
  12. * To enable strict mode, a single declare directive must be placed at the top
  13. * of the file.
  14. * This means that the strictness of typing is configured on a per-file basis.
  15. * This directive not only affects the type declarations of parameters, but also
  16. * a function's return type.
  17. *
  18. * For more info review the Concept on strict type checking in the PHP track
  19. * <link>.
  20. *
  21. * To disable strict typing, comment out the directive below.
  22. */
  23. declare(strict_types=1);
  24. class AllergiesTest extends PHPUnit\Framework\TestCase
  25. {
  26. public static function setUpBeforeClass(): void
  27. {
  28. require_once 'Allergies.php';
  29. }
  30. /**
  31. * @dataProvider provideListOfAllergen
  32. *
  33. * @param Allergen $allergen
  34. */
  35. public function testNoAllergiesMeansNotAllergicToAnything($allergen): void
  36. {
  37. $allergies = new Allergies(0);
  38. $this->assertFalse($allergies->isAllergicTo($allergen));
  39. }
  40. /**
  41. * @dataProvider provideListOfAllergen
  42. *
  43. * @param Allergen $allergicTo
  44. */
  45. public function testAllergiesToOneAllergen($allergicTo): void
  46. {
  47. $allergies = new Allergies($allergicTo->getScore());
  48. $this->assertTrue($allergies->isAllergicTo($allergicTo));
  49. $otherAllergen = array_filter(Allergen::allergenList(), function ($allergen) use ($allergicTo) {
  50. return $allergen != $allergicTo;
  51. });
  52. array_map(function ($allergen) use ($allergies) {
  53. $this->assertFalse($allergies->isAllergicTo($allergen));
  54. }, $otherAllergen);
  55. }
  56. public static function provideListOfAllergen(): array
  57. {
  58. require_once 'Allergies.php';
  59. return [
  60. [new Allergen(Allergen::CATS), 'Only allergic to cats'],
  61. [new Allergen(Allergen::CHOCOLATE), 'Only allergic to chocolate'],
  62. [new Allergen(Allergen::EGGS), 'Only allergic to eggs'],
  63. [new Allergen(Allergen::PEANUTS), 'Only allergic to peanuts'],
  64. [new Allergen(Allergen::POLLEN), 'Only allergic to pollen'],
  65. [new Allergen(Allergen::SHELLFISH), 'Only allergic to shellfish'],
  66. [new Allergen(Allergen::STRAWBERRIES), 'Only allergic to strawberries'],
  67. [new Allergen(Allergen::TOMATOES), 'Only allergic to tomatoes'],
  68. ];
  69. }
  70. public function testAllergicToEggsInAdditionToOtherStuff(): void
  71. {
  72. $allergies = new Allergies(5);
  73. $this->assertTrue($allergies->isAllergicTo(new Allergen(Allergen::EGGS)));
  74. }
  75. public function testIsAllergicToLotsOfStuffs(): void
  76. {
  77. $allergies = new Allergies(248);
  78. $this->assertEqualsCanonicalizing([
  79. new Allergen(Allergen::CATS),
  80. new Allergen(Allergen::CHOCOLATE),
  81. new Allergen(Allergen::POLLEN),
  82. new Allergen(Allergen::STRAWBERRIES),
  83. new Allergen(Allergen::TOMATOES),
  84. ], $allergies->getList());
  85. }
  86. public function testIsAllergicToEggsAndPeanuts(): void
  87. {
  88. $allergies = new Allergies(3);
  89. $this->assertEqualsCanonicalizing([
  90. new Allergen(Allergen::EGGS),
  91. new Allergen(Allergen::PEANUTS),
  92. ], $allergies->getList());
  93. }
  94. public function testIsAllergicToEggsAndShellfish(): void
  95. {
  96. $allergies = new Allergies(5);
  97. $this->assertEqualsCanonicalizing([
  98. new Allergen(Allergen::EGGS),
  99. new Allergen(Allergen::SHELLFISH),
  100. ], $allergies->getList());
  101. }
  102. public function testIgnoreNonAllergenScorePart(): void
  103. {
  104. $allergies = new Allergies(509);
  105. $this->assertEqualsCanonicalizing([
  106. new Allergen(Allergen::CATS),
  107. new Allergen(Allergen::CHOCOLATE),
  108. new Allergen(Allergen::EGGS),
  109. new Allergen(Allergen::POLLEN),
  110. new Allergen(Allergen::SHELLFISH),
  111. new Allergen(Allergen::STRAWBERRIES),
  112. new Allergen(Allergen::TOMATOES),
  113. ], $allergies->getList());
  114. }
  115. /**
  116. * @dataProvider provideListOfAllergen
  117. */
  118. public function testIsAllergicToEverything($allergen): void
  119. {
  120. $allergies = new Allergies(255);
  121. $this->assertTrue($allergies->isAllergicTo($allergen));
  122. }
  123. }