Code Duplication    Length = 34-38 lines in 3 locations

eZ/Publish/API/Repository/Tests/LanguageServiceTest.php 2 locations

@@ 588-625 (lines=38) @@
585
     * @covers \eZ\Publish\API\Repository\LanguageService::createLanguage
586
     * @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testCreateLanguage
587
     */
588
    public function testCreateLanguageInTransactionWithRollback()
589
    {
590
        $repository = $this->getRepository();
591
592
        /* BEGIN: Use Case */
593
        $languageService = $repository->getContentLanguageService();
594
595
        // Start a new transaction
596
        $repository->beginTransaction();
597
598
        try {
599
            // Get create struct and set properties
600
            $languageCreate = $languageService->newLanguageCreateStruct();
601
            $languageCreate->enabled = true;
602
            $languageCreate->name = 'English (New Zealand)';
603
            $languageCreate->languageCode = 'eng-NZ';
604
605
            // Create new language
606
            $languageService->createLanguage($languageCreate);
607
        } catch (Exception $e) {
608
            // Cleanup hanging transaction on error
609
            $repository->rollback();
610
            throw $e;
611
        }
612
613
        // Rollback all changes
614
        $repository->rollback();
615
616
        try {
617
            // This call will fail with a "NotFoundException"
618
            $languageService->loadLanguage('eng-NZ');
619
        } catch (NotFoundException $e) {
620
            // Expected execution path
621
        }
622
        /* END: Use Case */
623
624
        $this->assertTrue(isset($e), 'Can still load language after rollback');
625
    }
626
627
    /**
628
     * Test for the createLanguage() method.
@@ 633-666 (lines=34) @@
630
     * @covers \eZ\Publish\API\Repository\LanguageService::createLanguage
631
     * @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testCreateLanguage
632
     */
633
    public function testCreateLanguageInTransactionWithCommit()
634
    {
635
        $repository = $this->getRepository();
636
637
        /* BEGIN: Use Case */
638
        $languageService = $repository->getContentLanguageService();
639
640
        // Start a new transaction
641
        $repository->beginTransaction();
642
643
        try {
644
            // Get create struct and set properties
645
            $languageCreate = $languageService->newLanguageCreateStruct();
646
            $languageCreate->enabled = true;
647
            $languageCreate->name = 'English (New Zealand)';
648
            $languageCreate->languageCode = 'eng-NZ';
649
650
            // Create new language
651
            $languageService->createLanguage($languageCreate);
652
653
            // Commit all changes
654
            $repository->commit();
655
        } catch (Exception $e) {
656
            // Cleanup hanging transaction on error
657
            $repository->rollback();
658
            throw $e;
659
        }
660
661
        // Load new language
662
        $language = $languageService->loadLanguage('eng-NZ');
663
        /* END: Use Case */
664
665
        $this->assertEquals('eng-NZ', $language->languageCode);
666
    }
667
668
    /**
669
     * Test for the updateLanguageName() method.

eZ/Publish/API/Repository/Tests/SectionServiceTest.php 1 location

@@ 792-828 (lines=37) @@
789
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
790
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
791
     */
792
    public function testCreateSectionInTransactionWithRollback()
793
    {
794
        $repository = $this->getRepository();
795
796
        /* BEGIN: Use Case */
797
        $sectionService = $repository->getSectionService();
798
799
        // Start a new transaction
800
        $repository->beginTransaction();
801
802
        try {
803
            // Get a create struct and set some properties
804
            $sectionCreate = $sectionService->newSectionCreateStruct();
805
            $sectionCreate->name = 'Test Section';
806
            $sectionCreate->identifier = 'uniqueKey';
807
808
            // Create a new section
809
            $sectionService->createSection($sectionCreate);
810
        } catch (Exception $e) {
811
            // Cleanup hanging transaction on error
812
            $repository->rollback();
813
            throw $e;
814
        }
815
816
        // Rollback all changes
817
        $repository->rollback();
818
819
        try {
820
            // This call will fail with a not found exception
821
            $sectionService->loadSectionByIdentifier('uniqueKey');
822
        } catch (NotFoundException $e) {
823
            // Expected execution path
824
        }
825
        /* END: Use Case */
826
827
        $this->assertTrue(isset($e), 'Can still load section after rollback.');
828
    }
829
830
    /**
831
     * Test for the createSection() method.