Code Duplication    Length = 34-38 lines in 3 locations

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

@@ 557-594 (lines=38) @@
554
     * @covers \eZ\Publish\API\Repository\LanguageService::createLanguage
555
     * @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testCreateLanguage
556
     */
557
    public function testCreateLanguageInTransactionWithRollback()
558
    {
559
        $repository = $this->getRepository();
560
561
        /* BEGIN: Use Case */
562
        $languageService = $repository->getContentLanguageService();
563
564
        // Start a new transaction
565
        $repository->beginTransaction();
566
567
        try {
568
            // Get create struct and set properties
569
            $languageCreate = $languageService->newLanguageCreateStruct();
570
            $languageCreate->enabled = true;
571
            $languageCreate->name = 'English (New Zealand)';
572
            $languageCreate->languageCode = 'eng-NZ';
573
574
            // Create new language
575
            $languageService->createLanguage($languageCreate);
576
        } catch (Exception $e) {
577
            // Cleanup hanging transaction on error
578
            $repository->rollback();
579
            throw $e;
580
        }
581
582
        // Rollback all changes
583
        $repository->rollback();
584
585
        try {
586
            // This call will fail with a "NotFoundException"
587
            $languageService->loadLanguage('eng-NZ');
588
        } catch (NotFoundException $e) {
589
            // Expected execution path
590
        }
591
        /* END: Use Case */
592
593
        $this->assertTrue(isset($e), 'Can still load language after rollback');
594
    }
595
596
    /**
597
     * Test for the createLanguage() method.
@@ 602-635 (lines=34) @@
599
     * @covers \eZ\Publish\API\Repository\LanguageService::createLanguage
600
     * @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testCreateLanguage
601
     */
602
    public function testCreateLanguageInTransactionWithCommit()
603
    {
604
        $repository = $this->getRepository();
605
606
        /* BEGIN: Use Case */
607
        $languageService = $repository->getContentLanguageService();
608
609
        // Start a new transaction
610
        $repository->beginTransaction();
611
612
        try {
613
            // Get create struct and set properties
614
            $languageCreate = $languageService->newLanguageCreateStruct();
615
            $languageCreate->enabled = true;
616
            $languageCreate->name = 'English (New Zealand)';
617
            $languageCreate->languageCode = 'eng-NZ';
618
619
            // Create new language
620
            $languageService->createLanguage($languageCreate);
621
622
            // Commit all changes
623
            $repository->commit();
624
        } catch (Exception $e) {
625
            // Cleanup hanging transaction on error
626
            $repository->rollback();
627
            throw $e;
628
        }
629
630
        // Load new language
631
        $language = $languageService->loadLanguage('eng-NZ');
632
        /* END: Use Case */
633
634
        $this->assertEquals('eng-NZ', $language->languageCode);
635
    }
636
637
    /**
638
     * 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.