From 240f3c126bb9b8b2028918b40b32af92a0a01161 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 19 Nov 2016 15:55:49 +0100 Subject: [PATCH] Restored some tests. --- .env.testing | 45 +++++++ config/database.php | 121 ++++-------------- .../2016_08_25_091522_changes_for_3101.php | 2 +- test.sh | 41 ++++++ tests/ExampleTest.php | 6 +- tests/TestCase.php | 87 ++++++++++++- .../Controllers/HomeControllerTest.php | 63 +++++++++ tests/unit/Models/TransactionTypeTest.php | 43 +++++++ 8 files changed, 306 insertions(+), 102 deletions(-) create mode 100755 .env.testing create mode 100755 test.sh create mode 100755 tests/acceptance/Controllers/HomeControllerTest.php create mode 100755 tests/unit/Models/TransactionTypeTest.php diff --git a/.env.testing b/.env.testing new file mode 100755 index 0000000000..3c9802e829 --- /dev/null +++ b/.env.testing @@ -0,0 +1,45 @@ +APP_ENV=testing +APP_DEBUG=true +APP_FORCE_SSL=false +APP_FORCE_ROOT= +APP_KEY=TestTestTestTestTestTestTestTest +APP_LOG_LEVEL=debug +APP_URL=http://localhost + +DB_CONNECTION=sqlite +DB_HOST=127.0.0.1 +DB_PORT=3306 +DB_USERNAME=homestead +DB_PASSWORD=secret + +BROADCAST_DRIVER=log +CACHE_DRIVER=file +SESSION_DRIVER=file +QUEUE_DRIVER=sync + +COOKIE_PATH="/" +COOKIE_DOMAIN= +COOKIE_SECURE=false + +REDIS_HOST=127.0.0.1 +REDIS_PASSWORD=null +REDIS_PORT=6379 + +MAIL_DRIVER=smtp +MAIL_HOST=mailtrap.io +MAIL_PORT=2525 +MAIL_FROM=changeme@example.com +MAIL_USERNAME=null +MAIL_PASSWORD=null +MAIL_ENCRYPTION=null + +SEND_REGISTRATION_MAIL=true +SEND_ERROR_MESSAGE=true +SHOW_INCOMPLETE_TRANSLATIONS=false + +ANALYTICS_ID= +SITE_OWNER=mail@example.com + +PUSHER_KEY= +PUSHER_SECRET= +PUSHER_APP_ID= diff --git a/config/database.php b/config/database.php index 81a863d474..451ffd9741 100755 --- a/config/database.php +++ b/config/database.php @@ -11,120 +11,51 @@ return [ - /* - |-------------------------------------------------------------------------- - | PDO Fetch Style - |-------------------------------------------------------------------------- - | - | By default, database results will be returned as instances of the PHP - | stdClass object; however, you may desire to retrieve records in an - | array format for simplicity. Here you can tweak the fetch style. - | - */ - - 'fetch' => PDO::FETCH_OBJ, - - /* - |-------------------------------------------------------------------------- - | Default Database Connection Name - |-------------------------------------------------------------------------- - | - | Here you may specify which of the database connections below you wish - | to use as your default connection for all database work. Of course - | you may use many connections at once using the Database library. - | - */ - - 'default' => env('DB_CONNECTION', 'mysql'), - - /* - |-------------------------------------------------------------------------- - | 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. - | - */ - + 'fetch' => PDO::FETCH_OBJ, + 'default' => env('DB_CONNECTION', 'mysql'), 'connections' => [ - 'sqlite' => [ - 'driver' => 'sqlite', - 'database' => env('DB_DATABASE', database_path('database.sqlite')), - 'prefix' => '', + 'driver' => 'sqlite', + 'database' => env('DB_DATABASE', storage_path('database/database.sqlite')), + 'prefix' => '', ], 'mysql' => [ - 'driver' => 'mysql', - 'host' => env('DB_HOST', 'localhost'), - 'port' => env('DB_PORT', '3306'), - 'database' => env('DB_DATABASE', 'forge'), - 'username' => env('DB_USERNAME', 'forge'), - 'password' => env('DB_PASSWORD', ''), - 'charset' => 'utf8', + 'driver' => 'mysql', + 'host' => env('DB_HOST', 'localhost'), + 'port' => env('DB_PORT', '3306'), + 'database' => env('DB_DATABASE', 'forge'), + 'username' => env('DB_USERNAME', 'forge'), + 'password' => env('DB_PASSWORD', ''), + 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', - 'prefix' => '', - 'strict' => true, - 'engine' => null, + 'prefix' => '', + 'strict' => true, + 'engine' => null, ], 'pgsql' => [ - 'driver' => 'pgsql', - 'host' => env('DB_HOST', 'localhost'), - 'port' => env('DB_PORT', '5432'), + 'driver' => 'pgsql', + 'host' => env('DB_HOST', 'localhost'), + 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), - 'charset' => 'utf8', - 'prefix' => '', - 'schema' => 'public', - 'sslmode' => 'prefer', + 'charset' => 'utf8', + 'prefix' => '', + 'schema' => 'public', + 'sslmode' => 'prefer', ], ], - - /* - |-------------------------------------------------------------------------- - | 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', - - /* - |-------------------------------------------------------------------------- - | Redis Databases - |-------------------------------------------------------------------------- - | - | Redis is an open source, fast, and advanced key-value store that also - | provides a richer set of commands than a typical key-value systems - | such as APC or Memcached. Laravel makes it easy to dig right in. - | - */ - - 'redis' => [ - + 'migrations' => 'migrations', + 'redis' => [ 'cluster' => false, - 'default' => [ - 'host' => env('REDIS_HOST', 'localhost'), + 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), - 'port' => env('REDIS_PORT', 6379), + 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], - ], - ]; diff --git a/database/migrations/2016_08_25_091522_changes_for_3101.php b/database/migrations/2016_08_25_091522_changes_for_3101.php index 24883b7933..15a0c54afd 100644 --- a/database/migrations/2016_08_25_091522_changes_for_3101.php +++ b/database/migrations/2016_08_25_091522_changes_for_3101.php @@ -37,7 +37,7 @@ class ChangesFor3101 extends Migration { Schema::table( 'import_jobs', function (Blueprint $table) { - $table->text('extended_status'); + $table->text('extended_status')->nullable(); } ); } diff --git a/test.sh b/test.sh new file mode 100755 index 0000000000..6c2e7847fd --- /dev/null +++ b/test.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +DATABASE=storage/database/database.sqlite +DATABASECOPY=storage/database/databasecopy.sqlite + + +# backup current config: +mv .env .env.current + +# enable testing config +cp .env.testing .env + +# clear cache: +php artisan cache:clear + +if [ "$1" == "--reset" ]; then + echo "Must reset database" + + # touch files to make sure they exist. + touch $DATABASE + touch $DATABASECOPY + + # truncate original database file + truncate $DATABASE --size 0 + + # run migration + php artisan migrate:refresh --seed + + # copy new database over backup (resets backup) + cp $DATABASE $DATABASECOPY +fi + +# take database from copy: +cp $DATABASECOPY $DATABASE + +# run PHPUnit + +phpunit + +# restore current config: +mv .env.current .env \ No newline at end of file diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 451bd48bed..5f8b620794 100755 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,9 +1,5 @@ visit('/') - ->see('Laravel'); + ->see('Firefly'); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 2781681df5..79e8cbf40b 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,9 @@ id)->where('name', 'viewRange')->delete(); + Preference::create( + [ + 'user_id' => $user->id, + 'name' => 'viewRange', + 'data' => $range, + ] + ); + // set period to match? + + } + if ($range === 'custom') { + $this->session( + [ + 'start' => Carbon::now()->subDays(20), + 'end' => Carbon::now(), + ] + ); + } + } + /** * Creates the application. * @@ -19,10 +52,62 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase */ public function createApplication() { - $app = require __DIR__.'/../bootstrap/app.php'; + $app = require __DIR__ . '/../bootstrap/app.php'; $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); return $app; } + + /** + * @return array + */ + public function dateRangeProvider() + { + return [ + 'one day' => ['1D'], + 'one week' => ['1W'], + 'one month' => ['1M'], + 'three months' => ['3M'], + 'six months' => ['6M'], + 'one year' => ['1Y'], + 'custom range' => ['custom'], + ]; + } + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + public function setUp() + { + parent::setUp(); + + } + + + /** + * @return User + */ + public function user() + { + $user = User::find(1); + + return $user; + } + + /** + * @param string $class + * + * @return \Mockery\MockInterface + */ + protected function mock($class) + { + $object = Mockery::mock($class); + + + $this->app->instance($class, $object); + + return $object; + } } diff --git a/tests/acceptance/Controllers/HomeControllerTest.php b/tests/acceptance/Controllers/HomeControllerTest.php new file mode 100755 index 0000000000..7d4abdcdc8 --- /dev/null +++ b/tests/acceptance/Controllers/HomeControllerTest.php @@ -0,0 +1,63 @@ +assertTrue(true); + } + + /** + * @covers FireflyIII\Http\Controllers\HomeController::dateRange + * @covers FireflyIII\Http\Controllers\HomeController::__construct + */ + public function testDateRange() + { + + $this->be($this->user()); + + $args = [ + 'start' => '2012-01-01', + 'end' => '2012-04-01', + ]; + + // if date range is > 50, should have flash. + $this->call('POST', '/daterange', $args); + $this->assertResponseStatus(200); + $this->assertSessionHas('warning', '91 days of data may take a while to load.'); + } + + /** + * @covers FireflyIII\Http\Controllers\HomeController::flush + */ + public function testFlush() + { + $this->be($this->user()); + $this->call('GET', '/flush'); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\HomeController::index + * @covers FireflyIII\Http\Controllers\Controller::__construct + * @dataProvider dateRangeProvider + * + * @param $range + */ + public function testIndex($range) + { + $this->be($this->user()); + $this->changeDateRange($this->user(), $range); + $this->call('GET', '/'); + $this->assertResponseStatus(200); + } +} diff --git a/tests/unit/Models/TransactionTypeTest.php b/tests/unit/Models/TransactionTypeTest.php new file mode 100755 index 0000000000..e0c3f2aa86 --- /dev/null +++ b/tests/unit/Models/TransactionTypeTest.php @@ -0,0 +1,43 @@ +first(); + $this->assertTrue($transactionType->isDeposit()); + } + + public function testIsOpeningBalance() + { + $transactionType = TransactionType::whereType(TransactionType::OPENING_BALANCE)->first(); + $this->assertTrue($transactionType->isOpeningBalance()); + } + + public function testIsTransfer() + { + $transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first(); + $this->assertTrue($transactionType->isTransfer()); + } + + public function testIsWithdrawal() + { + $transactionType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + $this->assertTrue($transactionType->isWithdrawal()); + } +}