Added user related factories

main
Igor Scheller 4 years ago committed by msquare
parent 4ff44d141c
commit ec355d40f5

@ -49,6 +49,7 @@
},
"require-dev": {
"dms/phpunit-arraysubset-asserts": "^0.3.0",
"fakerphp/faker": "^1.14.1",
"filp/whoops": "^2.12",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6",
@ -65,6 +66,7 @@
},
"autoload-dev": {
"psr-4": {
"Database\\Factories\\Engelsystem\\Models\\": "db/factories/",
"Engelsystem\\Test\\": "tests/"
}
}

437
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,24 @@
<?php
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;
class ContactFactory extends Factory
{
/** @var string */
protected $model = Contact::class;
/**
* @return array
*/
public function definition()
{
return [
'dect' => $this->faker->optional()->numberBetween(1000, 9999),
'email' => $this->faker->unique()->optional()->safeEmail(),
'mobile' => $this->faker->optional(.2)->phoneNumber(),
];
}
}

@ -0,0 +1,22 @@
<?php
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\PasswordReset;
use Illuminate\Database\Eloquent\Factories\Factory;
class PasswordResetFactory extends Factory
{
/** @var string */
protected $model = PasswordReset::class;
/**
* @return array
*/
public function definition()
{
return [
'token' => md5($this->faker->unique()->password()),
];
}
}

@ -0,0 +1,31 @@
<?php
namespace Database\Factories\Engelsystem\Models\User;
use Carbon\Carbon;
use Engelsystem\Models\User\PersonalData;
use Illuminate\Database\Eloquent\Factories\Factory;
class PersonalDataFactory extends Factory
{
/** @var string */
protected $model = PersonalData::class;
/**
* @return array
*/
public function definition()
{
$arrival = $this->faker->optional()->dateTimeThisMonth('2 weeks');
$departure = $this->faker->optional()->dateTimeThisMonth('2 weeks');
return [
'first_name' => $this->faker->optional(.7)->firstName(),
'last_name' => $this->faker->optional()->lastName(),
'pronoun' => $this->faker->optional(.3)->pronoun(),
'shirt_size' => $this->faker->optional(.9)->shirtSize(),
'planned_arrival_date' => $arrival ? Carbon::instance($arrival) : null,
'planned_departure_date' => $departure ? Carbon::instance($departure) : null,
];
}
}

@ -0,0 +1,26 @@
<?php
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\Settings;
use Illuminate\Database\Eloquent\Factories\Factory;
class SettingsFactory extends Factory
{
/** @var string */
protected $model = Settings::class;
/**
* @return array
*/
public function definition()
{
return [
'language' => $this->faker->locale(),
'theme' => $this->faker->numberBetween(1, 20),
'email_human' => $this->faker->boolean(),
'email_shiftinfo' => $this->faker->boolean(),
'email_news' => $this->faker->boolean(),
];
}
}

@ -0,0 +1,47 @@
<?php
namespace Database\Factories\Engelsystem\Models\User;
use Carbon\Carbon;
use Engelsystem\Models\User\State;
use Illuminate\Database\Eloquent\Factories\Factory;
class StateFactory extends Factory
{
/** @var string */
protected $model = State::class;
/**
* @return array
*/
public function definition()
{
$arrival = $this->faker->optional()->dateTimeThisMonth();
return [
'arrived' => (bool)$arrival,
'arrival_date' => $arrival ? Carbon::instance($arrival) : null,
'active' => $this->faker->boolean(.3),
'force_active' => $this->faker->boolean(.1),
'got_shirt' => $this->faker->boolean(),
'got_voucher' => $this->faker->numberBetween(0, 10),
];
}
/**
* Indicate that the user is arrived
*
* @return self
*/
public function arrived()
{
return $this->state(
function (array $attributes) {
return [
'arrived' => true,
'arrival_date' => Carbon::instance($this->faker->dateTimeThisMonth()),
];
}
);
}
}

@ -0,0 +1,25 @@
<?php
namespace Database\Factories\Engelsystem\Models\User;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory
{
/** @var string */
protected $model = User::class;
/**
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->unique()->userName(),
'password' => password_hash($this->faker->password(), PASSWORD_DEFAULT),
'email' => $this->faker->unique()->safeEmail(),
'api_key' => md5($this->faker->unique()->password()),
];
}
}

@ -6,6 +6,7 @@ use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Container\ServiceProvider;
use Illuminate\Container\Container as IlluminateContainer;
use Illuminate\Contracts\Container\Container as IlluminateContainerContract;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;
@ -50,6 +51,7 @@ class Application extends Container
$this->instance(Container::class, $this);
$this->instance(Application::class, $this);
$this->instance(IlluminateContainer::class, $this);
$this->instance(IlluminateContainerContract::class, $this);
$this->bind(ContainerInterface::class, self::class);
}

@ -334,7 +334,7 @@ class Stats
$query->having($this->raw($having), '<=', $bucket);
}
$return[$bucket] = $query->count($this->raw($count));
$return[$bucket] = count($query->get($this->raw($count)));
}
return $return;

@ -2,6 +2,7 @@
namespace Engelsystem\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
@ -15,6 +16,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
*/
class Contact extends HasUserModel
{
use HasFactory;
/** @var string The table associated with the model */
protected $table = 'users_contact';

@ -3,6 +3,7 @@
namespace Engelsystem\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
@ -14,6 +15,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
*/
class PasswordReset extends HasUserModel
{
use HasFactory;
/** @var bool enable timestamps for created_at */
public $timestamps = true;

@ -3,6 +3,7 @@
namespace Engelsystem\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
@ -22,6 +23,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
*/
class PersonalData extends HasUserModel
{
use HasFactory;
/** @var string The table associated with the model */
protected $table = 'users_personal_data';

@ -2,6 +2,7 @@
namespace Engelsystem\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
@ -19,6 +20,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
*/
class Settings extends HasUserModel
{
use HasFactory;
/** @var string The table associated with the model */
protected $table = 'users_settings';

@ -3,6 +3,7 @@
namespace Engelsystem\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
@ -22,6 +23,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
*/
class State extends HasUserModel
{
use HasFactory;
/** @var string The table associated with the model */
protected $table = 'users_state';

@ -11,6 +11,7 @@ use Engelsystem\Models\OAuth;
use Engelsystem\Models\Question;
use Engelsystem\Models\Worklog;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder;
@ -57,6 +58,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
*/
class User extends BaseModel
{
use HasFactory;
/** @var bool enable timestamps */
public $timestamps = true;

@ -283,15 +283,7 @@ class NewsControllerTest extends ControllerTest
*/
protected function addUser()
{
$user = new User([
'name' => 'foo',
'password' => '',
'email' => '',
'api_key' => '',
'last_login_at' => null,
]);
$user->forceFill(['id' => 42]);
$user->save();
$user = User::factory(['id' => 42])->create();
$this->auth->expects($this->any())
->method('user')

@ -241,14 +241,7 @@ class QuestionsControllerTest extends ControllerTest
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
$this->user = new User([
'name' => 'foo',
'password' => '',
'email' => '',
'api_key' => '',
'last_login_at' => null,
]);
$this->user->save();
$this->user = User::factory()->create();
$this->setExpects($this->auth, 'user', null, $this->user, $this->any());
(new Question([

@ -185,22 +185,9 @@ class AuthControllerTest extends TestCase
*/
protected function createUser(): User
{
$user = new User([
'name' => 'foo',
'password' => '',
'email' => '',
'api_key' => '',
'last_login_at' => null,
]);
$user->forceFill(['id' => 42]);
$user->save();
$settings = new Settings(['language' => 'de_DE', 'theme' => '']);
$settings->user()
->associate($user)
->save();
return $user;
return User::factory(['id' => 42])
->has(Settings::factory(['language' => 'de_DE']))
->create();
}
/**

@ -269,15 +269,7 @@ class NewsControllerTest extends TestCase
*/
protected function addUser()
{
$user = new User([
'name' => 'foo',
'password' => '',
'email' => '',
'api_key' => '',
'last_login_at' => null,
]);
$user->forceFill(['id' => 42]);
$user->save();
$user = User::factory()->create(['id' => 42]);
$this->auth->expects($this->any())
->method('user')

@ -494,36 +494,15 @@ class OAuthControllerTest extends TestCase
$this->app->instance('session', $this->session);
$this->authenticatedUser = new User([
'name' => 'foo',
'password' => '',
'email' => 'foo@localhost',
'api_key' => '',
'last_login_at' => null,
]);
$this->authenticatedUser->save();
$this->authenticatedUser = User::factory()->create();
(new OAuth(['provider' => 'testprovider', 'identifier' => 'provider-user-identifier']))
->user()
->associate($this->authenticatedUser)
->save();
$this->otherUser = new User([
'name' => 'bar',
'password' => '',
'email' => 'bar@localhost',
'api_key' => '',
'last_login_at' => null,
]);
$this->otherUser->save();
$this->otherAuthenticatedUser = new User([
'name' => 'baz',
'password' => '',
'email' => 'baz@localhost',
'api_key' => '',
'last_login_at' => null,
]);
$this->otherAuthenticatedUser->save();
$this->otherUser = User::factory()->create();
$this->otherAuthenticatedUser = User::factory()->create();
(new OAuth(['provider' => 'testprovider', 'identifier' => 'provider-baz-identifier']))
->user()
->associate($this->otherAuthenticatedUser)

@ -256,15 +256,7 @@ class PasswordResetControllerTest extends TestCase
*/
protected function createUser(): User
{
$user = new User([
'name' => 'foo',
'password' => '',
'email' => 'foo@bar.batz',
'api_key' => '',
]);
$user->save();
return $user;
return User::factory()->create(['email' => 'foo@bar.batz']);
}
/**

@ -90,14 +90,7 @@ class QuestionsControllerTest extends ControllerTest
*/
public function testDeleteNotOwn()
{
$otherUser = new User([
'name' => 'bar',
'password' => '',
'email' => '.',
'api_key' => '',
'last_login_at' => null,
]);
$otherUser->save();
$otherUser = User::factory()->create();
(new Question([
'user_id' => $otherUser->id,
'text' => 'Lorem?',
@ -189,14 +182,7 @@ class QuestionsControllerTest extends ControllerTest
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
$this->user = new User([
'name' => 'foo',
'password' => '',
'email' => '',
'api_key' => '',
'last_login_at' => null,
]);
$this->user->save();
$this->user = User::factory()->create();
$this->setExpects($this->auth, 'user', null, $this->user, $this->any());
(new Question([

@ -313,12 +313,6 @@ class SettingsControllerTest extends TestCase
$this->auth = $this->createMock(Authenticator::class);
$this->app->instance(Authenticator::class, $this->auth);
$this->user = new User([
'name' => 'testuser',
'email' => 'test@engelsystem.de',
'password' => 'xxx',
'api_key' => 'xxx'
]);
$this->user->save();
$this->user = User::factory()->create();
}
}

@ -3,7 +3,6 @@
namespace Engelsystem\Test\Unit\Events\Listener;
use Engelsystem\Events\Listener\News;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Mail\EngelsystemMailer;
use Engelsystem\Models\News as NewsModel;
use Engelsystem\Models\User\Settings;
@ -80,22 +79,12 @@ class NewsTest extends TestCase
$this->mailer = $this->createMock(EngelsystemMailer::class);
$this->app->instance(EngelsystemMailer::class, $this->mailer);
$this->user = new User([
'name' => 'test',
'password' => '',
'email' => 'foo@bar.baz',
'api_key' => '',
]);
$this->user->save();
$settings = new Settings([
$this->user = User::factory()
->has(Settings::factory([
'language' => '',
'theme' => 1,
'email_news' => true,
]);
$settings->user()
->associate($this->user)
->save();
]))
->create();
}
}

@ -0,0 +1,30 @@
<?php
namespace Engelsystem\Test\Unit;
use Faker\Provider\Base;
class FakerProvider extends Base
{
/** @var string[] */
protected static $pronouns = ['fae', 'ae', 'e', 'ey', 'he', 'per', 'she', 'tey', 'they', 've', 'xe', 'ze', 'zie'];
/** @var string[] */
protected static $shirtSizes = ['S', 'S-G', 'M', 'M-G', 'L', 'L-G', 'XL', 'XL-G', '2XL', '3XL', '4XL'];
/**
* @return string
*/
public function pronoun()
{
return static::randomElement(static::$pronouns);
}
/**
* @return string
*/
public function shirtSize(): string
{
return static::randomElement(static::$shirtSizes);
}
}

@ -165,18 +165,15 @@ class AuthenticatorTest extends ServiceProviderTest
$session = $this->createMock(Session::class);
$userRepository = new User();
(new User([
User::factory([
'name' => 'lorem',
'password' => password_hash('testing', PASSWORD_DEFAULT),
'email' => 'lorem@foo.bar',
'api_key' => '',
]))->save();
(new User([
])->create();
User::factory([
'name' => 'ipsum',
'password' => '',
'email' => 'ipsum@foo.bar',
'api_key' => '',
]))->save();
])->create();
$auth = new Authenticator($request, $session, $userRepository);
$this->assertNull($auth->authenticate('not-existing', 'foo'));
@ -192,13 +189,11 @@ class AuthenticatorTest extends ServiceProviderTest
{
$this->initDatabase();
$password = password_hash('testing', PASSWORD_ARGON2I);
$user = new User([
/** @var User $user */
$user = User::factory([
'name' => 'lorem',
'password' => $password,
'email' => 'lorem@foo.bar',
'api_key' => '',
]);
$user->save();
])->create();
/** @var Authenticator|MockObject $auth */
$auth = $this->getMockBuilder(Authenticator::class)
@ -221,12 +216,11 @@ class AuthenticatorTest extends ServiceProviderTest
public function testSetPassword()
{
$this->initDatabase();
$user = new User([
/** @var User $user */
$user = User::factory([
'name' => 'ipsum',
'password' => '',
'email' => 'ipsum@foo.bar',
'api_key' => '',
]);
])->create();
$user->save();
$auth = $this->getAuthenticator();

@ -18,7 +18,7 @@ class UserAwareLoggerTest extends ServiceProviderTest
*/
public function testLog()
{
$user = (new User())->forceFill(['id' => 1, 'name' => 'admin']);
$user = User::factory(['id' => 1, 'name' => 'admin'])->make();
/** @var LogEntry|MockObject $logEntry */
$logEntry = $this->getMockBuilder(LogEntry::class)

@ -47,21 +47,10 @@ class EngelsystemMailerTest extends TestCase
{
$this->initDatabase();
$settings = new Settings([
'language' => 'de_DE',
'theme' => '',
]);
$contact = new Contact(['email' => null]);
$user = new User([
'id' => 42,
'name' => 'username',
'email' => 'foo@bar.baz',
'password' => '',
'api_key' => '',
]);
$user->save();
$settings->user()->associate($user)->save();
$contact->user()->associate($user)->save();
$user = User::factory(['email' => 'foo@bar.baz'])
->has(Settings::factory(['language' => 'de_DE']))
->has(Contact::factory(['email' => null]))
->create();
/** @var Renderer|MockObject $view */
$view = $this->createMock(Renderer::class);

@ -40,15 +40,13 @@ class SetLocaleTest extends TestCase
/** @var ResponseInterface|MockObject $response */
$response = $this->getMockForAbstractClass(ResponseInterface::class);
$user = User::create([
/** @var User $user */
$user = User::factory([
'name' => 'user',
'password' => '',
'email' => 'foo@bar.baz',
'api_key' => '',
]);
$settings = new Settings(['language' => 'uf_UF', 'theme' => '']);
$settings->user()->associate($user);
$settings->save();
])
->has(Settings::factory(['language' => 'uf_UF']))
->create();
$locale = 'te_ST';

@ -34,19 +34,8 @@ class MessageTest extends ModelTest
{
parent::setUp();
$this->user1 = User::create([
'name' => 'user1',
'password' => '',
'email' => 'user1@example.com',
'api_key' => '',
]);
$this->user2 = User::create([
'name' => 'user2',
'password' => '',
'email' => 'user2@example.com',
'api_key' => '',
]);
$this->user1 = User::factory()->create();
$this->user2 = User::factory()->create();
$this->message1 = Message::create([
'user_id' => $this->user1->id,

@ -29,12 +29,7 @@ class NewsCommentsTest extends ModelTest
{
parent::setUp();
$this->user = User::create([
'name' => 'lorem',
'password' => '',
'email' => 'lorem@example.com',
'api_key' => '',
]);
$this->user = User::factory()->create();
$this->news = News::create([
'title' => 'test title',

@ -25,13 +25,7 @@ class NewsTest extends ModelTest
{
parent::setUp();
$this->user = (new User())->create([
'name' => 'lorem',
'password' => '',
'email' => 'foo@bar.batz',
'api_key' => '',
]);
$this->user = User::factory()->create();
$this->newsData = [
'title' => 'test title',
'text' => 'test text',

@ -28,23 +28,8 @@ class QuestionTest extends ModelTest
{
parent::setUp();
$this->user1 = User::create(
[
'name' => 'user1',
'password' => '',
'email' => 'user1@example.com',
'api_key' => '',
]
);
$this->user2 = User::create(
[
'name' => 'user2',
'password' => '',
'email' => 'user2@example.com',
'api_key' => '',
]
);
$this->user1 = User::factory()->create();
$this->user2 = User::factory()->create();
}
/**

@ -15,18 +15,8 @@ class WorklogTest extends ModelTest
*/
public function testCreator(): void
{
$user1 = User::create([
'name' => 'user1',
'password' => '',
'email' => 'user1@example.com',
'api_key' => '',
]);
$user2 = User::create([
'name' => 'user2',
'password' => '',
'email' => 'user2@example.com',
'api_key' => '',
]);
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$worklog = new Worklog();
$worklog->user()->associate($user1);

@ -34,7 +34,7 @@ class AuthenticationTest extends ExtensionTest
{
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
$user = new User();
$user = User::factory()->make();
$auth->expects($this->exactly(4))
->method('user')

@ -3,9 +3,9 @@
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Test\Unit\TestCase;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Twig\Node\Node as TwigNode;
use Twig\TwigFunction;

@ -20,7 +20,7 @@ class GlobalsTest extends ExtensionTest
$auth = $this->createMock(Authenticator::class);
/** @var Request|MockObject $request */
$request = $this->createMock(Request::class);
$user = new User();
$user = User::factory()->make();
$auth->expects($this->exactly(2))
->method('user')

@ -3,6 +3,8 @@
namespace Engelsystem\Test\Unit;
use Engelsystem\Application;
use Faker\Factory as FakerFactory;
use Faker\Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
@ -45,5 +47,9 @@ abstract class TestCase extends PHPUnitTestCase
protected function setUp(): void
{
$this->app = new Application(__DIR__ . '/../../');
$faker = FakerFactory::create();
$faker->addProvider(new FakerProvider($faker));
$this->app->instance(Generator::class, $faker);
}
}

Loading…
Cancel
Save