ProteinTranslationTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ProteinTranslationTest extends PHPUnit\Framework\TestCase
  25. {
  26. private ProteinTranslation $translater;
  27. public static function setUpBeforeClass(): void
  28. {
  29. require_once 'ProteinTranslation.php';
  30. }
  31. public function setUp(): void
  32. {
  33. $this->translater = new ProteinTranslation();
  34. }
  35. public function testEmptyRnaSequence(): void
  36. {
  37. $this->assertEquals([], $this->translater->getProteins(''));
  38. }
  39. public function testMethionineRnaSequence(): void
  40. {
  41. $this->assertEquals(['Methionine'], $this->translater->getProteins('AUG'));
  42. }
  43. public function testPhenylalanineRnaSequenceOne(): void
  44. {
  45. $this->assertEquals(['Phenylalanine'], $this->translater->getProteins('UUU'));
  46. }
  47. public function testPhenylalanineRnaSequenceTwo(): void
  48. {
  49. $this->assertEquals(['Phenylalanine'], $this->translater->getProteins('UUC'));
  50. }
  51. public function testLeucineRnaSequenceOne(): void
  52. {
  53. $this->assertEquals(['Leucine'], $this->translater->getProteins('UUA'));
  54. }
  55. public function testLeucineRnaSequenceTwo(): void
  56. {
  57. $this->assertEquals(['Leucine'], $this->translater->getProteins('UUG'));
  58. }
  59. public function testSerineRnaSequenceOne(): void
  60. {
  61. $this->assertEquals(['Serine'], $this->translater->getProteins('UCU'));
  62. }
  63. public function testSerineRnaSequenceTwo(): void
  64. {
  65. $this->assertEquals(['Serine'], $this->translater->getProteins('UCC'));
  66. }
  67. public function testSerineRnaSequenceThree(): void
  68. {
  69. $this->assertEquals(['Serine'], $this->translater->getProteins('UCA'));
  70. }
  71. public function testSerineRnaSequenceFour(): void
  72. {
  73. $this->assertEquals(['Serine'], $this->translater->getProteins('UCG'));
  74. }
  75. public function testTyrosineRnaSequenceOne(): void
  76. {
  77. $this->assertEquals(['Tyrosine'], $this->translater->getProteins('UAU'));
  78. }
  79. public function testTyrosineRnaSequenceTwo(): void
  80. {
  81. $this->assertEquals(['Tyrosine'], $this->translater->getProteins('UAC'));
  82. }
  83. public function testCysteineRnaSequenceOne(): void
  84. {
  85. $this->assertEquals(['Cysteine'], $this->translater->getProteins('UGU'));
  86. }
  87. public function testCysteineRnaSequenceTwo(): void
  88. {
  89. $this->assertEquals(['Cysteine'], $this->translater->getProteins('UGC'));
  90. }
  91. public function testTryptophanRnaSequence(): void
  92. {
  93. $this->assertEquals(['Tryptophan'], $this->translater->getProteins('UGG'));
  94. }
  95. public function testStopCodonRnaSequenceOne(): void
  96. {
  97. $this->assertEquals([], $this->translater->getProteins('UAA'));
  98. }
  99. public function testStopCodonRnaSequenceTwo(): void
  100. {
  101. $this->assertEquals([], $this->translater->getProteins('UAG'));
  102. }
  103. public function testStopCodonRnaSequenceThree(): void
  104. {
  105. $this->assertEquals([], $this->translater->getProteins('UGA'));
  106. }
  107. public function testToCodonsTranslateToProteins(): void
  108. {
  109. $this->assertEquals(['Phenylalanine', 'Phenylalanine'], $this->translater->getProteins('UUUUUU'));
  110. }
  111. public function testToDifferentCodonsTranslateToProteins(): void
  112. {
  113. $this->assertEquals(['Leucine', 'Leucine'], $this->translater->getProteins('UUAUUG'));
  114. }
  115. public function testTranslateRnaStrandToCorrectProteinList(): void
  116. {
  117. $this->assertEquals(
  118. ['Methionine', 'Phenylalanine', 'Tryptophan'],
  119. $this->translater->getProteins('AUGUUUUGG')
  120. );
  121. }
  122. public function testTranslationStopsIfStopCodonAtBeginningOfSequence(): void
  123. {
  124. $this->assertEquals([], $this->translater->getProteins('UAGUGG'));
  125. }
  126. public function testTranslationStopsIfStopCodonAtEndOfTwoCodonSequence(): void
  127. {
  128. $this->assertEquals(['Tryptophan'], $this->translater->getProteins('UGGUAG'));
  129. }
  130. public function testTranslationStopsIfStopCodonAtEndOfThreeCodonSequence(): void
  131. {
  132. $this->assertEquals(['Methionine', 'Phenylalanine'], $this->translater->getProteins('AUGUUUUAA'));
  133. }
  134. public function testTranslationStopsIfStopCodonInMiddleOfThreeCodonSequence(): void
  135. {
  136. $this->assertEquals(['Tryptophan'], $this->translater->getProteins('UGGUAGUGG'));
  137. }
  138. public function testTranslationStopsIfStopCodonInMiddleOfSixCodonSequence(): void
  139. {
  140. $this->assertEquals(
  141. ['Tryptophan', 'Cysteine', 'Tyrosine'],
  142. $this->translater->getProteins('UGGUGUUAUUAAUGGUUU')
  143. );
  144. }
  145. public function invalidCodonDataProvider(): array
  146. {
  147. return [
  148. 'Non-existing' => ['AAA'],
  149. 'Unknown' => ['XYZ'],
  150. 'Incomplete' => ['AUGU'],
  151. ];
  152. }
  153. /**
  154. * @dataProvider invalidCodonDataProvider
  155. */
  156. public function testTranslateFailsForInvalidCodons(string $rna): void
  157. {
  158. $this->expectException(InvalidArgumentException::class);
  159. $this->expectExceptionMessage('Invalid codon');
  160. $this->translater->getProteins($rna);
  161. }
  162. public function testTranslatePassesIfStopCodeBeforeIncompleteSequence(): void
  163. {
  164. $this->assertEquals(['Phenylalanine', 'Phenylalanine'], $this->translater->getProteins('UUCUUCUAAUGGU'));
  165. }
  166. }