objectsService = $this->createMock( ObjectsService::class ); $this->objectsService->method( 'query' ) ->will( $this->returnCallback( function( $query) { $existsId = sprintf( 'https://example.com/ap/objects/%s', self::EXISTING_ID_STR ); if ( array_key_exists( 'id', $query ) && $query['id'] == $existsId ) { return array( 'existingObject' ); } else { return array(); } } ) ); } public function testIdProvider() { $testCases = array( array( 'id' => 'providesId', 'providedRnd' => array( 'foo' ), 'expectedId' => 'https://example.com/ap/objects/foo', ), array( 'id' => 'checksForExisting', 'providedRnd' => array( self::EXISTING_ID_STR, 'bar' ), 'expectedId' => 'https://example.com/ap/objects/bar', ), array( 'id' => 'addsPath', 'providedRnd' => array( 'foo' ), 'path' => 'notes', 'expectedId' => 'https://example.com/ap/notes/foo', ), ); foreach ( $testCases as $testCase ) { $randomProvider = $this->createMock( RandomProvider::class ); call_user_func_array( array( $randomProvider->method( 'randomString' ), 'willReturnOnConsecutiveCalls' ), $testCase['providedRnd'] ); $idProvider = new IdProvider( $this->objectsService, $randomProvider, 'ap' ); $request = Request::create( 'https://example.com' ); $id = ''; if ( array_key_exists( 'path', $testCase ) ) { $id = $idProvider->getId( $request, $testCase['path'] ); } else { $id = $idProvider->getId( $request ); } $this->assertEquals( $testCase['expectedId'], $id, "Error on test $testCase[id]" ); } } } ?>