From 33a2999a57854495fcea8c1acf1b75b04a28a0bf Mon Sep 17 00:00:00 2001 From: Dan Brown <ssddanbrown@googlemail.com> Date: Sat, 4 Feb 2017 11:58:42 +0000 Subject: [PATCH] Namespaced tests to align with new laravel default --- composer.json | 7 +++--- tests/ActivityTrackingTest.php | 2 +- tests/AttachmentTest.php | 3 +-- tests/Auth/AuthTest.php | 7 +++--- tests/Auth/LdapTest.php | 10 ++++---- tests/Auth/SocialAuthTest.php | 18 +++++++------- tests/BrowserKitTest.php | 21 ++++++++-------- tests/CreatesApplication.php | 18 ++++++++++++++ tests/Entity/EntitySearchTest.php | 2 +- tests/Entity/EntityTest.php | 2 +- tests/Entity/MarkdownTest.php | 2 +- tests/Entity/PageContentTest.php | 2 +- tests/Entity/PageDraftTest.php | 2 +- tests/Entity/SortTest.php | 2 +- tests/Entity/TagTest.php | 2 +- tests/ImageTest.php | 2 +- tests/Permissions/RestrictionsTest.php | 2 +- tests/Permissions/RolesTest.php | 2 +- tests/PublicActionTest.php | 2 +- tests/TestCase.php | 33 ++++---------------------- tests/UserProfileTest.php | 10 ++++---- 21 files changed, 71 insertions(+), 80 deletions(-) create mode 100644 tests/CreatesApplication.php diff --git a/composer.json b/composer.json index 959bdbdc2..5755afffe 100644 --- a/composer.json +++ b/composer.json @@ -36,10 +36,9 @@ } }, "autoload-dev": { - "classmap": [ - "tests/TestCase.php", - "tests/BrowserKitTest.php" - ] + "psr-4": { + "Tests\\": "tests/" + } }, "scripts": { "post-root-package-install": [ diff --git a/tests/ActivityTrackingTest.php b/tests/ActivityTrackingTest.php index 137317996..bb65fbcf4 100644 --- a/tests/ActivityTrackingTest.php +++ b/tests/ActivityTrackingTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class ActivityTrackingTest extends BrowserKitTest diff --git a/tests/AttachmentTest.php b/tests/AttachmentTest.php index f99051a72..a17f003db 100644 --- a/tests/AttachmentTest.php +++ b/tests/AttachmentTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class AttachmentTest extends BrowserKitTest { @@ -75,7 +75,6 @@ class AttachmentTest extends BrowserKitTest { $page = \BookStack\Page::first(); $this->asAdmin(); - $admin = $this->getAdmin(); $fileName = 'upload_test_file.txt'; $this->uploadFile($fileName, $page->id); diff --git a/tests/Auth/AuthTest.php b/tests/Auth/AuthTest.php index f75958838..4de35faff 100644 --- a/tests/Auth/AuthTest.php +++ b/tests/Auth/AuthTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; use BookStack\Notifications\ConfirmEmail; use Illuminate\Support\Facades\Notification; @@ -88,7 +88,7 @@ class AuthTest extends BrowserKitTest ->press('Resend Confirmation Email'); // Get confirmation and confirm notification matches - $emailConfirmation = DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first(); + $emailConfirmation = \DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first(); Notification::assertSentTo($dbUser, ConfirmEmail::class, function($notification, $channels) use ($emailConfirmation) { return $notification->token === $emailConfirmation->token; }); @@ -177,7 +177,7 @@ class AuthTest extends BrowserKitTest ->seePageIs('/settings/users'); $userPassword = \BookStack\User::find($user->id)->password; - $this->assertTrue(Hash::check('newpassword', $userPassword)); + $this->assertTrue(\Hash::check('newpassword', $userPassword)); } public function test_user_deletion() @@ -238,7 +238,6 @@ class AuthTest extends BrowserKitTest Notification::assertSentTo($user, \BookStack\Notifications\ResetPassword::class); $n = Notification::sent($user, \BookStack\Notifications\ResetPassword::class); - $reset = DB::table('password_resets')->where('email', '=', 'admin@admin.com')->first(); $this->visit('/password/reset/' . $n->first()->token) ->see('Reset Password') ->submitForm('Reset Password', [ diff --git a/tests/Auth/LdapTest.php b/tests/Auth/LdapTest.php index 80d0c9fe2..681ead91c 100644 --- a/tests/Auth/LdapTest.php +++ b/tests/Auth/LdapTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; use BookStack\User; class LdapTest extends BrowserKitTest @@ -12,7 +12,7 @@ class LdapTest extends BrowserKitTest { parent::setUp(); app('config')->set(['auth.method' => 'ldap', 'services.ldap.base_dn' => 'dc=ldap,dc=local', 'auth.providers.users.driver' => 'ldap']); - $this->mockLdap = Mockery::mock(BookStack\Services\Ldap::class); + $this->mockLdap = \Mockery::mock(\BookStack\Services\Ldap::class); $this->app['BookStack\Services\Ldap'] = $this->mockLdap; $this->mockUser = factory(User::class)->make(); } @@ -22,7 +22,7 @@ class LdapTest extends BrowserKitTest $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId); $this->mockLdap->shouldReceive('setVersion')->once(); $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4) - ->with($this->resourceId, config('services.ldap.base_dn'), Mockery::type('string'), Mockery::type('array')) + ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array')) ->andReturn(['count' => 1, 0 => [ 'uid' => [$this->mockUser->name], 'cn' => [$this->mockUser->name], @@ -50,7 +50,7 @@ class LdapTest extends BrowserKitTest $this->mockLdap->shouldReceive('setVersion')->once(); $ldapDn = 'cn=test-user,dc=test' . config('services.ldap.base_dn'); $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2) - ->with($this->resourceId, config('services.ldap.base_dn'), Mockery::type('string'), Mockery::type('array')) + ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array')) ->andReturn(['count' => 1, 0 => [ 'cn' => [$this->mockUser->name], 'dn' => $ldapDn, @@ -73,7 +73,7 @@ class LdapTest extends BrowserKitTest $this->mockLdap->shouldReceive('connect')->once()->andReturn($this->resourceId); $this->mockLdap->shouldReceive('setVersion')->once(); $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2) - ->with($this->resourceId, config('services.ldap.base_dn'), Mockery::type('string'), Mockery::type('array')) + ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array')) ->andReturn(['count' => 1, 0 => [ 'uid' => [$this->mockUser->name], 'cn' => [$this->mockUser->name], diff --git a/tests/Auth/SocialAuthTest.php b/tests/Auth/SocialAuthTest.php index aff86a81e..e3494d073 100644 --- a/tests/Auth/SocialAuthTest.php +++ b/tests/Auth/SocialAuthTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class SocialAuthTest extends BrowserKitTest { @@ -11,10 +11,10 @@ class SocialAuthTest extends BrowserKitTest $this->setSettings(['registration-enabled' => 'true']); config(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']); - $mockSocialite = Mockery::mock('Laravel\Socialite\Contracts\Factory'); + $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory'); $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite; - $mockSocialDriver = Mockery::mock('Laravel\Socialite\Contracts\Provider'); - $mockSocialUser = Mockery::mock('\Laravel\Socialite\Contracts\User'); + $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider'); + $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User'); $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver); $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/')); @@ -34,18 +34,16 @@ class SocialAuthTest extends BrowserKitTest public function test_social_login() { - $user = factory(\BookStack\User::class)->make(); - config([ 'GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'GITHUB_APP_ID' => 'abc123', 'GITHUB_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost' ]); - $mockSocialite = Mockery::mock('Laravel\Socialite\Contracts\Factory'); + $mockSocialite = \Mockery::mock('Laravel\Socialite\Contracts\Factory'); $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite; - $mockSocialDriver = Mockery::mock('Laravel\Socialite\Contracts\Provider'); - $mockSocialUser = Mockery::mock('\Laravel\Socialite\Contracts\User'); + $mockSocialDriver = \Mockery::mock('Laravel\Socialite\Contracts\Provider'); + $mockSocialUser = \Mockery::mock('\Laravel\Socialite\Contracts\User'); $mockSocialUser->shouldReceive('getId')->twice()->andReturn('logintest123'); @@ -68,7 +66,7 @@ class SocialAuthTest extends BrowserKitTest ->seePageIs('/login'); // Test social callback with matching social account - DB::table('social_accounts')->insert([ + \DB::table('social_accounts')->insert([ 'user_id' => $this->getAdmin()->id, 'driver' => 'github', 'driver_id' => 'logintest123' diff --git a/tests/BrowserKitTest.php b/tests/BrowserKitTest.php index 0a97c1292..674aef984 100644 --- a/tests/BrowserKitTest.php +++ b/tests/BrowserKitTest.php @@ -1,9 +1,12 @@ -<?php +<?php namespace Tests; +use BookStack\Role; +use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Laravel\BrowserKitTesting\TestCase; use Symfony\Component\DomCrawler\Crawler; -abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase +abstract class BrowserKitTest extends TestCase { use DatabaseTransactions; @@ -28,7 +31,7 @@ abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase { $app = require __DIR__.'/../bootstrap/app.php'; - $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); + $app->make(Kernel::class)->bootstrap(); return $app; } @@ -48,7 +51,7 @@ abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase */ public function getAdmin() { if($this->admin === null) { - $adminRole = \BookStack\Role::getRole('admin'); + $adminRole = Role::getRole('admin'); $this->admin = $adminRole->users->first(); } return $this->admin; @@ -95,9 +98,9 @@ abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase protected function createEntityChainBelongingToUser($creatorUser, $updaterUser = false) { if ($updaterUser === false) $updaterUser = $creatorUser; - $book = factory(BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); - $chapter = factory(BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); - $page = factory(BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]); + $book = factory(\BookStack\Book::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); + $chapter = factory(\BookStack\Chapter::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id]); + $page = factory(\BookStack\Page::class)->create(['created_by' => $creatorUser->id, 'updated_by' => $updaterUser->id, 'book_id' => $book->id]); $book->chapters()->saveMany([$chapter]); $chapter->pages()->saveMany([$page]); $restrictionService = $this->app[\BookStack\Services\PermissionService::class]; @@ -117,7 +120,7 @@ abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase protected function getEditor($attributes = []) { $user = factory(\BookStack\User::class)->create($attributes); - $role = \BookStack\Role::getRole('editor'); + $role = Role::getRole('editor'); $user->attachRole($role);; return $user; } @@ -211,7 +214,6 @@ abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase /** * Check if the page contains the given element. * @param string $selector - * @return bool */ protected function pageHasElement($selector) { @@ -223,7 +225,6 @@ abstract class BrowserKitTest extends \Laravel\BrowserKitTesting\TestCase /** * Check if the page contains the given element. * @param string $selector - * @return bool */ protected function pageNotHasElement($selector) { diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php new file mode 100644 index 000000000..42a5da2d1 --- /dev/null +++ b/tests/CreatesApplication.php @@ -0,0 +1,18 @@ +<?php namespace Tests; + +use Illuminate\Contracts\Console\Kernel; + +trait CreatesApplication +{ + /** + * Creates the application. + * + * @return \Illuminate\Foundation\Application + */ + public function createApplication() + { + $app = require __DIR__.'/../bootstrap/app.php'; + $app->make(Kernel::class)->bootstrap(); + return $app; + } +} \ No newline at end of file diff --git a/tests/Entity/EntitySearchTest.php b/tests/Entity/EntitySearchTest.php index 23351e546..4ef8d46fb 100644 --- a/tests/Entity/EntitySearchTest.php +++ b/tests/Entity/EntitySearchTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class EntitySearchTest extends BrowserKitTest { diff --git a/tests/Entity/EntityTest.php b/tests/Entity/EntityTest.php index 1b115ad65..5fa331737 100644 --- a/tests/Entity/EntityTest.php +++ b/tests/Entity/EntityTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class EntityTest extends BrowserKitTest { diff --git a/tests/Entity/MarkdownTest.php b/tests/Entity/MarkdownTest.php index 03f3aa12f..b85e92565 100644 --- a/tests/Entity/MarkdownTest.php +++ b/tests/Entity/MarkdownTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class MarkdownTest extends BrowserKitTest { diff --git a/tests/Entity/PageContentTest.php b/tests/Entity/PageContentTest.php index 3a005bc67..4f65d3406 100644 --- a/tests/Entity/PageContentTest.php +++ b/tests/Entity/PageContentTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class PageContentTest extends BrowserKitTest { diff --git a/tests/Entity/PageDraftTest.php b/tests/Entity/PageDraftTest.php index de875c3f3..b8ac9d3ae 100644 --- a/tests/Entity/PageDraftTest.php +++ b/tests/Entity/PageDraftTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class PageDraftTest extends BrowserKitTest diff --git a/tests/Entity/SortTest.php b/tests/Entity/SortTest.php index 3c6400d58..d61b3f56f 100644 --- a/tests/Entity/SortTest.php +++ b/tests/Entity/SortTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class SortTest extends BrowserKitTest { diff --git a/tests/Entity/TagTest.php b/tests/Entity/TagTest.php index f4ce581e3..257c20789 100644 --- a/tests/Entity/TagTest.php +++ b/tests/Entity/TagTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; use BookStack\Tag; use BookStack\Page; diff --git a/tests/ImageTest.php b/tests/ImageTest.php index debde848c..3bb41138b 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class ImageTest extends BrowserKitTest { diff --git a/tests/Permissions/RestrictionsTest.php b/tests/Permissions/RestrictionsTest.php index e874fccae..7007985e4 100644 --- a/tests/Permissions/RestrictionsTest.php +++ b/tests/Permissions/RestrictionsTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class RestrictionsTest extends BrowserKitTest { diff --git a/tests/Permissions/RolesTest.php b/tests/Permissions/RolesTest.php index a4b502915..24b8ae0f5 100644 --- a/tests/Permissions/RolesTest.php +++ b/tests/Permissions/RolesTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class RolesTest extends BrowserKitTest { diff --git a/tests/PublicActionTest.php b/tests/PublicActionTest.php index 6127e1cd4..422511dbf 100644 --- a/tests/PublicActionTest.php +++ b/tests/PublicActionTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class PublicActionTest extends BrowserKitTest { diff --git a/tests/TestCase.php b/tests/TestCase.php index e0455f447..6b97682e8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,31 +1,8 @@ -<?php +<?php namespace Tests; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use Symfony\Component\DomCrawler\Crawler; +use Illuminate\Foundation\Testing\TestCase as BaseTestCase; -class TestCase extends Illuminate\Foundation\Testing\TestCase +abstract class TestCase extends BaseTestCase { - - use DatabaseTransactions; - - /** - * The base URL of the application. - * - * @var string - */ - public $baseUrl = 'http://localhost'; - - /** - * Creates the application. - * - * @return \Illuminate\Foundation\Application - */ - public function createApplication() - { - $app = require __DIR__.'/../bootstrap/app.php'; - - $app->make(Kernel::class)->bootstrap(); - - return $app; - } -} + use CreatesApplication; +} \ No newline at end of file diff --git a/tests/UserProfileTest.php b/tests/UserProfileTest.php index a448e3a9f..12f88483f 100644 --- a/tests/UserProfileTest.php +++ b/tests/UserProfileTest.php @@ -1,4 +1,4 @@ -<?php +<?php namespace Tests; class UserProfileTest extends BrowserKitTest { @@ -55,8 +55,8 @@ class UserProfileTest extends BrowserKitTest $newUser = $this->getEditor(); $this->actingAs($newUser); $entities = $this->createEntityChainBelongingToUser($newUser, $newUser); - Activity::add($entities['book'], 'book_update', $entities['book']->id); - Activity::add($entities['page'], 'page_create', $entities['book']->id); + \Activity::add($entities['book'], 'book_update', $entities['book']->id); + \Activity::add($entities['page'], 'page_create', $entities['book']->id); $this->asAdmin()->visit('/user/' . $newUser->id) ->seeInElement('#recent-activity', 'updated book') @@ -69,8 +69,8 @@ class UserProfileTest extends BrowserKitTest $newUser = $this->getEditor(); $this->actingAs($newUser); $entities = $this->createEntityChainBelongingToUser($newUser, $newUser); - Activity::add($entities['book'], 'book_update', $entities['book']->id); - Activity::add($entities['page'], 'page_create', $entities['book']->id); + \Activity::add($entities['book'], 'book_update', $entities['book']->id); + \Activity::add($entities['page'], 'page_create', $entities['book']->id); $this->asAdmin()->visit('/')->clickInElement('#recent-activity', $newUser->name) ->seePageIs('/user/' . $newUser->id)