From cf14306c2b3f82a81f8d56669a71633b4d4b5fce Mon Sep 17 00:00:00 2001 From: marvin-borner@live.com Date: Mon, 16 Apr 2018 21:09:05 +0200 Subject: Main merge to user management system - files are now at /main/public/ --- .../core/tests/Unit/BelongsToManyThroughTest.php | 103 ++++++++++++++++++ .../core/tests/Unit/DatabaseSyncableTest.php | 119 +++++++++++++++++++++ main/app/sprinkles/core/tests/Unit/SprunjeTest.php | 100 +++++++++++++++++ 3 files changed, 322 insertions(+) create mode 100755 main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php create mode 100755 main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php create mode 100755 main/app/sprinkles/core/tests/Unit/SprunjeTest.php (limited to 'main/app/sprinkles/core/tests/Unit') diff --git a/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php b/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php new file mode 100755 index 0000000..d4fd4f8 --- /dev/null +++ b/main/app/sprinkles/core/tests/Unit/BelongsToManyThroughTest.php @@ -0,0 +1,103 @@ +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 new file mode 100755 index 0000000..188b012 --- /dev/null +++ b/main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php @@ -0,0 +1,119 @@ +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 new file mode 100755 index 0000000..fa19319 --- /dev/null +++ b/main/app/sprinkles/core/tests/Unit/SprunjeTest.php @@ -0,0 +1,100 @@ + [ + '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'; +} + -- cgit v1.2.3