Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies and remove unused code #12

Merged
merged 5 commits into from
Jan 10, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1, 8.0]
php: [8.2, 8.1]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand All @@ -22,7 +22,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_mysql, mysql, pdo_sqlsrv, sqlsrv, pdo_pgsql, pgsql, bcmath, soap, intl, gd, exif, iconv, fileinfo
extensions: dom, curl, libxml, mbstring, zip, pdo, pdo_mysql, pdo_sqlsrv, pdo_pgsql, bcmath, soap, intl, gd, exif, iconv, fileinfo
coverage: pcov

- name: Setup problem matchers
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"doctrine/dbal": "^3.6",
"farzai/transport": "^0.0.2",
"farzai/transport": "^1.2.0",
"symfony/cache": "^5.4.21|^6.2.10",
"symfony/console": "^5.4.21|^6.2.10"
},
Expand Down
7 changes: 0 additions & 7 deletions src/Contracts/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,4 @@ public function performQuery(string $query): array;
* @return array<string>
*/
public function getTables(): array;

/**
* Get all columns with types in the given table.
*
* @return array<string, string>
*/
public function getColumns(string $table): array;
}
2 changes: 0 additions & 2 deletions src/Contracts/Database/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ interface ConnectorInterface
{
/**
* Connect to the database.
*
* @return \Farzai\Viola\Contracts\Database\ConnectionInterface
*/
public function connect(
#[SensitiveParameter] array $config
Expand Down
2 changes: 0 additions & 2 deletions src/Contracts/StorageRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ interface StorageRepositoryInterface
{
/**
* Get the value of the given key.
*
* @param mixed $default
*/
public function get(string $key, mixed $default = null): mixed;

Expand Down
18 changes: 0 additions & 18 deletions src/Database/DoctrineConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,4 @@ public function getTables(): array
{
return $this->connection->createSchemaManager()->listTableNames();
}

/**
* Get all columns with types in the given table.
*
* @return array<string, string>
*/
public function getColumns(string $table): array
{
$columns = $this->connection->createSchemaManager()->listTableColumns($table);

$columnsWithType = [];

foreach ($columns as $column) {
$columnsWithType[$column->getName()] = $column->getType()->getName();
}

return $columnsWithType;
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/UnexpectedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class UnexpectedResponse extends Exception
{
public function __construct($message = 'Unexpected response from the server', $code = 0, Exception $previous = null)
public function __construct($message = 'Unexpected response from the server', $code = 0, ?Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Storage/CacheFilesystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function __construct($prefix = '')

/**
* Get the value of the given key.
*
* @param mixed $default
*/
public function get(string $key, mixed $default = null): mixed
{
Expand Down
118 changes: 27 additions & 91 deletions tests/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,48 @@
use Farzai\Viola\Database\DoctrineConnection;

it('should return the correct platform', function () {
$connection = new DoctrineConnection(
\Mockery::mock(\Doctrine\DBAL\Connection::class)
->shouldReceive('getDatabasePlatform')
->andReturn(new \Doctrine\DBAL\Platforms\MySQL80Platform())
->getMock()
);
$mysql = new \Doctrine\DBAL\Platforms\MySQL80Platform();
$mysqlConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$mysqlConnection->expects($this->once())
->method('getDatabasePlatform')
->willReturn($mysql);

$connection = new DoctrineConnection($mysqlConnection);

expect($connection)->toBeInstanceOf(ConnectionInterface::class);

expect($connection->getPlatform())->toBe('MySQL80');
});

it('should return the correct tables', function () {
$connection = new DoctrineConnection(
\Mockery::mock(\Doctrine\DBAL\Connection::class)
->shouldReceive('createSchemaManager')
->andReturn(
\Mockery::mock(\Doctrine\DBAL\Schema\AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()
->andReturn([
'users',
'posts',
])
->getMock()
)
->getMock()
);
$schemaManager = $this->createMock(\Doctrine\DBAL\Schema\AbstractSchemaManager::class);
$schemaManager->expects($this->once())
->method('listTableNames')
->willReturn([
'users',
'posts',
]);

$mysqlConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$mysqlConnection->expects($this->once())
->method('createSchemaManager')
->willReturn($schemaManager);

$connection = new DoctrineConnection($mysqlConnection);

expect($connection->getTables())->toBe([
'users',
'posts',
]);
});

it('should return the correct columns', function () {
$connection = new DoctrineConnection(
\Mockery::mock(\Doctrine\DBAL\Connection::class)
->shouldReceive('executeQuery')
->andReturn(
\Mockery::mock(\Doctrine\DBAL\Result::class)
->shouldReceive('fetchAllAssociative')->once()
->andReturn([
['id' => 1, 'name' => 'Farzad'],
['id' => 2, 'name' => 'John'],
])
->getMock()
)
->getMock()
);

expect($connection->performQuery('SELECT * FROM users'))->toBe([
['id' => 1, 'name' => 'Farzad'],
['id' => 2, 'name' => 'John'],
]);
});

it('should throw an exception when the query is invalid', function () {
$connection = new DoctrineConnection(
\Mockery::mock(\Doctrine\DBAL\Connection::class)
->shouldReceive('executeQuery')
->andThrow(new \Exception('Invalid Query'))
->getMock()
);
$mysqlConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$mysqlConnection->expects($this->once())
->method('executeQuery')
->willThrowException(new \Exception('Invalid Query'));

$connection = new DoctrineConnection($mysqlConnection);

$connection->performQuery('SELECT * FROM users');
})->throws(\Exception::class, "Error Processing Query: \nSELECT * FROM users\n\nInvalid Query");

it('should get the correct columns', function () {
$connection = new DoctrineConnection(
\Mockery::mock(\Doctrine\DBAL\Connection::class)
->shouldReceive('createSchemaManager')
->andReturn(
\Mockery::mock(\Doctrine\DBAL\Schema\AbstractSchemaManager::class)
->shouldReceive('listTableColumns')
->andReturn([
\Mockery::mock(\Doctrine\DBAL\Schema\Column::class)
->shouldReceive('getName')->once()
->andReturn('id')
->shouldReceive('getType')->once()
->andReturn(
\Mockery::mock(\Doctrine\DBAL\Types\IntegerType::class)
->shouldReceive('getName')->once()
->andReturn('integer')
->getMock()
)
->getMock(),

\Mockery::mock(\Doctrine\DBAL\Schema\Column::class)
->shouldReceive('getName')->once()
->andReturn('name')
->shouldReceive('getType')->once()
->andReturn(
\Mockery::mock(\Doctrine\DBAL\Types\StringType::class)
->shouldReceive('getName')->once()
->andReturn('string')
->getMock()
)
->getMock(),
])
->getMock()
)
->getMock()
);

expect($connection->getColumns('users'))->toBe([
'id' => 'integer',
'name' => 'string',
]);
});
29 changes: 16 additions & 13 deletions tests/OpenAI/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
use Psr\Log\LoggerInterface;

it('should send completion success', function () {
$psrClient = \Mockery::mock(ClientInterface::class)
->shouldReceive('sendRequest')->once()
->andReturn(\Mockery::mock(\Psr\Http\Message\ResponseInterface::class)
->shouldReceive('getStatusCode')
->andReturn(200)
->getMock()
)
->getMock();
$psrResponse = $this->createMock(\Psr\Http\Message\ResponseInterface::class);
$psrResponse->expects($this->once())
->method('getStatusCode')
->willReturn(200);

$psrLogger = \Mockery::mock(LoggerInterface::class)
->shouldReceive('debug')
->getMock();
$psrClient = $this->createMock(ClientInterface::class);
$psrClient->expects($this->once())
->method('sendRequest')
->willReturn($psrResponse);

$psrLogger = $this->createMock(LoggerInterface::class);
$psrLogger->expects($this->once())
->method('info');

$client = new Client('secret', $psrClient, $psrLogger);

expect($client->sendCompletion([]))
->toBeInstanceOf(\Farzai\Transport\Contracts\ResponseInterface::class);
$response = $client->sendCompletion([]);

expect($response)->toBeInstanceOf(\Farzai\Transport\Contracts\ResponseInterface::class);
expect($response->statusCode())->toBe(200);
});