| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | <?php/* * By adding type hints and enabling strict type checking, code can become * easier to read, self-documenting and reduce the number of potential bugs. * By default, type declarations are non-strict, which means they will attempt * to change the original type to match the type specified by the * type-declaration. * * In other words, if you pass a string to a function requiring a float, * it will attempt to convert the string value to a float. * * To enable strict mode, a single declare directive must be placed at the top * of the file. * This means that the strictness of typing is configured on a per-file basis. * This directive not only affects the type declarations of parameters, but also * a function's return type. * * For more info review the Concept on strict type checking in the PHP track * <link>. * * To disable strict typing, comment out the directive below. */declare(strict_types=1);class ProteinTranslationTest extends PHPUnit\Framework\TestCase{    private ProteinTranslation $translater;    public static function setUpBeforeClass(): void    {        require_once 'ProteinTranslation.php';    }    public function setUp(): void    {        $this->translater = new ProteinTranslation();    }    public function testEmptyRnaSequence(): void    {        $this->assertEquals([], $this->translater->getProteins(''));    }    public function testMethionineRnaSequence(): void    {        $this->assertEquals(['Methionine'], $this->translater->getProteins('AUG'));    }    public function testPhenylalanineRnaSequenceOne(): void    {        $this->assertEquals(['Phenylalanine'], $this->translater->getProteins('UUU'));    }    public function testPhenylalanineRnaSequenceTwo(): void    {        $this->assertEquals(['Phenylalanine'], $this->translater->getProteins('UUC'));    }    public function testLeucineRnaSequenceOne(): void    {        $this->assertEquals(['Leucine'], $this->translater->getProteins('UUA'));    }    public function testLeucineRnaSequenceTwo(): void    {        $this->assertEquals(['Leucine'], $this->translater->getProteins('UUG'));    }    public function testSerineRnaSequenceOne(): void    {        $this->assertEquals(['Serine'], $this->translater->getProteins('UCU'));    }    public function testSerineRnaSequenceTwo(): void    {        $this->assertEquals(['Serine'], $this->translater->getProteins('UCC'));    }    public function testSerineRnaSequenceThree(): void    {        $this->assertEquals(['Serine'], $this->translater->getProteins('UCA'));    }    public function testSerineRnaSequenceFour(): void    {        $this->assertEquals(['Serine'], $this->translater->getProteins('UCG'));    }    public function testTyrosineRnaSequenceOne(): void    {        $this->assertEquals(['Tyrosine'], $this->translater->getProteins('UAU'));    }    public function testTyrosineRnaSequenceTwo(): void    {        $this->assertEquals(['Tyrosine'], $this->translater->getProteins('UAC'));    }    public function testCysteineRnaSequenceOne(): void    {        $this->assertEquals(['Cysteine'], $this->translater->getProteins('UGU'));    }    public function testCysteineRnaSequenceTwo(): void    {        $this->assertEquals(['Cysteine'], $this->translater->getProteins('UGC'));    }    public function testTryptophanRnaSequence(): void    {        $this->assertEquals(['Tryptophan'], $this->translater->getProteins('UGG'));    }    public function testStopCodonRnaSequenceOne(): void    {        $this->assertEquals([], $this->translater->getProteins('UAA'));    }    public function testStopCodonRnaSequenceTwo(): void    {        $this->assertEquals([], $this->translater->getProteins('UAG'));    }    public function testStopCodonRnaSequenceThree(): void    {        $this->assertEquals([], $this->translater->getProteins('UGA'));    }    public function testToCodonsTranslateToProteins(): void    {        $this->assertEquals(['Phenylalanine', 'Phenylalanine'], $this->translater->getProteins('UUUUUU'));    }    public function testToDifferentCodonsTranslateToProteins(): void    {        $this->assertEquals(['Leucine', 'Leucine'], $this->translater->getProteins('UUAUUG'));    }    public function testTranslateRnaStrandToCorrectProteinList(): void    {        $this->assertEquals(            ['Methionine', 'Phenylalanine', 'Tryptophan'],            $this->translater->getProteins('AUGUUUUGG')        );    }    public function testTranslationStopsIfStopCodonAtBeginningOfSequence(): void    {        $this->assertEquals([], $this->translater->getProteins('UAGUGG'));    }    public function testTranslationStopsIfStopCodonAtEndOfTwoCodonSequence(): void    {        $this->assertEquals(['Tryptophan'], $this->translater->getProteins('UGGUAG'));    }    public function testTranslationStopsIfStopCodonAtEndOfThreeCodonSequence(): void    {        $this->assertEquals(['Methionine', 'Phenylalanine'], $this->translater->getProteins('AUGUUUUAA'));    }    public function testTranslationStopsIfStopCodonInMiddleOfThreeCodonSequence(): void    {        $this->assertEquals(['Tryptophan'], $this->translater->getProteins('UGGUAGUGG'));    }    public function testTranslationStopsIfStopCodonInMiddleOfSixCodonSequence(): void    {        $this->assertEquals(            ['Tryptophan', 'Cysteine', 'Tyrosine'],            $this->translater->getProteins('UGGUGUUAUUAAUGGUUU')        );    }    public function invalidCodonDataProvider(): array    {        return [            'Non-existing' => ['AAA'],            'Unknown' => ['XYZ'],            'Incomplete' => ['AUGU'],        ];    }    /**     * @dataProvider invalidCodonDataProvider     */    public function testTranslateFailsForInvalidCodons(string $rna): void    {        $this->expectException(InvalidArgumentException::class);        $this->expectExceptionMessage('Invalid codon');        $this->translater->getProteins($rna);    }    public function testTranslatePassesIfStopCodeBeforeIncompleteSequence(): void    {        $this->assertEquals(['Phenylalanine', 'Phenylalanine'], $this->translater->getProteins('UUCUUCUAAUGGU'));    }}
 |