Code Duplication    Length = 33-38 lines in 6 locations

eZ/Publish/API/Repository/Tests/SectionServiceTest.php 4 locations

@@ 740-776 (lines=37) @@
737
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
738
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
739
     */
740
    public function testCreateSectionInTransactionWithRollback()
741
    {
742
        $repository = $this->getRepository();
743
744
        /* BEGIN: Use Case */
745
        $sectionService = $repository->getSectionService();
746
747
        // Start a new transaction
748
        $repository->beginTransaction();
749
750
        try {
751
            // Get a create struct and set some properties
752
            $sectionCreate = $sectionService->newSectionCreateStruct();
753
            $sectionCreate->name = 'Test Section';
754
            $sectionCreate->identifier = 'uniqueKey';
755
756
            // Create a new section
757
            $sectionService->createSection($sectionCreate);
758
        } catch (Exception $e) {
759
            // Cleanup hanging transaction on error
760
            $repository->rollback();
761
            throw $e;
762
        }
763
764
        // Rollback all changes
765
        $repository->rollback();
766
767
        try {
768
            // This call will fail with a not found exception
769
            $sectionService->loadSectionByIdentifier('uniqueKey');
770
        } catch (NotFoundException $e) {
771
            // Expected execution path
772
        }
773
        /* END: Use Case */
774
775
        $this->assertTrue(isset($e), 'Can still load section after rollback.');
776
    }
777
778
    /**
779
     * Test for the createSection() method.
@@ 785-817 (lines=33) @@
782
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
783
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
784
     */
785
    public function testCreateSectionInTransactionWithCommit()
786
    {
787
        $repository = $this->getRepository();
788
789
        /* BEGIN: Use Case */
790
        $sectionService = $repository->getSectionService();
791
792
        // Start a new transaction
793
        $repository->beginTransaction();
794
795
        try {
796
            // Get a create struct and set some properties
797
            $sectionCreate = $sectionService->newSectionCreateStruct();
798
            $sectionCreate->name = 'Test Section';
799
            $sectionCreate->identifier = 'uniqueKey';
800
801
            // Create a new section
802
            $sectionService->createSection($sectionCreate);
803
804
            // Commit all changes
805
            $repository->commit();
806
        } catch (Exception $e) {
807
            // Cleanup hanging transaction on error
808
            $repository->rollback();
809
            throw $e;
810
        }
811
812
        // Load new section
813
        $section = $sectionService->loadSectionByIdentifier('uniqueKey');
814
        /* END: Use Case */
815
816
        $this->assertEquals('uniqueKey', $section->identifier);
817
    }
818
819
    /**
820
     * Test for the createSection() method.
@@ 826-860 (lines=35) @@
823
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
824
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
825
     */
826
    public function testUpdateSectionInTransactionWithRollback()
827
    {
828
        $repository = $this->getRepository();
829
830
        /* BEGIN: Use Case */
831
        $sectionService = $repository->getSectionService();
832
833
        // Start a new transaction
834
        $repository->beginTransaction();
835
836
        try {
837
            // Load standard section
838
            $section = $sectionService->loadSectionByIdentifier('standard');
839
840
            // Get an update struct and change section name
841
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
842
            $sectionUpdate->name = 'My Standard';
843
844
            // Update section
845
            $sectionService->updateSection($section, $sectionUpdate);
846
        } catch (Exception $e) {
847
            // Cleanup hanging transaction on error
848
            $repository->rollback();
849
            throw $e;
850
        }
851
852
        // Rollback all changes
853
        $repository->rollback();
854
855
        // Load updated section, name will still be "Standard"
856
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
857
        /* END: Use Case */
858
859
        $this->assertEquals('Standard', $updatedStandard->name);
860
    }
861
862
    /**
863
     * Test for the createSection() method.
@@ 869-903 (lines=35) @@
866
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
867
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSectionByIdentifier
868
     */
869
    public function testUpdateSectionInTransactionWithCommit()
870
    {
871
        $repository = $this->getRepository();
872
873
        /* BEGIN: Use Case */
874
        $sectionService = $repository->getSectionService();
875
876
        // Start a new transaction
877
        $repository->beginTransaction();
878
879
        try {
880
            // Load standard section
881
            $section = $sectionService->loadSectionByIdentifier('standard');
882
883
            // Get an update struct and change section name
884
            $sectionUpdate = $sectionService->newSectionUpdateStruct();
885
            $sectionUpdate->name = 'My Standard';
886
887
            // Update section
888
            $sectionService->updateSection($section, $sectionUpdate);
889
890
            // Commit all changes
891
            $repository->commit();
892
        } catch (Exception $e) {
893
            // Cleanup hanging transaction on error
894
            $repository->rollback();
895
            throw $e;
896
        }
897
898
        // Load updated section, name will now be "My Standard"
899
        $updatedStandard = $sectionService->loadSectionByIdentifier('standard');
900
        /* END: Use Case */
901
902
        $this->assertEquals('My Standard', $updatedStandard->name);
903
    }
904
}
905

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

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