-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement unit testing for this plugin (#328)
Huge credit to @scottbedard
- Loading branch information
1 parent
cd2de68
commit 2f8c6af
Showing
7 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
composer.lock | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
language: php | ||
|
||
php: | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
- nightly | ||
|
||
matrix: | ||
allow_failures: | ||
- php: nightly | ||
|
||
before_script: | ||
- wget https://getcomposer.org/composer.phar | ||
- php composer.phar install --no-interaction | ||
- composer self-update | ||
- git clone https://github.com/octobercms/october.git | ||
- mkdir -p ./october/plugins/rainlab/user | ||
- mv !(october) october/plugins/rainlab/user | ||
- cd october | ||
- composer install | ||
- cp config/cms.php config/testing/cms.php | ||
- cd ./plugins/rainlab/user | ||
|
||
script: | ||
- php vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="../../../tests/bootstrap.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false"> | ||
<testsuites> | ||
<testsuite name="rainlab.user"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
<env name="CACHE_DRIVER" value="array"/> | ||
<env name="SESSION_DRIVER" value="array"/> | ||
</php> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./classes</directory> | ||
<directory suffix=".php">./components</directory> | ||
<directory suffix=".php">./controllers</directory> | ||
<directory suffix=".php">./facades</directory> | ||
<directory suffix=".php">./models</directory> | ||
<directory suffix=".php">./notifyrules</directory> | ||
<exclude> | ||
<file>./Plugin.php</file> | ||
<directory>./tests</directory> | ||
<directory>./updates</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php namespace RainLab\User\Tests; | ||
|
||
use App; | ||
use Artisan; | ||
use Illuminate\Foundation\AliasLoader; | ||
use PluginTestCase as BasePluginTestCase; | ||
use RainLab\User\Models\Settings; | ||
|
||
abstract class PluginTestCase extends BasePluginTestCase | ||
{ | ||
/** | ||
* @var array Plugins to refresh between tests. | ||
*/ | ||
protected $refreshPlugins = [ | ||
'RainLab.User', | ||
]; | ||
|
||
/** | ||
* Perform test case set up. | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
// reset any modified settings | ||
Settings::resetDefault(); | ||
|
||
// log out after each test | ||
\RainLab\User\Classes\AuthManager::instance()->logout(); | ||
|
||
// register the auth facade | ||
$alias = AliasLoader::getInstance(); | ||
$alias->alias('Auth', 'RainLab\User\Facades\Auth'); | ||
|
||
App::singleton('user.auth', function() { | ||
return \RainLab\User\Classes\AuthManager::instance(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php namespace RainLab\User\Tests\Unit\Facades; | ||
|
||
use Auth; | ||
use RainLab\User\Models\User; | ||
use RainLab\User\Tests\PluginTestCase; | ||
|
||
class AuthFacadeTest extends PluginTestCase | ||
{ | ||
public function test_registering_a_user() | ||
{ | ||
// register a user | ||
$user = Auth::register([ | ||
'name' => 'Some User', | ||
'email' => '[email protected]', | ||
'password' => 'changeme', | ||
'password_confirmation' => 'changeme', | ||
]); | ||
|
||
// our one user should be returned | ||
$this->assertEquals(1, User::count()); | ||
$this->assertInstanceOf('RainLab\User\Models\User', $user); | ||
|
||
// and that user should have the following data | ||
$this->assertFalse($user->is_activated); | ||
$this->assertEquals('Some User', $user->name); | ||
$this->assertEquals('[email protected]', $user->email); | ||
} | ||
|
||
public function test_registering_a_user_with_auto_activation() | ||
{ | ||
// register a user with the auto-activate flag | ||
$user = Auth::register([ | ||
'name' => 'Some User', | ||
'email' => '[email protected]', | ||
'password' => 'changeme', | ||
'password_confirmation' => 'changeme', | ||
], true); | ||
|
||
// that user should be activated | ||
$this->assertTrue($user->is_activated); | ||
|
||
// and we should now be authenticated | ||
$this->assertTrue(Auth::check()); | ||
} | ||
|
||
public function test_registering_a_guest() | ||
{ | ||
// register a guest | ||
$guest = Auth::registerGuest(['email' => '[email protected]']); | ||
|
||
// our one guest should be returned | ||
$this->assertEquals(1, User::count()); | ||
$this->assertInstanceOf('RainLab\User\Models\User', $guest); | ||
|
||
// and that guest should have the following data | ||
$this->assertTrue($guest->is_guest); | ||
$this->assertEquals('[email protected]', $guest->email); | ||
} | ||
|
||
public function test_login_and_checking_authentication() | ||
{ | ||
// we should not be authenticated | ||
$this->assertFalse(Auth::check()); | ||
|
||
// create a user | ||
$user = User::create([ | ||
'name' => 'Some User', | ||
'email' => '[email protected]', | ||
'password' => 'changeme', | ||
'password_confirmation' => 'changeme', | ||
]); | ||
|
||
// in order to log in as this user, we must be activated | ||
$user->is_activated = true; | ||
$user->activated_at = now(); | ||
$user->save(); | ||
|
||
// log in as a new user | ||
Auth::login($user); | ||
|
||
// we should now be authenticated | ||
$this->assertTrue(Auth::check()); | ||
} | ||
} |