Skip to content

Commit c54a319

Browse files
author
Илья
committed
Added ReadFromFileCredentials
1 parent f351b87 commit c54a319

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/Auth/ReadFromFileCredentials.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Auth;
4+
5+
class ReadFromFileCredentials extends Auth
6+
{
7+
8+
protected $fileName;
9+
protected $readInterval;
10+
11+
/**
12+
* @param string $fileName
13+
* @param int $readInterval
14+
*/
15+
public function __construct(string $fileName = "token.txt", int $readInterval = 600)
16+
{
17+
$this->fileName = $fileName;
18+
$this->readInterval = $readInterval;
19+
}
20+
21+
public function getTokenInfo(): TokenInfo
22+
{
23+
return new TokenInfo(file_get_contents($this->fileName), $this->readInterval, 1);
24+
}
25+
26+
public function getName(): string
27+
{
28+
return "from file";
29+
}
30+
}

tests/CheckReadTokenFile.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace YdbPlatform\Ydb\Test;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use YdbPlatform\Ydb\Auth\ReadFromFileCredentials;
6+
use YdbPlatform\Ydb\Ydb;
7+
use YdbPlatform\Ydb\YdbTable;
8+
9+
class CheckReadTokenFile extends TestCase{
10+
public function testRead(){
11+
$config = [
12+
13+
// Database path
14+
'database' => '/local',
15+
16+
// Database endpoint
17+
'endpoint' => 'localhost:2136',
18+
19+
// Auto discovery (dedicated server only)
20+
'discovery' => false,
21+
22+
'credentials' => new ReadFromFileCredentials("./token.txt")
23+
];
24+
25+
$ydb = new Ydb($config);
26+
self::assertEquals('test-example-token',
27+
$ydb->iam()->token()
28+
);
29+
30+
31+
}
32+
33+
private function getYdbResult(array $config) : array
34+
{
35+
$table = $ydb->table();
36+
37+
$session = $table->createSession();
38+
39+
$session->createTable(
40+
'episodes',
41+
YdbTable::make()
42+
->addColumn('series_id', 'UINT64')
43+
->addColumn('title', 'UTF8')
44+
->addColumn('episode_id', 'UINT64')
45+
->addColumn('season_id', 'UINT64')
46+
->primaryKey('series_id')
47+
);
48+
49+
$session->transaction(function($session) {
50+
return $session->query('
51+
UPSERT INTO `episodes` (series_id, season_id, episode_id, title)
52+
VALUES (2, 6, 1, "TBD");');
53+
});
54+
55+
$result = $session->query('select `series_id`, `season_id`, `episode_id`, `title` from `episodes`;');
56+
return [$result->rowCount(), $result->rows()[0]["season_id"],
57+
$result->rows()[0]["episode_id"],
58+
$result->rows()[0]["series_id"],
59+
$result->rows()[0]["title"]];
60+
}
61+
}

tests/token.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-example-token

0 commit comments

Comments
 (0)