public function testShouldNotAutoIncrementSequenceIfValueIsNotNull() { $schema = m::mock(Schema::class . '[]'); $sequenceService = m::mock(SequenceService::class); $value = 3; $schema->collection = 'resources'; // Act Ioc::instance(SequenceService::class, $sequenceService); $sequenceService->shouldReceive('getNextValue')->with('resources')->never()->andReturn(7); // Should never be returned // Assertion $this->assertEquals(3, $schema->sequence($value)); }
public function testShouldGetDataMapperForEntitiesWithRegisteredSchemas() { // Arrange $manager = new Manager(); $schema = m::mock(Schema::class); $dataMapper = m::mock(DataMapper::class)->makePartial(); $schema->entityClass = 'Bacon'; // Act Ioc::instance(DataMapper::class, $dataMapper); // Assert $manager->registerSchema($schema); $result = $manager->getMapper('Bacon'); $this->assertEquals($dataMapper, $result); $this->assertAttributeEquals($schema, 'schema', $result); }
public function testShouldGetOriginalCursorFromDatabaseAfterTheDocumentLimit() { // Arrange $documentsFromDb = [['name' => 'joe'], ['name' => 'doe']]; $cursor = $this->getCachableCursor()->limit(150); $cacheComponent = m::mock(CacheComponentInterface::class); $rawCollection = m::mock(); $this->setProtected($cursor, 'position', CacheableCursor::DOCUMENT_LIMIT + 1); $this->setProtected($cursor, 'collection', $rawCollection); // Act $cursor->shouldReceive('generateCacheKey')->never(); Ioc::instance(CacheComponentInterface::class, $cacheComponent); $cacheComponent->shouldReceive('get')->with('find:collection:123', null)->never(); $rawCollection->shouldReceive('find')->with([], ['skip' => CacheableCursor::DOCUMENT_LIMIT, 'limit' => 49])->andReturn(new ArrayIterator($documentsFromDb)); $cacheComponent->shouldReceive('put')->never(); // Assert $this->assertEquals(new IteratorIterator(new ArrayIterator($documentsFromDb)), $this->callProtected($cursor, 'getCursor')); }
/** * @dataProvider EntityAssemblerFixture */ public function testShouldAssembleEntityForTheGivenSchema($inputValue, $availableSchemas, $inputSchema, $expectedOutput) { // Arrange $entityAssembler = new EntityAssembler(); $schemas = []; foreach ($availableSchemas as $key => $value) { $schemas[$key] = m::mock(Schema::class . '[]'); $schemas[$key]->entityClass = $value['entityClass']; $schemas[$key]->fields = $value['fields']; } // Act foreach ($schemas as $className => $instance) { Ioc::instance($className, $instance); } // Assert $result = $entityAssembler->assemble($inputValue, $schemas[$inputSchema]); $this->assertEquals($expectedOutput, $result); }
public function testShouldExecuteBulkWrite() { $entity = m::mock(HasSchemaInterface::class); $schema = m::mock(Schema::class); $entity->schema = $schema; $mongoBulkWrite = m::mock(new MongoBulkWrite()); $pool = m::mock(Pool::class); $connection = m::mock(Connection::class); $manager = m::mock(Manager::class); $connection->defaultDatabase = 'foo'; $schema->collection = 'bar'; $namespace = 'foo.bar'; Ioc::instance(Pool::class, $pool); // Expect $entity->shouldReceive('getSchema')->once()->with()->andReturn($schema); $pool->shouldReceive('getConnection')->once()->with()->andReturn($connection); $connection->shouldReceive('getRawManager')->once()->with()->andReturn($manager); $manager->shouldReceive('executeBulkWrite')->once()->with($namespace, $mongoBulkWrite, m::type(WriteConcern::class))->andReturn(true); $bulkWrite = m::mock(BulkWrite::class . '[getBulkWrite]', [$entity]); $bulkWrite->shouldReceive('getBulkWrite')->once()->with()->andReturn($mongoBulkWrite); // Act $bulkWrite->execute(); }
protected function getEventService() { if (!($this->eventService ?? false)) { $this->eventService = m::mock(EventTriggerService::class); Ioc::instance(EventTriggerService::class, $this->eventService); } return $this->eventService; }
/** * @dataProvider embedsScenarios */ public function testShouldEmbedsMany($entity, $field, $fieldValue, $expectedItems) { // Set $model = m::mock(ActiveRecord::class . '[]'); $cursorFactory = m::mock(CursorFactory::class); $cursor = m::mock(EmbeddedCursor::class); $document = $fieldValue; $model->{$field} = $document; $instantiableClass = $entity instanceof Schema ? 'stdClass' : get_class($entity); // Act Ioc::instance(CursorFactory::class, $cursorFactory); $cursorFactory->shouldReceive('createEmbeddedCursor')->once()->with($instantiableClass, $expectedItems)->andReturn($cursor); // Assert $result = $this->callProtected($model, 'embedsMany', [get_class($entity), $field]); $this->assertEquals($cursor, $result); }
public function testShouldGetSchemaIfFieldsIsTheClassName() { // Arrage $this->setProtected($this->entity, 'fields', 'MySchemaClass'); $schema = m::mock(Schema::class); // Act Ioc::instance('MySchemaClass', $schema); // Assert $this->assertEquals($schema, $this->entity->getSchema()); }
public function testShouldMapAnArrayValueToAnotherSchemaSchema() { // Arrange $schema = m::mock(Schema::class); $mySchema = m::mock(Schema::class); $schemaMapper = new SchemaMapper($schema); $value = ['foo' => 'bar']; $test = $this; // Act Ioc::instance('Xd\\MySchema', $mySchema); // Register MySchema in Ioc // When instantiating the SchemaMapper with the specified $param as dependency Ioc::bind(SchemaMapper::class, function ($container, $params) use($value, $mySchema, $test) { // Check if mySchema has been injected correctly $test->assertSame($mySchema, $params[0]); // Instantiate a SchemaMapper with mySchema $anotherSchemaMapper = m::mock(SchemaMapper::class, [$params[0]]); // Set expectation to receiva a map call $anotherSchemaMapper->shouldReceive('map')->once()->with($value)->andReturn(['foo' => 'PARSED']); return $anotherSchemaMapper; }); //Assert $this->assertEquals([['foo' => 'PARSED']], $this->callProtected($schemaMapper, 'mapToSchema', [$value, 'Xd\\MySchema'])); }
public function testShouldSerializeAnActiveCursor() { // Arrange $pool = m::mock(Pool::class); $conn = m::mock(Connection::class); $schema = new DynamicSchema(); $cursor = $this->getCursor($schema, null, 'find', [[]]); $driverCollection = $this->getDriverCollection(); $this->setProtected($cursor, 'collection', $driverCollection); // Act Ioc::instance(Pool::class, $pool); $pool->shouldReceive('getConnection')->andReturn($conn); $conn->shouldReceive('getRawConnection')->andReturn($conn); $conn->defaultDatabase = 'db'; $conn->db = $conn; $conn->my_collection = $driverCollection; // Return the same driver Collection // Assert $result = unserialize(serialize($cursor)); $this->assertEquals($cursor, $result); }