diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e9be6c5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: php + +services: + - docker + +script: docker-compose -f docker-compose.test.yaml run php sh /src/tests.bootstrap.sh diff --git a/composer.json b/composer.json index bb554fc..2ca23e2 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,10 @@ "illuminate/support": "^7.0|^8.0|^9.0", "illuminate/database": "^7.0|^8.0|^9.0" }, + "require-dev": { + "laravel/framework": "^9.0", + "phpunit/phpunit": "^9" + }, "suggest": { }, "autoload": { diff --git a/docker-compose.test.yaml b/docker-compose.test.yaml new file mode 100644 index 0000000..6f578e4 --- /dev/null +++ b/docker-compose.test.yaml @@ -0,0 +1,18 @@ +version: '3' + +services: + + php: + build: tests/docker + depends_on: + - clickhouse + volumes: + - ./:/src + + clickhouse: + image: yandex/clickhouse-server + ulimits: + nproc: 65535 + nofile: + soft: 262144 + hard: 262144 \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..6a0c68b --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,12 @@ + + + + + tests + + + diff --git a/tests.bootstrap.sh b/tests.bootstrap.sh new file mode 100644 index 0000000..4e6deb8 --- /dev/null +++ b/tests.bootstrap.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Installing current library files to empty laravel app +cp -r /src/* vendor/glushkovds/phpclickhouse-laravel + +# Preparing Phpunit +cp /src/phpunit.xml phpunit.xml +rm -rf /app/tests/Feature +rm -rf /app/tests/Unit +cp -r /src/tests/* /app/tests + +# Configuring environment +cp /src/tests/config/database.php /app/config/database.php +cp /src/tests/config/app.php /app/config/app.php +cp /src/tests/migrations/exampleTable.php /app/database/migrations/2022_11_11_120334_example.php +cat /src/tests/config/.env >> /app/.env + +# Running tests +php artisan test diff --git a/tests/BaseTest.php b/tests/BaseTest.php new file mode 100644 index 0000000..611043a --- /dev/null +++ b/tests/BaseTest.php @@ -0,0 +1,21 @@ +getClient(); + Artisan::call('migrate'); + Example::insertAssoc([['f_int' => 1, 'f_string' => 'zz']]); + $rows = Example::select()->getRows(); + $this->assertEquals(1, $rows[0]['f_int']); + $this->assertEquals('zz', $rows[0]['f_string']); + } +} \ No newline at end of file diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php new file mode 100644 index 0000000..547152f --- /dev/null +++ b/tests/CreatesApplication.php @@ -0,0 +1,22 @@ +make(Kernel::class)->bootstrap(); + + return $app; + } +} diff --git a/tests/Models/Example.php b/tests/Models/Example.php new file mode 100644 index 0000000..1bb37aa --- /dev/null +++ b/tests/Models/Example.php @@ -0,0 +1,10 @@ + env('APP_NAME', 'Laravel'), + + /* + |-------------------------------------------------------------------------- + | Application Environment + |-------------------------------------------------------------------------- + | + | This value determines the "environment" your application is currently + | running in. This may determine how you prefer to configure various + | services the application utilizes. Set this in your ".env" file. + | + */ + + 'env' => env('APP_ENV', 'production'), + + /* + |-------------------------------------------------------------------------- + | Application Debug Mode + |-------------------------------------------------------------------------- + | + | When your application is in debug mode, detailed error messages with + | stack traces will be shown on every error that occurs within your + | application. If disabled, a simple generic error page is shown. + | + */ + + 'debug' => (bool) env('APP_DEBUG', false), + + /* + |-------------------------------------------------------------------------- + | Application URL + |-------------------------------------------------------------------------- + | + | This URL is used by the console to properly generate URLs when using + | the Artisan command line tool. You should set this to the root of + | your application so that it is used when running Artisan tasks. + | + */ + + 'url' => env('APP_URL', 'http://localhost'), + + 'asset_url' => env('ASSET_URL'), + + /* + |-------------------------------------------------------------------------- + | Application Timezone + |-------------------------------------------------------------------------- + | + | Here you may specify the default timezone for your application, which + | will be used by the PHP date and date-time functions. We have gone + | ahead and set this to a sensible default for you out of the box. + | + */ + + 'timezone' => 'UTC', + + /* + |-------------------------------------------------------------------------- + | Application Locale Configuration + |-------------------------------------------------------------------------- + | + | The application locale determines the default locale that will be used + | by the translation service provider. You are free to set this value + | to any of the locales which will be supported by the application. + | + */ + + 'locale' => 'en', + + /* + |-------------------------------------------------------------------------- + | Application Fallback Locale + |-------------------------------------------------------------------------- + | + | The fallback locale determines the locale to use when the current one + | is not available. You may change the value to correspond to any of + | the language folders that are provided through your application. + | + */ + + 'fallback_locale' => 'en', + + /* + |-------------------------------------------------------------------------- + | Faker Locale + |-------------------------------------------------------------------------- + | + | This locale will be used by the Faker PHP library when generating fake + | data for your database seeds. For example, this will be used to get + | localized telephone numbers, street address information and more. + | + */ + + 'faker_locale' => 'en_US', + + /* + |-------------------------------------------------------------------------- + | Encryption Key + |-------------------------------------------------------------------------- + | + | This key is used by the Illuminate encrypter service and should be set + | to a random, 32 character string, otherwise these encrypted strings + | will not be safe. Please do this before deploying an application! + | + */ + + 'key' => env('APP_KEY'), + + 'cipher' => 'AES-256-CBC', + + /* + |-------------------------------------------------------------------------- + | Maintenance Mode Driver + |-------------------------------------------------------------------------- + | + | These configuration options determine the driver used to determine and + | manage Laravel's "maintenance mode" status. The "cache" driver will + | allow maintenance mode to be controlled across multiple machines. + | + | Supported drivers: "file", "cache" + | + */ + + 'maintenance' => [ + 'driver' => 'file', + // 'store' => 'redis', + ], + + /* + |-------------------------------------------------------------------------- + | Autoloaded Service Providers + |-------------------------------------------------------------------------- + | + | The service providers listed here will be automatically loaded on the + | request to your application. Feel free to add your own services to + | this array to grant expanded functionality to your applications. + | + */ + + 'providers' => [ + + /* + * Laravel Framework Service Providers... + */ + Illuminate\Auth\AuthServiceProvider::class, + Illuminate\Broadcasting\BroadcastServiceProvider::class, + Illuminate\Bus\BusServiceProvider::class, + Illuminate\Cache\CacheServiceProvider::class, + Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, + Illuminate\Cookie\CookieServiceProvider::class, + Illuminate\Database\DatabaseServiceProvider::class, + Illuminate\Encryption\EncryptionServiceProvider::class, + Illuminate\Filesystem\FilesystemServiceProvider::class, + Illuminate\Foundation\Providers\FoundationServiceProvider::class, + Illuminate\Hashing\HashServiceProvider::class, + Illuminate\Mail\MailServiceProvider::class, + Illuminate\Notifications\NotificationServiceProvider::class, + Illuminate\Pagination\PaginationServiceProvider::class, + Illuminate\Pipeline\PipelineServiceProvider::class, + Illuminate\Queue\QueueServiceProvider::class, + Illuminate\Redis\RedisServiceProvider::class, + Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, + Illuminate\Session\SessionServiceProvider::class, + Illuminate\Translation\TranslationServiceProvider::class, + Illuminate\Validation\ValidationServiceProvider::class, + Illuminate\View\ViewServiceProvider::class, + + /* + * Package Service Providers... + */ + + /* + * Application Service Providers... + */ + App\Providers\AppServiceProvider::class, + App\Providers\AuthServiceProvider::class, + // App\Providers\BroadcastServiceProvider::class, + App\Providers\EventServiceProvider::class, + App\Providers\RouteServiceProvider::class, + + \PhpClickHouseLaravel\ClickhouseServiceProvider::class, + ], + + /* + |-------------------------------------------------------------------------- + | Class Aliases + |-------------------------------------------------------------------------- + | + | This array of class aliases will be registered when this application + | is started. However, feel free to register as many as you wish as + | the aliases are "lazy" loaded so they don't hinder performance. + | + */ + + 'aliases' => Facade::defaultAliases()->merge([ + // 'ExampleClass' => App\Example\ExampleClass::class, + ])->toArray(), + +]; diff --git a/tests/config/database.php b/tests/config/database.php new file mode 100644 index 0000000..e957380 --- /dev/null +++ b/tests/config/database.php @@ -0,0 +1,68 @@ + 'clickhouse', + + /* + |-------------------------------------------------------------------------- + | Database Connections + |-------------------------------------------------------------------------- + | + | Here are each of the database connections setup for your application. + | Of course, examples of configuring each database platform that is + | supported by Laravel is shown below to make development simple. + | + | + | All database work in Laravel is done through the PHP PDO facilities + | so make sure you have the driver for your particular database of + | choice installed on your machine before you begin development. + | + */ + + 'connections' => [ + + 'clickhouse' => [ + 'driver' => 'clickhouse', + 'host' => env('CLICKHOUSE_HOST'), + 'port' => env('CLICKHOUSE_PORT', '8123'), + 'database' => env('CLICKHOUSE_DATABASE', 'default'), + 'username' => env('CLICKHOUSE_USERNAME', 'default'), + 'password' => env('CLICKHOUSE_PASSWORD', ''), + 'timeout_connect' => env('CLICKHOUSE_TIMEOUT_CONNECT', 2), + 'timeout_query' => env('CLICKHOUSE_TIMEOUT_QUERY', 2), + 'https' => (bool)env('CLICKHOUSE_HTTPS', null), + 'retries' => env('CLICKHOUSE_RETRIES', 0), + 'settings' => [ + 'max_partitions_per_insert_block' => 300, + ], + ], + ], + + /* + |-------------------------------------------------------------------------- + | Migration Repository Table + |-------------------------------------------------------------------------- + | + | This table keeps track of all the migrations that have already run for + | your application. Using this information, we can determine which of + | the migrations on disk haven't actually been run in the database. + | + */ + + 'migrations' => 'migrations', + +]; diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile new file mode 100644 index 0000000..53a1c5a --- /dev/null +++ b/tests/docker/Dockerfile @@ -0,0 +1,5 @@ +FROM bitnami/laravel + +RUN cd / && composer create-project laravel/laravel app +RUN composer require glushkovds/phpclickhouse-laravel +WORKDIR "/app" diff --git a/tests/migrations/exampleTable.php b/tests/migrations/exampleTable.php new file mode 100644 index 0000000..01b8e2b --- /dev/null +++ b/tests/migrations/exampleTable.php @@ -0,0 +1,33 @@ +