-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
262 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
Firestore/tests/Snippet/FirestoreSessionHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
/** | ||
* Copyright 2019 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Firestore\Tests\Snippet; | ||
|
||
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase; | ||
use Google\Cloud\Core\Testing\TestHelpers; | ||
use Google\Cloud\Firestore\Connection\ConnectionInterface; | ||
use Google\Cloud\Firestore\FirestoreClient; | ||
use Google\Cloud\Firestore\FirestoreSessionHandler; | ||
use Prophecy\Argument; | ||
|
||
/** | ||
* @group firestore | ||
* @group firestore-session | ||
* @runTestsInSeparateProcesses | ||
*/ | ||
class FirestoreSessionHandlerTest extends SnippetTestCase | ||
{ | ||
const TRANSACTION = 'transaction-id'; | ||
|
||
private $connection; | ||
private $client; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
// Since the tests in this class must run in isolation, they won't be | ||
// recognized as having been covered, and will cause a CI error. | ||
// We can call `snippetFromClass` in the parent process to mark the | ||
// snippets as having been covered. | ||
self::snippetFromClass(FirestoreSessionHandler::class); | ||
self::snippetFromClass(FirestoreSessionHandler::class, 1); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
$this->connection = $this->prophesize(ConnectionInterface::class); | ||
$this->client = TestHelpers::stub(FirestoreClient::class); | ||
} | ||
|
||
public function testClass() | ||
{ | ||
$snippet = $this->snippetFromClass(FirestoreSessionHandler::class); | ||
$snippet->replace('$firestore = new FirestoreClient();', ''); | ||
|
||
$this->connection->batchGetDocuments(Argument::withEntry('documents', Argument::type('array'))) | ||
->shouldBeCalled() | ||
->willReturn(new \ArrayIterator([ | ||
'found' => [ | ||
[ | ||
'name' => '', | ||
'fields' => [] | ||
] | ||
] | ||
])); | ||
|
||
$this->connection->beginTransaction(Argument::any()) | ||
->shouldBeCalled() | ||
->willReturn([ | ||
'transaction' => self::TRANSACTION | ||
]); | ||
|
||
$value = 'name|' . serialize('Bob'); | ||
$this->connection->commit(Argument::allOf( | ||
Argument::that(function ($args) use ($value) { | ||
return strpos($args['writes'][0]['update']['name'], 'PHPSESSID:') !== false | ||
&& $args['writes'][0]['update']['fields']['data']['stringValue'] === $value | ||
&& isset($args['writes'][0]['update']['fields']['t']['integerValue']); | ||
}), | ||
Argument::withEntry('transaction', self::TRANSACTION) | ||
))->shouldBeCalled()->willReturn([ | ||
'writeResults' => [] | ||
]); | ||
|
||
$this->client->___setProperty('connection', $this->connection->reveal()); | ||
$snippet->addLocal('firestore', $this->client); | ||
|
||
$res = $snippet->invoke(); | ||
session_write_close(); | ||
|
||
$this->assertEquals('Bob', $res->output()); | ||
} | ||
|
||
/** | ||
* @expectedException \RuntimeException | ||
*/ | ||
public function testClassErrorHandler() | ||
{ | ||
$snippet = $this->snippetFromClass(FirestoreSessionHandler::class, 1); | ||
$snippet->replace('$firestore = new FirestoreClient();', ''); | ||
|
||
$this->connection->batchGetDocuments(Argument::any()) | ||
->shouldBeCalled() | ||
->willReturn(new \ArrayIterator([ | ||
'found' => [ | ||
[ | ||
'name' => '', | ||
'fields' => [] | ||
] | ||
] | ||
])); | ||
|
||
$this->connection->beginTransaction(Argument::any()) | ||
->shouldBeCalled() | ||
->willReturn([ | ||
'transaction' => self::TRANSACTION | ||
]); | ||
|
||
$this->connection->commit(Argument::any()) | ||
->shouldBeCalled() | ||
->will(function () { | ||
trigger_error('oops!', E_USER_WARNING); | ||
}); | ||
|
||
$this->client->___setProperty('connection', $this->connection->reveal()); | ||
$snippet->addLocal('firestore', $this->client); | ||
|
||
$res = $snippet->invoke(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Copyright 2019 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Firestore\Tests\System; | ||
|
||
use Google\Cloud\Firestore\FirestoreSessionHandler; | ||
|
||
/** | ||
* @group firestore | ||
* @group firestore-session | ||
* | ||
* @runTestsInSeparateProcesses | ||
*/ | ||
class FirestoreSessionHandlerTest extends FirestoreTestCase | ||
{ | ||
public function testSessionHandler() | ||
{ | ||
$client = self::$client; | ||
|
||
$namespace = uniqid('sess-' . self::COLLECTION_NAME); | ||
$content = 'foo'; | ||
$storedValue = 'name|' . serialize($content); | ||
|
||
$handler = new FirestoreSessionHandler($client); | ||
|
||
session_set_save_handler($handler, true); | ||
session_save_path($namespace); | ||
session_start(); | ||
|
||
$sessionId = session_id(); | ||
$_SESSION['name'] = $content; | ||
|
||
session_write_close(); | ||
sleep(1); | ||
|
||
$hasDocument = false; | ||
$query = $client->collection($namespace); | ||
foreach ($query->documents() as $snapshot) { | ||
self::$deletionQueue->add($snapshot->reference()); | ||
if (!$hasDocument) { | ||
$hasDocument = $snapshot['data'] === $storedValue; | ||
} | ||
} | ||
|
||
$this->assertTrue($hasDocument); | ||
} | ||
} |
Oops, something went wrong.