4545use Google \Cloud \Kms \V1 \MacVerifyRequest ;
4646use Google \Cloud \Kms \V1 \ProtectionLevel ;
4747use Google \Cloud \Kms \V1 \UpdateCryptoKeyRequest ;
48+ use Google \Cloud \Kms \V1 \DeleteCryptoKeyRequest ;
49+ use Google \Cloud \Kms \V1 \ListRetiredResourcesRequest ;
50+ use Google \Cloud \Kms \V1 \GetCryptoKeyRequest ;
4851use Google \Cloud \TestUtils \TestTrait ;
4952use Google \Protobuf \FieldMask ;
5053use PHPUnit \Framework \TestCase ;
@@ -820,19 +823,19 @@ public function testDeleteCryptoKey()
820823 $ client = new KeyManagementServiceClient ();
821824 $ keyRingName = $ client ->keyRingName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId );
822825 $ keyId = self ::randomId ();
823-
826+
824827 // Create an ASYMMETRIC_SIGN key (no initial version created by default for this purpose).
825828 $ key = (new CryptoKey ())
826829 ->setPurpose (CryptoKeyPurpose::ASYMMETRIC_SIGN )
827830 ->setVersionTemplate ((new CryptoKeyVersionTemplate )
828831 ->setAlgorithm (CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256 ));
829-
832+
830833 $ request = (new CreateCryptoKeyRequest ())
831834 ->setParent ($ keyRingName )
832835 ->setCryptoKeyId ($ keyId )
833836 ->setCryptoKey ($ key )
834837 ->setSkipInitialVersionCreation (true );
835-
838+
836839 $ client ->createCryptoKey ($ request );
837840
838841 // Delete it.
@@ -844,72 +847,70 @@ public function testDeleteCryptoKey()
844847 ]);
845848
846849 $ this ->assertStringContainsString ('Deleted crypto key ' , $ output );
847-
850+
848851 $ keyName = $ client ->cryptoKeyName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId , $ keyId );
849852 try {
850- $ getKeyRequest = (new \ Google \ Cloud \ Kms \ V1 \ GetCryptoKeyRequest ())->setName ($ keyName );
851- $ deletedKey = $ client ->getCryptoKey ($ getKeyRequest );
852- $ this ->assertEquals ( CryptoKey \State:: DELETED , $ deletedKey -> getState () );
853+ $ getKeyRequest = (new GetCryptoKeyRequest ())->setName ($ keyName );
854+ $ client ->getCryptoKey ($ getKeyRequest );
855+ $ this ->fail ( ' Key should be deleted ' );
853856 } catch (\Google \ApiCore \ApiException $ e ) {
854- // If the key is not found, it might be due to eventual consistency or it's effectively deleted.
855- // However, typically it SHOULD exist in DELETED state.
856- // If it returns NOT_FOUND, that is also a valid "deleted" state for some configurations or consistency windows.
857- // Let's accept NOT_FOUND as valid for this test.
858857 $ this ->assertEquals (\Google \Rpc \Code::NOT_FOUND , $ e ->getCode ());
859858 }
860859
861860 return $ keyId ;
862861 }
863862
864- /**
865- * @depends testDeleteCryptoKey
866- */
867- public function testListRetiredResources ($ deletedKeyId )
868- {
869- list (, $ output ) = $ this ->runFunctionSnippet ('list_retired_resources ' , [
870- self ::$ projectId ,
871- self ::$ locationId
872- ]);
873-
874- $ this ->assertStringContainsString ('Retired Resource Name ' , $ output );
875- $ this ->assertStringContainsString ($ deletedKeyId , $ output );
876- }
877-
878- /**
879- * @depends testDeleteCryptoKey
880- */
881- public function testGetRetiredResource ($ deletedKeyId )
863+ public function testListAndGetRetiredResource ()
882864 {
865+ // Create a key to delete
883866 $ client = new KeyManagementServiceClient ();
867+ $ keyRingName = $ client ->keyRingName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId );
868+ $ keyId = self ::randomId ();
869+ $ key = (new CryptoKey ())
870+ ->setPurpose (CryptoKeyPurpose::ASYMMETRIC_SIGN )
871+ ->setVersionTemplate ((new CryptoKeyVersionTemplate )
872+ ->setAlgorithm (CryptoKeyVersionAlgorithm::EC_SIGN_P256_SHA256 ));
873+
874+ // Create key (with no initial version)
875+ $ request = (new CreateCryptoKeyRequest ())
876+ ->setParent ($ keyRingName )
877+ ->setCryptoKeyId ($ keyId )
878+ ->setCryptoKey ($ key )
879+ ->setSkipInitialVersionCreation (true );
880+ $ client ->createCryptoKey ($ request );
881+
882+ // Delete it
883+ $ keyName = $ client ->cryptoKeyName (self ::$ projectId , self ::$ locationId , self ::$ keyRingId , $ keyId );
884+ $ deleteRequest = (new DeleteCryptoKeyRequest ())->setName ($ keyName );
885+ $ client ->deleteCryptoKey ($ deleteRequest );
886+
887+ // Find the retired resource ID first (needed for the snippet)
884888 $ parent = $ client ->locationName (self ::$ projectId , self ::$ locationId );
885- $ listRequest = (new \ Google \ Cloud \ Kms \ V1 \ ListRetiredResourcesRequest ())->setParent ($ parent );
886-
889+ $ listRequest = (new ListRetiredResourcesRequest ())->setParent ($ parent );
890+
887891 $ retiredResource = null ;
888892 foreach ($ client ->listRetiredResources ($ listRequest ) as $ res ) {
889- if (strpos ($ res ->getOriginalResource (), $ deletedKeyId ) !== false ) {
893+ if (strpos ($ res ->getOriginalResource (), $ keyId ) !== false ) {
890894 $ retiredResource = $ res ;
891895 break ;
892896 }
893897 }
894-
895- if (!$ retiredResource ) {
896- $ this ->markTestSkipped ('Could not find retired resource for retrieval test. ' );
897- return ;
898- }
898+
899+ $ this ->assertNotNull ($ retiredResource , 'Could not find retired resource for retrieval test. ' );
899900
900901 $ parts = explode ('/ ' , $ retiredResource ->getName ());
901902 $ retiredResourceId = end ($ parts );
902-
903- list (, $ output ) = $ this ->runFunctionSnippet ('get_retired_resource ' , [
903+
904+ list ($ response , $ output ) = $ this ->runFunctionSnippet ('get_retired_resource ' , [
904905 self ::$ projectId ,
905906 self ::$ locationId ,
906907 $ retiredResourceId
907908 ]);
908909
910+ $ this ->assertStringContainsString ($ keyId , $ response ->getOriginalResource ());
909911 $ this ->assertStringContainsString ('Retired Resource Name ' , $ output );
910- $ this ->assertStringContainsString ($ deletedKeyId , $ output );
911912 }
912-
913+
913914 public function testDeleteCryptoKeyVersion ()
914915 {
915916 $ this ->markTestSkipped ('Skipping deleteCryptoKeyVersion test due to complexity of destroying a key version. ' );
0 commit comments