Esempio n. 1
0
 /**
  * @covers \SweetORM\EntityManager
  * @covers \SweetORM\EntityManager::getInstance
  * @covers \SweetORM\EntityManager::getEntityStructure
  * @covers \SweetORM\EntityManager::validator
  * @covers \SweetORM\Entity::validator
  * @covers \SweetORM\Structure\Indexer\ColumnIndexer
  * @covers \SweetORM\Structure\EntityStructure
  * @covers \SweetORM\Structure\ValidationManager
  * @covers \SweetORM\Structure\ValidationManager::validator
  * @covers \SweetORM\Structure\Validator\Validator
  * @covers \SweetORM\Structure\Validator\ArrayValidator
  * @covers \SweetORM\Structure\Annotation\Constraint
  * @covers \SweetORM\Database\Query
  */
 public function testArrayValidation()
 {
     $manager = EntityManager::getInstance();
     $manager->clearRegisteredEntities();
     $array1 = array('tests' => 'nope');
     $result1 = Category::validator($array1)->test();
     $this->assertInstanceOf(ValidationResult::class, $result1);
     $this->assertFalse($result1->isSuccess());
     $this->assertCount(2, $result1->getErrors());
     $array2 = array('id' => 55, 'name' => 0, 'description' => null);
     $result2 = Category::validator($array2)->test();
     $this->assertInstanceOf(ValidationResult::class, $result2);
     $this->assertFalse($result2->isSuccess());
     $this->assertCount(2, $result2->getErrors());
     $this->assertContains('Must be \'string\'', $result2->getErrors()[0]);
     // Validate valid array.
     $array3 = array('name' => 'Test Name', 'description' => 'Jaja');
     $result3 = Category::validator($array3)->test();
     $this->assertInstanceOf(ValidationResult::class, $result3);
     $this->assertTrue($result3->isSuccess());
     // Validate invalid email.
     $array4 = array('name' => 'Henk', 'email' => 'invalid');
     $result4 = Student::validator($array4)->test();
     $this->assertInstanceOf(ValidationResult::class, $result4);
     $this->assertFalse($result4->isSuccess());
     // Validate valid email.
     $array5 = array('name' => 'Henk', 'email' => '*****@*****.**');
     $result5 = Student::validator($array5)->test();
     $this->assertInstanceOf(ValidationResult::class, $result5);
     $this->assertTrue($result5->isSuccess());
     // Validate invalid date.
     $array6 = array('name' => 'Henk', 'description' => '1234test', 'created' => 'invalid date');
     $result6 = Category::validator($array6)->test();
     $this->assertInstanceOf(ValidationResult::class, $result6);
     $this->assertFalse($result6->isSuccess());
     // Validate string date.
     $array7 = array('name' => 'Henk', 'description' => '1234test', 'created' => date('c'));
     $result7 = Category::validator($array7)->test();
     $this->assertInstanceOf(ValidationResult::class, $result7);
     $this->assertTrue($result7->isSuccess());
     // Validate numeric date.
     $array8 = array('name' => 'Henk', 'description' => '1234test', 'created' => time());
     $result8 = Category::validator($array8)->test();
     $this->assertInstanceOf(ValidationResult::class, $result8);
     $this->assertTrue($result8->isSuccess());
     // Type Testing
     $array9 = array('id' => intval('1'), 'string' => 'correct', 'text' => 'correct', 'double' => 1.22, 'float' => floatval('1.5345345'), 'boolean' => true);
     $result9 = TypeTest::validator($array9)->test();
     $this->assertTrue($result9->isSuccess());
 }
Esempio n. 2
0
 /**
  * @covers \SweetORM\Entity
  * @covers \SweetORM\EntityManager
  * @covers \SweetORM\Structure\RelationManager
  * @covers \SweetORM\Structure\RelationManager::saveRelations
  * @covers \SweetORM\Database\Query
  * @covers \SweetORM\Database\QueryGenerator
  * @covers \SweetORM\Database\Solver
  * @covers \SweetORM\Database\Solver\ManyToMany
  * @covers \SweetORM\Database\Solver\ManyToMany::solveSave
  */
 public function testSaveManyToMany()
 {
     Utilities::resetDatabase();
     // We will get student 2 first, the student already got course with id 1!
     $student = Student::get(2);
     /** @var Student $student */
     $this->assertEquals(2, $student->_id);
     // Add course with id 2
     $student->courses[] = Course::get(2);
     // Check if it's in the array
     $this->assertCount(2, $student->courses);
     // Save the student, this should solve the relation updates too
     $result = $student->save();
     $this->assertTrue($result);
     // Verify if it worked by clearing caches and re-fetch the student and courses
     RelationManager::clearCache();
     $student = Student::get(2);
     $this->assertEquals(2, $student->_id);
     $this->assertCount(2, $student->courses);
     // Check the other way around
     $course = Course::get(2);
     /** @var Course $course */
     $found = false;
     foreach ($course->students as $student) {
         if ($student->_id == 2) {
             $found = true;
         }
     }
     $this->assertTrue($found);
     // Test deleting all and inserting none
     RelationManager::clearCache();
     $student = Student::get(2);
     $student->courses->clear();
     $student->save();
     RelationManager::clearCache();
     $student = Student::get(2);
     $this->assertCount(0, $student->courses);
     // Testing replacing all for one.
     RelationManager::clearCache();
     $student = Student::get(2);
     $student->courses->clear();
     $student->courses[] = Course::get(2);
     $student->save();
     RelationManager::clearCache();
     $student = Student::get(2);
     $this->assertCount(1, $student->courses);
 }