Ejemplo n.º 1
0
 public function testHasOne()
 {
     $customer = new Customer();
     $source = DataSource::make($customer);
     $source['name'] = 'Frank';
     $source['surname'] = 'Sinatra';
     $source['details.biography'] = 'A nice life!';
     $source['details.accepts_cookies'] = 0;
     $source['details.accepts_cookies'] = 0;
     // test cache
     assertSame('Frank', $source['name']);
     assertSame('A nice life!', $source['details.biography']);
     assertSame(0, $source['details.accepts_cookies']);
     $source->save();
     assertSame(1, Customer::all()->count());
     assertSame(1, CustomerDetail::all()->count());
     // test everything's saved
     $result = Customer::all()->first();
     assertSame('A nice life!', $result->details->biography);
     assertEquals(0, $result->details->accepts_cookies);
     // test we don't create duplicates
     $source['surname'] = 'Sinatrax';
     $source['details.biography'] = 'prefers not say';
     $source['details.accepts_cookies'] = 1;
     assertSame(1, Customer::all()->count());
     assertSame(1, CustomerDetail::all()->count());
     return;
 }
Ejemplo n.º 2
0
 public function testCreateChildren()
 {
     $this->createModels(CustomerDetail::class, []);
     $this->createModels(Customer::class, []);
     $source = DataSource::make(new CustomerDetail());
     $source['id'] = 1;
     $source['biography'] = 'A nice life!';
     $source['accepts_cookies'] = 0;
     $source['customer.name'] = 'Frank';
     $source['customer.surname'] = 'Sinatra';
     $source->save();
     assertModelArrayEqual([['customer_id' => '1', 'biography' => 'A nice life!', 'accepts_cookies' => '0', 'customer' => ['id' => '1', 'name' => 'Frank', 'surname' => 'Sinatra']]], CustomerDetail::with('customer')->get()->toArray());
     assertModelArrayEqual([['name' => 'Frank', 'surname' => 'Sinatra']], Customer::all()->toArray());
     // repeat
     $source->save();
     assertModelArrayEqual([['customer_id' => '1', 'biography' => 'A nice life!', 'accepts_cookies' => '0', 'customer' => ['id' => '1', 'name' => 'Frank', 'surname' => 'Sinatra']]], CustomerDetail::with('customer')->get()->toArray());
     assertModelArrayEqual([['name' => 'Frank', 'surname' => 'Sinatra']], Customer::all()->toArray());
 }