diff options
author | marvin-borner@live.com | 2018-04-16 21:09:05 +0200 |
---|---|---|
committer | marvin-borner@live.com | 2018-04-16 21:09:05 +0200 |
commit | cf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch) | |
tree | 86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/core/tests/Unit | |
parent | 619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff) |
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/core/tests/Unit')
3 files changed, 322 insertions, 0 deletions
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 @@ +<?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 new file mode 100755 index 0000000..188b012 --- /dev/null +++ b/main/app/sprinkles/core/tests/Unit/DatabaseSyncableTest.php @@ -0,0 +1,119 @@ +<?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 new file mode 100755 index 0000000..fa19319 --- /dev/null +++ b/main/app/sprinkles/core/tests/Unit/SprunjeTest.php @@ -0,0 +1,100 @@ +<?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'; +} + |