Example #1
0
File: CRUD.php Project: helix/spot2
 public function testSampleNewsDelete()
 {
     $mapper = test_spot_mapper();
     $post = $mapper->first('Entity_Post', array('title' => "Test Post Modified"));
     $result = $mapper->delete($post);
     $this->assertTrue($result);
 }
Example #2
0
 public function testGetCustomEntityMapper()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Event');
     $this->assertInstanceOf(Entity\Event::mapper(), $mapper);
     $query = $mapper->testQuery();
     $this->assertInstanceOf('Spot\\Query', $query);
 }
Example #3
0
 public function testLength()
 {
     $mapper = test_spot_mapper();
     $entity = new Entity_Author(array('email' => 't@t', 'password' => 'test'));
     $mapper->save($entity);
     $this->assertTrue($entity->hasErrors());
     $this->assertContains("Email must be longer than 4", $entity->errors('email'));
 }
 public function testPostHasManyPolymorphicComments()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $post = $mapper->first();
     $this->assertInstanceOf('SpotTest\\Entity\\Post', $post);
     $query = $post->polymorphic_comments->query();
     $this->assertEquals(3, count($post->polymorphic_comments));
 }
Example #5
0
 public function testLength()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Author');
     $entity = new SpotTest\Entity\Author(['email' => 't@t', 'password' => 'test']);
     $mapper->save($entity);
     $this->assertTrue($entity->hasErrors());
     $this->assertContains("Email must be longer than 4", $entity->errors('email'));
 }
Example #6
0
 public function testInsertPostArray()
 {
     $mapper = test_spot_mapper();
     $post = array('title' => "Test Post", 'body' => "<p>This is a really awesome super-duper post.</p><p>It's really quite lovely.</p>", 'date_created' => $mapper->connection('\\Spot\\Entity\\Post')->dateTime());
     $result = $mapper->insert('\\Spot\\Entity\\Post', $post);
     // returns inserted id
     $this->assertTrue($result !== false);
 }
Example #7
0
 public function testEntitySetDataConstruct()
 {
     $mapper = test_spot_mapper();
     $post = new Entity_Post(array('title' => 'My Awesome Post', 'body' => '<p>Body</p>'));
     $data = $post->data();
     ksort($data);
     $testData = array('id' => null, 'title' => 'My Awesome Post', 'body' => '<p>Body</p>', 'status' => 0, 'date_created' => null);
     ksort($testData);
     $this->assertEquals($testData, $data);
 }
Example #8
0
 public function testUniqueCompoundIndexNoValidationErrorWhenDataDifferent()
 {
     $zipMapper = test_spot_mapper('\\SpotTest\\Entity\\Zip');
     $data = ['code' => '23456', 'city' => 'Testville', 'state' => 'NY', 'lat' => 1, 'lng' => 2];
     $zip1 = $zipMapper->create($data);
     // Make data slightly different on unique compound index
     $data2 = array_merge($data, ['city' => 'Testville2']);
     $zip2 = $zipMapper->create($data2);
     $this->assertEmpty($zip1->errors());
     $this->assertEmpty($zip2->errors());
 }
Example #9
0
 public function testEntityRelations()
 {
     $mapper = test_spot_mapper();
     $post = new Entity_Post();
     $relations = $mapper->relations('Entity_Post');
     $sortedRelations = array_keys($relations);
     sort($sortedRelations);
     // Assert $relations are correct
     $testRelations = array('comments', 'tags', 'author');
     sort($testRelations);
     $this->assertEquals($sortedRelations, $testRelations);
 }
Example #10
0
 public function testEntitySetPropertiesData()
 {
     $mapper = test_spot_mapper();
     $post = new Entity_Post();
     // Set data
     $post->title = "My Awesome Post";
     $post->body = "<p>Body</p>";
     $data = $mapper->data($post);
     ksort($data);
     $testData = array('id' => null, 'title' => 'My Awesome Post', 'body' => '<p>Body</p>', 'status' => null, 'date_created' => null);
     ksort($testData);
     $this->assertEquals($testData, $data);
 }
Example #11
0
 public static function events(EventEmitter $eventEmitter)
 {
     $eventEmitter->on('beforeInsert', function ($entity, $mapper) {
         $entity->token = uniqid();
     });
     $eventEmitter->on('afterInsert', function ($entity, $mapper) {
         $mapper = test_spot_mapper('SpotTest\\Entity\\Event\\Search');
         $result = $mapper->create(['event_id' => $entity->id, 'body' => $entity->title . ' ' . $entity->description]);
         if (!$result) {
             throw new \Spot\Exception("Event search index entity failed to save!");
         }
     });
 }
Example #12
0
 public function testUniqueFieldCreatesValidationError()
 {
     $mapper = test_spot_mapper();
     // Setup new user
     $user1 = new \Spot\Entity\User(array('email' => '*****@*****.**', 'password' => 'test', 'is_admin' => true));
     $mapper->save($user1);
     // Setup new user (identical, expecting a validation error)
     $user2 = new \Spot\Entity\User(array('email' => '*****@*****.**', 'password' => 'test', 'is_admin' => false));
     $mapper->save($user2);
     $this->assertFalse($user1->hasErrors());
     $this->assertTrue($user2->hasErrors());
     $this->assertEquals($user2->errors('email'), array("Email '*****@*****.**' is already taken."));
 }
Example #13
0
 public function testEventSearchIndex()
 {
     $mapper = test_spot_mapper();
     if (!$mapper->config()->connection() instanceof \Spot\Adapter\Mysql) {
         $this->markTestSkipped('Only supported in MySQL - requires FULLTEXT search');
     }
     $event = new Entity_Event(array('title' => 'Test Event 1', 'description' => 'Test Description', 'type' => 'free', 'date_start' => strtotime('+1 day')));
     $mapper->save($event);
     // Ensure Event_Search record was inserted with 'afterSave' hook
     $eventSearchEntity = $mapper->first('Entity_Event_Search', array('event_id' => $event->id));
     $this->assertInstanceOf('Entity_Event_Search', $eventSearchEntity);
     $events = $mapper->all('Entity_Event_Search')->search('body', 'Test', array('boolean' => true))->execute();
     $this->assertGreaterThan(0, count($events));
 }
Example #14
0
 public function testRelationConditions()
 {
     $mapper = test_spot_mapper();
     for ($i = 1; $i <= 10; $i++) {
         $id = $mapper->insert('Entity_Post', array('title' => ($i % 2 ? 'odd' : 'even') . '_title', 'author_id' => 1, 'body' => '<p>' . $i . '_body</p>', 'status' => $i, 'date_created' => $mapper->connection('Entity_Post')->dateTime()));
         for ($j = 1; $j <= 2; $j++) {
             $mapper->insert('Entity_Post_Comment', array('post_id' => $id, 'name' => ($j % 2 ? 'odd' : 'even') . '_title', 'email' => '*****@*****.**', 'body' => ($j % 2 ? 'odd' : 'even') . '_comment_body'));
         }
     }
     $post = $mapper->all('Entity_Post')->first();
     $relation = $mapper->loadRelation($post, 'comments');
     $this->assertEquals($relation->conditions(), array('post_id' => 1));
     $posts = $mapper->all('Entity_Post')->execute();
     $relation = $mapper->loadRelation($posts, 'comments');
     $this->assertEquals($relation->conditions(), array('post_id' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)));
 }
Example #15
0
 public function testInsertWithTransactionRollbackOnReturnFalse()
 {
     $post = new \Spot\Entity\Post();
     $mapper = test_spot_mapper();
     $post->title = "Rolledback";
     $post->body = "<p>This is a really awesome super-duper post -- in a TRANSACTION!.</p>";
     $post->date_created = $mapper->connection('\\Spot\\Entity\\Post')->dateTime();
     // Save in transation
     $phpunit = $this;
     $mapper->transaction(function ($mapper) use($post, $phpunit) {
         $result = $mapper->insert($post);
         // Return false AFTER save to trigger rollback
         return false;
     });
     // Ensure record was NOT saved
     $this->assertFalse($mapper->first('\\Spot\\Entity\\Post', array('title' => $post->title)));
 }
Example #16
0
 public function testCustomTypeRegistration()
 {
     $mapper = test_spot_mapper();
     $cfg = $mapper->config();
     // Register JSON type, add json field type, and migrate
     $cfg->typeHandler('json_oneway', 'Test\\Type\\Json');
     Entity_Type::$_fields['json_oneway'] = array('type' => 'json_oneway');
     $mapper->entityManager()->resetFields();
     // Have to resetFields b/c field definitions are cached
     $this->assertArrayHasKey('json_oneway', $mapper->fields('Entity_Type'));
     $mapper->migrate('Entity_Type');
     // Save array data with JSON type
     $data = array('json_oneway' => array('a' => 'b', 'foo' => 'bar'));
     $result = $mapper->create('Entity_Type', $data);
     $this->assertTrue($result !== false);
     // Select entity from db to ensure it is saved correctly
     $entity = $mapper->get('Entity_Type', $result->id);
     $this->assertSame(json_encode($data['json_oneway']), $entity->json_oneway);
     $mapper->dropDatasource('Entity_Type');
 }
Example #17
0
 /**
  * @depends testEventInsert
  */
 public function testEventSearchBelongsToEvent($eventId)
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Event\\Search');
     $eventSearch = $mapper->first(['event_id' => $eventId]);
     $this->assertInstanceOf('SpotTest\\Entity\\Event', $eventSearch->event->execute());
 }
Example #18
0
 public function testSerialized()
 {
     $data = array('title' => 'A Post', 'body' => 'A Body', 'status' => 0, 'author_id' => 1, 'data' => array('posts' => 'are cool', 'another field' => 'to serialize'));
     $post = new Entity_Post($data);
     $this->assertEquals($post->data, array('posts' => 'are cool', 'another field' => 'to serialize'));
     $mapper = test_spot_mapper();
     $mapper->save($post);
     $post = $mapper->all('Entity_Post')->first();
     $this->assertEquals($post->data, array('posts' => 'are cool', 'another field' => 'to serialize'));
     $post->data = 'asdf';
     $this->assertEquals($post->data, 'asdf');
     $mapper->save($post);
     $post = $mapper->all('Entity_Post')->first();
     $this->assertEquals($post->data, 'asdf');
 }
Example #19
0
 public function testInsertEventRunsTypeOptionsValidation()
 {
     $mapper = test_spot_mapper('\\SpotTest\\Entity\\Event');
     $event = new \SpotTest\Entity\Event(['title' => 'Test Event 1', 'description' => 'Test Description', 'type' => 'invalid_value', 'date_start' => new \DateTime('+1 day')]);
     $result = $mapper->insert($event);
     $this->assertFalse($result);
     $this->assertEquals(['Type contains invalid value'], $event->errors('type'));
 }
Example #20
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testInvalidRelationClass()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $entity = $mapper->first();
     $entity->fake = $mapper->hasOne($entity, 'Nonexistent\\Entity', 'fake_field');
     $entity->fake->something;
 }
Example #21
0
 public function testJsonArray()
 {
     $data = ['title' => 'A Post', 'body' => 'A Body', 'status' => 0, 'author_id' => 1, 'data' => ['posts' => 'are cool', 'another field' => 'to serialize'], 'date_created' => new \DateTime()];
     $post = new \SpotTest\Entity\Post($data);
     $this->assertEquals($post->data, ['posts' => 'are cool', 'another field' => 'to serialize']);
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $mapper->save($post);
     $post = $mapper->get($post->id);
     $this->assertEquals($post->data, ['posts' => 'are cool', 'another field' => 'to serialize']);
     $post->data = 'asdf';
     $this->assertEquals($post->data, 'asdf');
     $mapper->save($post);
     $post = $mapper->get($post->id);
     $this->assertEquals($post->data, 'asdf');
 }
Example #22
0
 public function testInsertWithoutAutoIncrementWithoutPKValueHasValidationError()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\NoSerial');
     $entity = $mapper->build(['data' => 'Testing insert']);
     $result = $mapper->insert($entity);
     $this->assertEquals(false, $result);
     $this->assertEquals(1, count($entity->errors('id')));
 }
Example #23
0
 public function testWhereFieldSqlSubqueryInClause()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $params = [3, 4, 5];
     $postsSub = $mapper->where(['status !=' => $params]);
     $posts = $mapper->select()->whereFieldSql('id', 'IN(' . $postsSub->toSql() . ')', $params);
     $this->assertContains('IN', $posts->toSql());
 }
Example #24
0
 public function testMultipleScopes()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Event');
     $query = $mapper->select()->noQuote()->free()->active();
     $this->assertEquals("SELECT * FROM test_events test_events WHERE (test_events.type = ?) AND (test_events.status = ?)", $query->toSql());
 }
Example #25
0
 public function testSaveEventsTriggeredOnUpdate()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $eventEmitter = $mapper->eventEmitter();
     $hooks = [];
     $eventEmitter = $mapper->eventEmitter();
     $eventEmitter->on('beforeSave', function ($post, $mapper) use(&$hooks) {
         $hooks[] = 'before';
     });
     $eventEmitter->on('afterSave', function ($post, $mapper) use(&$hooks) {
         $hooks[] = 'after';
     });
     $post = $mapper->create(['title' => 'A title', 'body' => '<p>body</p>', 'status' => 1, 'author_id' => 1, 'date_created' => new \DateTime()]);
     $post->status = 2;
     $mapper->update($post);
     $this->assertEquals(['before', 'after', 'before', 'after'], $hooks);
     $eventEmitter->removeAllListeners();
 }
Example #26
0
 /**
  * @depends testBlogPostInsert
  */
 public function testBlogCommentsRelationCanBeModified($postId)
 {
     $mapper = test_spot_mapper();
     $post = $mapper->get('Entity_Post', $postId);
     $this->assertTrue($post->comments instanceof \Spot\Relation\HasMany);
     $sortedComments = $post->comments->order(array('date_created' => 'DESC'));
     $this->assertTrue($sortedComments instanceof \Spot\Query);
 }
Example #27
0
 public function testCustomQueryWithSqlAndNamedParams()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $posts = $mapper->query("SELECT * FROM " . $mapper->table() . " WHERE status < :status", ['status' => 10]);
     $this->assertInstanceOf('Spot\\Entity\\Collection', $posts);
     $postCount = count($posts);
     $i = 0;
     foreach ($posts as $post) {
         $i++;
         $this->assertInstanceOf('SpotTest\\Entity\\Post', $post);
     }
     $this->assertSame($postCount, $i);
 }
Example #28
0
 public function testArrayNotInMultiple()
 {
     $mapper = test_spot_mapper();
     $posts = $mapper->all('Entity_Post', array('status !=' => array(3, 4, 5)));
     $this->assertEquals(7, $posts->count());
     $posts = $mapper->all('Entity_Post', array('status :not' => array(3, 4, 5)));
     $this->assertEquals(7, $posts->count());
 }
Example #29
0
 public function testAfterSaveEntityHook()
 {
     $mapper = test_spot_mapper();
     $post = new Entity_Post(array('title' => 'A title', 'body' => '<p>body</p>', 'status' => 1, 'author_id' => 1, 'date_created' => new \DateTime()));
     $i = $post->status;
     Entity_Post::$hooks = array('afterSave' => array('mock_save_hook'));
     $mapper->save($post);
     $this->assertEquals($i + 1, $post->status);
 }
Example #30
0
 public function testAfterSaveEvent()
 {
     $mapper = test_spot_mapper('SpotTest\\Entity\\Post');
     $eventEmitter = $mapper->eventEmitter();
     $post = new \SpotTest\Entity\Post(['title' => 'A title', 'body' => '<p>body</p>', 'status' => 1, 'author_id' => 1, 'date_created' => new \DateTime()]);
     $eventEmitter->removeAllListeners('afterSave');
     \SpotTest\Entity\Post::$events = ['afterSave' => ['mock_save_hook']];
     $mapper->loadEvents();
     $result = $mapper->save($post);
     $this->assertEquals(2, $post->status);
     $eventEmitter->removeAllListeners('afterSave');
 }