Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2018 Google Inc.
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,25 +26,36 @@
use Google\Cloud\Firestore\FirestoreClient;

/**
* An example of an invalid range query. @see https://cloud.google.com/firestore/docs/query-data/queries#compound_queries
* Example of a query with range and inequality filters on multiple fields.
* @see https://cloud.google.com/firestore/docs/query-data/multiple-range-fields
*
* @param string $projectId The Google Cloud Project ID
*/
function query_filter_range_invalid(string $projectId): void
function query_filter_compound_multi_ineq(string $projectId): void
{
// Create the Cloud Firestore client
$db = new FirestoreClient([
'projectId' => $projectId,
]);
$citiesRef = $db->collection('samples/php/cities');
# [START firestore_query_filter_range_invalid]
$invalidRangeQuery = $citiesRef
->where('state', '>=', 'CA')
->where('population', '>', 1000000);
# [END firestore_query_filter_range_invalid]
$collection = $db->collection('samples/php/users');
// Setup the data before querying for it
$collection->document('person1')->set(['age' => 23, 'height' => 65]);
$collection->document('person2')->set(['age' => 37, 'height' => 55]);
$collection->document('person3')->set(['age' => 40, 'height' => 75]);
$collection->document('person4')->set(['age' => 40, 'height' => 65]);

// This will throw an exception
$invalidRangeQuery->documents();
# [START firestore_query_filter_compound_multi_ineq]
$chainedQuery = $collection
->where('age', '>', 35)
->where('height', '>', 60)
->where('height', '<', 70);
# [END firestore_query_filter_compound_multi_ineq]
foreach ($chainedQuery->documents() as $document) {
printf(
'Document %s returned by age > 35 and height between 60 and 70' . PHP_EOL,
$document->id()
);
}
}

// The following 2 lines are only needed to run the samples
Expand Down
52 changes: 0 additions & 52 deletions firestore/src/query_order_field_invalid.php

This file was deleted.

31 changes: 6 additions & 25 deletions firestore/test/firestoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace Google\Cloud\Samples\Firestore;

use Google\Cloud\Core\Exception\BadRequestException;
use Google\Cloud\Core\Exception\FailedPreconditionException;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\TestUtils\TestTrait;
Expand Down Expand Up @@ -275,6 +274,12 @@ public function testChainedQuery()
$this->assertStringContainsString('Document SF returned by query state=CA and name=San Francisco', $output);
}

public function testChainedInequalityQuery()
{
$output = $this->runFirestoreSnippet('query_filter_compound_multi_ineq');
$this->assertStringContainsString('Document person4 returned by age > 35 and height between 60 and 70', $output);
}

/**
* @depends testQueryCreateExamples
*/
Expand All @@ -298,18 +303,6 @@ public function testRangeQuery()
$this->assertStringContainsString('Document SF returned by query CA<=state<=IN', $output);
}

/**
* @depends testQueryCreateExamples
*/
public function testInvalidRangeQuery()
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage(
'Cannot have inequality filters on multiple properties'
);
$this->runFirestoreSnippet('query_filter_range_invalid');
}

/**
* @depends testQueryCreateExamples
*/
Expand Down Expand Up @@ -509,18 +502,6 @@ public function testRangeOrderByQuery()
$this->assertStringContainsString('Document BJ returned by range with order by query', $output);
}

/**
* @depends testRetrieveCreateExamples
*/
public function testInvalidRangeOrderByQuery()
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage(
'inequality filter property and first sort order must be the same'
);
$this->runFirestoreSnippet('query_order_field_invalid');
}

public function testDocumentRef()
{
$output = $this->runFirestoreSnippet('data_reference_document');
Expand Down