Esempio n. 1
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);
 }