diff options
author | Marvin Borner | 2018-06-08 20:03:25 +0200 |
---|---|---|
committer | Marvin Borner | 2018-06-08 20:03:25 +0200 |
commit | 92b7dd3335a6572debeacfb5faa82c63a5e67888 (patch) | |
tree | 7ebbca22595d542ec5e2912a24a0400ac8f6b113 /main/app/sprinkles/core/tests/Unit | |
parent | 22a1bb27f94ea33042b0bdd35bef1a5cfa96cc0d (diff) |
Some minor fixes
Diffstat (limited to 'main/app/sprinkles/core/tests/Unit')
3 files changed, 311 insertions, 311 deletions
diff --git a/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php b/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php index 04d3843..81ef642 100644 --- a/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php +++ b/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php @@ -1,101 +1,101 @@ -<?php -/** - * UserFrosting (http://www.userfrosting.com) - * - * @link https://github.com/userfrosting/UserFrosting - * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) - */ - -namespace UserFrosting\Tests\Unit; - -use Illuminate\Database\Capsule\Manager as DB; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Mockery as m; -use ReflectionClass; -use UserFrosting\Tests\TestCase; -use UserFrosting\Tests\DatabaseTransactions; -use UserFrosting\Sprinkle\Core\Database\Builder as QueryBuilder; -use UserFrosting\Sprinkle\Core\Database\Models\Model; -use UserFrosting\Sprinkle\Core\Database\Relations\BelongsToManyThrough; - -/** - * Tests the BelongsToManyThrough relation. - * - * @extends TestCase - */ -class BelongsToManyThroughTest extends TestCase -{ - public function tearDown() { - m::close(); - } - - function testPaginatedQuery() { - // Creates a real BelongsToManyThrough object - $relation = $this->getRelation(); - - // We need to define a mock base query, because Eloquent\Builder will pass through many calls - // to this underlying Query\Builder object. - $baseQuery = m::mock(QueryBuilder::class); - $builder = m::mock(EloquentBuilder::class, [$baseQuery])->makePartial(); - - $related = $relation->getRelated(); - $related->shouldReceive('getQualifiedKeyName')->once()->andReturn('users.id'); - - $builder->shouldReceive('withGlobalScope')->once()->andReturnSelf(); - - $builder->shouldReceive('limit')->once()->with(2)->andReturnSelf(); - $builder->shouldReceive('offset')->once()->with(1)->andReturnSelf(); - - // Mock the collection generated by the constrained query - $collection = m::mock('Illuminate\Database\Eloquent\Collection'); - $collection->shouldReceive('pluck')->once()->with('id')->andReturn($collection); - $collection->shouldReceive('toArray')->once()->andReturn([1, 2]); - $builder->shouldReceive('get')->once()->andReturn($collection); - - // Test the final modification to the original unpaginated query - $builder->shouldReceive('whereIn')->once()->with('users.id', [1, 2])->andReturnSelf(); - - $paginatedQuery = $relation->getPaginatedQuery($builder, 2, 1); - } - - /** - * Set up and simulate base expectations for arguments to relationship. - */ - protected function getRelation() { - // We simulate a BelongsToManyThrough relationship that gets all related users for a specified permission(s). - $builder = m::mock(EloquentBuilder::class); - $related = m::mock('Illuminate\Database\Eloquent\Model'); - $related->shouldReceive('getKey')->andReturn(1); - $related->shouldReceive('getTable')->andReturn('users'); - $related->shouldReceive('getKeyName')->andReturn('id'); - // Tie the mocked builder to the mocked related model - $builder->shouldReceive('getModel')->andReturn($related); - - // Mock the intermediate role->permission BelongsToMany relation - $intermediateRelationship = m::mock(BelongsToMany::class); - $intermediateRelationship->shouldReceive('getTable')->once()->andReturn('permission_roles'); - $intermediateRelationship->shouldReceive('getQualifiedRelatedKeyName')->once()->andReturn('permission_roles.role_id'); - // Crazy pivot query stuff - $newPivot = m::mock('\Illuminate\Database\Eloquent\Relations\Pivot'); - $newPivot->shouldReceive('getForeignKey')->andReturn('permission_id'); - $intermediateRelationship->shouldReceive('newExistingPivot')->andReturn($newPivot); - - // Expectations for joining the main relation - users to roles - $builder->shouldReceive('join')->once()->with('role_users', 'users.id', '=', 'role_users.user_id'); - - // Expectations for joining the intermediate relation - roles to permissions - $builder->shouldReceive('join')->once()->with('permission_roles', 'permission_roles.role_id', '=', 'role_users.role_id'); - $builder->shouldReceive('where')->once()->with('permission_id', '=', 1); - - // Now we set up the relationship with the related model. - return new BelongsToManyThrough( - $builder, $related, $intermediateRelationship, 'role_users', 'role_id', 'user_id', 'relation_name' - ); - } -} - -class EloquentBelongsToManyModelStub extends Model -{ - protected $guarded = []; -} +<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+
+namespace UserFrosting\Tests\Unit;
+
+use Illuminate\Database\Capsule\Manager as DB;
+use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Mockery as m;
+use ReflectionClass;
+use UserFrosting\Tests\TestCase;
+use UserFrosting\Tests\DatabaseTransactions;
+use UserFrosting\Sprinkle\Core\Database\Builder as QueryBuilder;
+use UserFrosting\Sprinkle\Core\Database\Models\Model;
+use UserFrosting\Sprinkle\Core\Database\Relations\BelongsToManyThrough;
+
+/**
+ * Tests the BelongsToManyThrough relation.
+ *
+ * @extends TestCase
+ */
+class BelongsToManyThroughTest extends TestCase
+{
+ public function tearDown() {
+ m::close();
+ }
+
+ function testPaginatedQuery() {
+ // Creates a real BelongsToManyThrough object
+ $relation = $this->getRelation();
+
+ // We need to define a mock base query, because Eloquent\Builder will pass through many calls
+ // to this underlying Query\Builder object.
+ $baseQuery = m::mock(QueryBuilder::class);
+ $builder = m::mock(EloquentBuilder::class, [$baseQuery])->makePartial();
+
+ $related = $relation->getRelated();
+ $related->shouldReceive('getQualifiedKeyName')->once()->andReturn('users.id');
+
+ $builder->shouldReceive('withGlobalScope')->once()->andReturnSelf();
+
+ $builder->shouldReceive('limit')->once()->with(2)->andReturnSelf();
+ $builder->shouldReceive('offset')->once()->with(1)->andReturnSelf();
+
+ // Mock the collection generated by the constrained query
+ $collection = m::mock('Illuminate\Database\Eloquent\Collection');
+ $collection->shouldReceive('pluck')->once()->with('id')->andReturn($collection);
+ $collection->shouldReceive('toArray')->once()->andReturn([1, 2]);
+ $builder->shouldReceive('get')->once()->andReturn($collection);
+
+ // Test the final modification to the original unpaginated query
+ $builder->shouldReceive('whereIn')->once()->with('users.id', [1, 2])->andReturnSelf();
+
+ $paginatedQuery = $relation->getPaginatedQuery($builder, 2, 1);
+ }
+
+ /**
+ * Set up and simulate base expectations for arguments to relationship.
+ */
+ protected function getRelation() {
+ // We simulate a BelongsToManyThrough relationship that gets all related users for a specified permission(s).
+ $builder = m::mock(EloquentBuilder::class);
+ $related = m::mock('Illuminate\Database\Eloquent\Model');
+ $related->shouldReceive('getKey')->andReturn(1);
+ $related->shouldReceive('getTable')->andReturn('users');
+ $related->shouldReceive('getKeyName')->andReturn('id');
+ // Tie the mocked builder to the mocked related model
+ $builder->shouldReceive('getModel')->andReturn($related);
+
+ // Mock the intermediate role->permission BelongsToMany relation
+ $intermediateRelationship = m::mock(BelongsToMany::class);
+ $intermediateRelationship->shouldReceive('getTable')->once()->andReturn('permission_roles');
+ $intermediateRelationship->shouldReceive('getQualifiedRelatedKeyName')->once()->andReturn('permission_roles.role_id');
+ // Crazy pivot query stuff
+ $newPivot = m::mock('\Illuminate\Database\Eloquent\Relations\Pivot');
+ $newPivot->shouldReceive('getForeignKey')->andReturn('permission_id');
+ $intermediateRelationship->shouldReceive('newExistingPivot')->andReturn($newPivot);
+
+ // Expectations for joining the main relation - users to roles
+ $builder->shouldReceive('join')->once()->with('role_users', 'users.id', '=', 'role_users.user_id');
+
+ // Expectations for joining the intermediate relation - roles to permissions
+ $builder->shouldReceive('join')->once()->with('permission_roles', 'permission_roles.role_id', '=', 'role_users.role_id');
+ $builder->shouldReceive('where')->once()->with('permission_id', '=', 1);
+
+ // Now we set up the relationship with the related model.
+ return new BelongsToManyThrough(
+ $builder, $related, $intermediateRelationship, 'role_users', 'role_id', 'user_id', 'relation_name'
+ );
+ }
+}
+
+class EloquentBelongsToManyModelStub extends Model
+{
+ protected $guarded = [];
+}
diff --git a/main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php b/main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php index d53976a..f94fbd5 100644 --- a/main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php +++ b/main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php @@ -1,114 +1,114 @@ -<?php -/** - * UserFrosting (http://www.userfrosting.com) - * - * @link https://github.com/userfrosting/UserFrosting - * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) - */ - -namespace UserFrosting\Tests\Unit; - -use stdClass; -use Mockery as m; -use ReflectionClass; -use PHPUnit\Framework\TestCase; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Collection; -use Illuminate\Support\Collection as BaseCollection; -use Illuminate\Database\Eloquent\ModelNotFoundException; - -use UserFrosting\Sprinkle\Core\Database\Relations\HasManySyncable; - -class DatabaseSyncableTest extends TestCase -{ - public function tearDown() { - m::close(); - } - - /** - * @dataProvider syncMethodHasManyListProvider - */ - public function testSyncMethod($list) { - $relation = $this->getRelation(); - - // Simulate determination of related key from builder - $relation->getRelated()->shouldReceive('getKeyName')->once()->andReturn('id'); - - // Simulate fetching of current relationships (1,2,3) - $query = m::mock('stdClass'); - $relation->shouldReceive('newQuery')->once()->andReturn($query); - $query->shouldReceive('pluck')->once()->with('id')->andReturn(new BaseCollection([1, 2, 3])); - - // withoutGlobalScopes will get called exactly 3 times - $relation->getRelated()->shouldReceive('withoutGlobalScopes')->times(3)->andReturn($query); - - // Test deletions of items removed from relationship (1) - $query->shouldReceive('whereIn')->once()->with('id', [1])->andReturn($query); - $query->shouldReceive('delete')->once()->andReturn($query); - - // Test updates to existing items in relationship (2,3) - $query->shouldReceive('where')->once()->with('id', 2)->andReturn($query); - $query->shouldReceive('update')->once()->with(['id' => 2, 'species' => 'Tyto'])->andReturn($query); - $query->shouldReceive('where')->once()->with('id', 3)->andReturn($query); - $query->shouldReceive('update')->once()->with(['id' => 3, 'species' => 'Megascops'])->andReturn($query); - - // Test creation of new items ('x') - $model = $this->expectCreatedModel($relation, [ - 'id' => 'x' - ]); - $model->shouldReceive('getAttribute')->with('id')->andReturn('x'); - - $this->assertEquals(['created' => ['x'], 'deleted' => [1], 'updated' => [2, 3]], $relation->sync($list)); - } - - /** - * Set up and simulate base expectations for arguments to relationship. - */ - protected function getRelation() { - $builder = m::mock('Illuminate\Database\Eloquent\Builder'); - $builder->shouldReceive('whereNotNull')->with('table.foreign_key'); - $builder->shouldReceive('where')->with('table.foreign_key', '=', 1); - $related = m::mock('Illuminate\Database\Eloquent\Model'); - $builder->shouldReceive('getModel')->andReturn($related); - $parent = m::mock('Illuminate\Database\Eloquent\Model'); - $parent->shouldReceive('getAttribute')->with('id')->andReturn(1); - $parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at'); - $parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at'); - return new HasManySyncable($builder, $parent, 'table.foreign_key', 'id'); - } - - public function syncMethodHasManyListProvider() { - return [ - // First test set - [ - // First argument - [ - [ - 'id' => 2, - 'species' => 'Tyto' - ], - [ - 'id' => 3, - 'species' => 'Megascops' - ], - [ - 'id' => 'x' - ] - ] - ] - // Additional test sets here - ]; - } - - protected function expectNewModel($relation, $attributes = NULL) { - $relation->getRelated()->shouldReceive('newInstance')->once()->with($attributes)->andReturn($model = m::mock(Model::class)); - $model->shouldReceive('setAttribute')->with('foreign_key', 1)->andReturn($model); - return $model; - } - - protected function expectCreatedModel($relation, $attributes) { - $model = $this->expectNewModel($relation, $attributes); - $model->shouldReceive('save')->andReturn($model); - return $model; - } -} +<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+
+namespace UserFrosting\Tests\Unit;
+
+use stdClass;
+use Mockery as m;
+use ReflectionClass;
+use PHPUnit\Framework\TestCase;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Collection as BaseCollection;
+use Illuminate\Database\Eloquent\ModelNotFoundException;
+
+use UserFrosting\Sprinkle\Core\Database\Relations\HasManySyncable;
+
+class DatabaseSyncableTest extends TestCase
+{
+ public function tearDown() {
+ m::close();
+ }
+
+ /**
+ * @dataProvider syncMethodHasManyListProvider
+ */
+ public function testSyncMethod($list) {
+ $relation = $this->getRelation();
+
+ // Simulate determination of related key from builder
+ $relation->getRelated()->shouldReceive('getKeyName')->once()->andReturn('id');
+
+ // Simulate fetching of current relationships (1,2,3)
+ $query = m::mock('stdClass');
+ $relation->shouldReceive('newQuery')->once()->andReturn($query);
+ $query->shouldReceive('pluck')->once()->with('id')->andReturn(new BaseCollection([1, 2, 3]));
+
+ // withoutGlobalScopes will get called exactly 3 times
+ $relation->getRelated()->shouldReceive('withoutGlobalScopes')->times(3)->andReturn($query);
+
+ // Test deletions of items removed from relationship (1)
+ $query->shouldReceive('whereIn')->once()->with('id', [1])->andReturn($query);
+ $query->shouldReceive('delete')->once()->andReturn($query);
+
+ // Test updates to existing items in relationship (2,3)
+ $query->shouldReceive('where')->once()->with('id', 2)->andReturn($query);
+ $query->shouldReceive('update')->once()->with(['id' => 2, 'species' => 'Tyto'])->andReturn($query);
+ $query->shouldReceive('where')->once()->with('id', 3)->andReturn($query);
+ $query->shouldReceive('update')->once()->with(['id' => 3, 'species' => 'Megascops'])->andReturn($query);
+
+ // Test creation of new items ('x')
+ $model = $this->expectCreatedModel($relation, [
+ 'id' => 'x'
+ ]);
+ $model->shouldReceive('getAttribute')->with('id')->andReturn('x');
+
+ $this->assertEquals(['created' => ['x'], 'deleted' => [1], 'updated' => [2, 3]], $relation->sync($list));
+ }
+
+ /**
+ * Set up and simulate base expectations for arguments to relationship.
+ */
+ protected function getRelation() {
+ $builder = m::mock('Illuminate\Database\Eloquent\Builder');
+ $builder->shouldReceive('whereNotNull')->with('table.foreign_key');
+ $builder->shouldReceive('where')->with('table.foreign_key', '=', 1);
+ $related = m::mock('Illuminate\Database\Eloquent\Model');
+ $builder->shouldReceive('getModel')->andReturn($related);
+ $parent = m::mock('Illuminate\Database\Eloquent\Model');
+ $parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
+ $parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at');
+ $parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
+ return new HasManySyncable($builder, $parent, 'table.foreign_key', 'id');
+ }
+
+ public function syncMethodHasManyListProvider() {
+ return [
+ // First test set
+ [
+ // First argument
+ [
+ [
+ 'id' => 2,
+ 'species' => 'Tyto'
+ ],
+ [
+ 'id' => 3,
+ 'species' => 'Megascops'
+ ],
+ [
+ 'id' => 'x'
+ ]
+ ]
+ ]
+ // Additional test sets here
+ ];
+ }
+
+ protected function expectNewModel($relation, $attributes = NULL) {
+ $relation->getRelated()->shouldReceive('newInstance')->once()->with($attributes)->andReturn($model = m::mock(Model::class));
+ $model->shouldReceive('setAttribute')->with('foreign_key', 1)->andReturn($model);
+ return $model;
+ }
+
+ protected function expectCreatedModel($relation, $attributes) {
+ $model = $this->expectNewModel($relation, $attributes);
+ $model->shouldReceive('save')->andReturn($model);
+ return $model;
+ }
+}
diff --git a/main/app/sprinkles/core/tests/Unit/SprunjeTest.php b/main/app/sprinkles/core/tests/Unit/SprunjeTest.php index dab34a5..66fcb34 100644 --- a/main/app/sprinkles/core/tests/Unit/SprunjeTest.php +++ b/main/app/sprinkles/core/tests/Unit/SprunjeTest.php @@ -1,96 +1,96 @@ -<?php -/** - * UserFrosting (http://www.userfrosting.com) - * - * @link https://github.com/userfrosting/UserFrosting - * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) - */ - -namespace UserFrosting\Tests\Unit; - -use Illuminate\Database\Capsule\Manager as DB; -use Mockery as m; -use UserFrosting\Tests\TestCase; -use UserFrosting\Tests\DatabaseTransactions; -use UserFrosting\Sprinkle\Core\Database\Builder as Builder; -use UserFrosting\Sprinkle\Core\Database\Models\Model; -use UserFrosting\Sprinkle\Core\Sprunje\Sprunje; -use UserFrosting\Sprinkle\Core\Util\ClassMapper; - -/** - * SprunjeTest class. - * Tests a basic Sprunje. - * - * @extends TestCase - */ -class SprunjeTest extends TestCase -{ - public function tearDown() { - m::close(); - } - - function testSprunjeApplyFiltersDefault() { - $sprunje = new SprunjeStub([ - 'filters' => [ - 'species' => 'Tyto' - ] - ]); - - $builder = $sprunje->getQuery(); - - // Need to mock the new Builder instance that Laravel spawns in the where() closure. - // See https://stackoverflow.com/questions/20701679/mocking-callbacks-in-laravel-4-mockery - $builder->shouldReceive('newQuery')->andReturn( - $subBuilder = m::mock(Builder::class, function ($subQuery) { - $subQuery->makePartial(); - $subQuery->shouldReceive('orLike')->with('species', 'Tyto')->once()->andReturn($subQuery); - }) - ); - - $sprunje->applyFilters($builder); - } - - function testSprunjeApplySortsDefault() { - $sprunje = new SprunjeStub([ - 'sorts' => [ - 'species' => 'asc' - ] - ]); - - $builder = $sprunje->getQuery(); - $builder->shouldReceive('orderBy')->once()->with('species', 'asc'); - $sprunje->applySorts($builder); - } - -} - -class SprunjeStub extends Sprunje -{ - protected $filterable = [ - 'species' - ]; - - protected $sortable = [ - 'species' - ]; - - public function __construct($options) { - $classMapper = new ClassMapper(); - parent::__construct($classMapper, $options); - } - - protected function baseQuery() { - // We use a partial mock for Builder, because we need to be able to run some of its actual methods. - // For example, we need to be able to run the `where` method with a closure. - $builder = m::mock(Builder::class); - $builder->makePartial(); - - return $builder; - } -} - -class SprunjeTestModelStub extends Model -{ - protected $table = 'table'; -} - +<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+
+namespace UserFrosting\Tests\Unit;
+
+use Illuminate\Database\Capsule\Manager as DB;
+use Mockery as m;
+use UserFrosting\Tests\TestCase;
+use UserFrosting\Tests\DatabaseTransactions;
+use UserFrosting\Sprinkle\Core\Database\Builder as Builder;
+use UserFrosting\Sprinkle\Core\Database\Models\Model;
+use UserFrosting\Sprinkle\Core\Sprunje\Sprunje;
+use UserFrosting\Sprinkle\Core\Util\ClassMapper;
+
+/**
+ * SprunjeTest class.
+ * Tests a basic Sprunje.
+ *
+ * @extends TestCase
+ */
+class SprunjeTest extends TestCase
+{
+ public function tearDown() {
+ m::close();
+ }
+
+ function testSprunjeApplyFiltersDefault() {
+ $sprunje = new SprunjeStub([
+ 'filters' => [
+ 'species' => 'Tyto'
+ ]
+ ]);
+
+ $builder = $sprunje->getQuery();
+
+ // Need to mock the new Builder instance that Laravel spawns in the where() closure.
+ // See https://stackoverflow.com/questions/20701679/mocking-callbacks-in-laravel-4-mockery
+ $builder->shouldReceive('newQuery')->andReturn(
+ $subBuilder = m::mock(Builder::class, function ($subQuery) {
+ $subQuery->makePartial();
+ $subQuery->shouldReceive('orLike')->with('species', 'Tyto')->once()->andReturn($subQuery);
+ })
+ );
+
+ $sprunje->applyFilters($builder);
+ }
+
+ function testSprunjeApplySortsDefault() {
+ $sprunje = new SprunjeStub([
+ 'sorts' => [
+ 'species' => 'asc'
+ ]
+ ]);
+
+ $builder = $sprunje->getQuery();
+ $builder->shouldReceive('orderBy')->once()->with('species', 'asc');
+ $sprunje->applySorts($builder);
+ }
+
+}
+
+class SprunjeStub extends Sprunje
+{
+ protected $filterable = [
+ 'species'
+ ];
+
+ protected $sortable = [
+ 'species'
+ ];
+
+ public function __construct($options) {
+ $classMapper = new ClassMapper();
+ parent::__construct($classMapper, $options);
+ }
+
+ protected function baseQuery() {
+ // We use a partial mock for Builder, because we need to be able to run some of its actual methods.
+ // For example, we need to be able to run the `where` method with a closure.
+ $builder = m::mock(Builder::class);
+ $builder->makePartial();
+
+ return $builder;
+ }
+}
+
+class SprunjeTestModelStub extends Model
+{
+ protected $table = 'table';
+}
+
|