Skip to content

Commit a37b9d5

Browse files
committed
test: update existing tests
1 parent 8f20f04 commit a37b9d5

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

tests/system/Entity/EntityTest.php

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -347,23 +347,31 @@ public function testCastIntBool(): void
347347
];
348348
};
349349

350-
$entity->setAttributes(['active' => '1']);
350+
$entity->active = '1';
351351

352352
$this->assertTrue($entity->active);
353353

354-
$entity->setAttributes(['active' => '0']);
354+
$entity->active = '0';
355+
356+
$this->assertFalse($entity->active);
357+
358+
$entity->active = 1;
359+
360+
$this->assertTrue($entity->active);
361+
362+
$entity->active = 0;
355363

356364
$this->assertFalse($entity->active);
357365

358366
$entity->active = true;
359367

360368
$this->assertTrue($entity->active);
361-
$this->assertSame(['active' => 1], $entity->toRawArray());
369+
$this->assertSame(['active' => 1], $entity->toDatabase());
362370

363371
$entity->active = false;
364372

365373
$this->assertFalse($entity->active);
366-
$this->assertSame(['active' => 0], $entity->toRawArray());
374+
$this->assertSame(['active' => 0], $entity->toDatabase());
367375
}
368376

369377
public function testCastFloat(): void
@@ -423,11 +431,12 @@ public function testCastBoolean(): void
423431

424432
public function testCastCSV(): void
425433
{
426-
$entity = $this->getCastEntity();
434+
$entity = $this->getCastEntity();
435+
427436
$data = ['foo', 'bar', 'bam'];
428437
$entity->twelfth = $data;
429438

430-
$result = $entity->toRawArray();
439+
$result = $entity->toDatabase();
431440

432441
$this->assertIsString($result['twelfth']);
433442
$this->assertSame('foo,bar,bam', $result['twelfth']);
@@ -486,8 +495,9 @@ public function testCastArray(): void
486495

487496
$entity->seventh = ['foo' => 'bar'];
488497

489-
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
490-
$this->assertSame(serialize(['foo' => 'bar']), $check);
498+
$result = $entity->toDatabase();
499+
500+
$this->assertSame(serialize(['foo' => 'bar']), $result['seventh']);
491501
$this->assertSame(['foo' => 'bar'], $entity->seventh);
492502
}
493503

@@ -497,11 +507,12 @@ public function testCastArrayByStringSerialize(): void
497507

498508
$entity->seventh = 'foobar';
499509

510+
$result = $entity->toDatabase();
511+
500512
// Should be a serialized string now...
501-
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
502-
$this->assertSame(serialize('foobar'), $check);
513+
$this->assertSame(serialize('foobar'), $result['seventh']);
503514

504-
$this->assertSame(['foobar'], $entity->seventh);
515+
$this->assertSame('foobar', $entity->seventh);
505516
}
506517

507518
public function testCastArrayByArraySerialize(): void
@@ -510,9 +521,10 @@ public function testCastArrayByArraySerialize(): void
510521

511522
$entity->seventh = ['foo' => 'bar'];
512523

524+
$result = $entity->toDatabase();
525+
513526
// Should be a serialized string now...
514-
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
515-
$this->assertSame(serialize(['foo' => 'bar']), $check);
527+
$this->assertSame(serialize(['foo' => 'bar']), $result['seventh']);
516528

517529
$this->assertSame(['foo' => 'bar'], $entity->seventh);
518530
}
@@ -524,9 +536,10 @@ public function testCastArrayByFill(): void
524536
$data = ['seventh' => [1, 2, 3]];
525537
$entity->fill($data);
526538

539+
$result = $entity->toDatabase();
540+
527541
// Check if serialiazed
528-
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
529-
$this->assertSame(serialize([1, 2, 3]), $check);
542+
$this->assertSame(serialize([1, 2, 3]), $result['seventh']);
530543
// Check if unserialized
531544
$this->assertSame([1, 2, 3], $entity->seventh);
532545
}
@@ -536,9 +549,10 @@ public function testCastArrayByConstructor(): void
536549
$data = ['seventh' => [1, 2, 3]];
537550
$entity = $this->getCastEntity($data);
538551

552+
$result = $entity->toDatabase();
553+
539554
// Check if serialiazed
540-
$check = $this->getPrivateProperty($entity, 'attributes')['seventh'];
541-
$this->assertSame(serialize([1, 2, 3]), $check);
555+
$this->assertSame(serialize([1, 2, 3]), $result['seventh']);
542556
// Check if unserialized
543557
$this->assertSame([1, 2, 3], $entity->seventh);
544558
}
@@ -584,12 +598,12 @@ public function testCastAsJSON(): void
584598

585599
$entity->tenth = ['foo' => 'bar'];
586600

601+
$result = $entity->toDatabase();
602+
587603
// Should be a JSON-encoded string now...
588-
$check = $this->getPrivateProperty($entity, 'attributes')['tenth'];
589-
$this->assertSame('{"foo":"bar"}', $check);
604+
$this->assertSame('{"foo":"bar"}', $result['tenth']);
590605

591-
$this->assertInstanceOf('stdClass', $entity->tenth);
592-
$this->assertSame(['foo' => 'bar'], (array) $entity->tenth);
606+
$this->assertSame(['foo' => 'bar'], $entity->tenth);
593607
}
594608

595609
public function testCastAsJSONArray(): void
@@ -599,9 +613,10 @@ public function testCastAsJSONArray(): void
599613
$data = ['Sun', 'Mon', 'Tue'];
600614
$entity->eleventh = $data;
601615

616+
$result = $entity->toDatabase();
617+
602618
// Should be a JSON-encoded string now...
603-
$check = $this->getPrivateProperty($entity, 'attributes')['eleventh'];
604-
$this->assertSame('["Sun","Mon","Tue"]', $check);
619+
$this->assertSame('["Sun","Mon","Tue"]', $result['eleventh']);
605620

606621
$this->assertSame($data, $entity->eleventh);
607622
}
@@ -613,9 +628,10 @@ public function testCastAsJsonByFill(): void
613628
$data = ['eleventh' => [1, 2, 3]];
614629
$entity->fill($data);
615630

631+
$result = $entity->toDatabase();
632+
616633
// Check if serialiazed
617-
$check = $this->getPrivateProperty($entity, 'attributes')['eleventh'];
618-
$this->assertSame(json_encode([1, 2, 3]), $check);
634+
$this->assertSame(json_encode([1, 2, 3]), $result['eleventh']);
619635
// Check if unserialized
620636
$this->assertSame([1, 2, 3], $entity->eleventh);
621637
}
@@ -625,9 +641,10 @@ public function testCastAsJsonByConstructor(): void
625641
$data = ['eleventh' => [1, 2, 3]];
626642
$entity = $this->getCastEntity($data);
627643

644+
$result = $entity->toDatabase();
645+
628646
// Check if serialiazed
629-
$check = $this->getPrivateProperty($entity, 'attributes')['eleventh'];
630-
$this->assertSame(json_encode([1, 2, 3]), $check);
647+
$this->assertSame(json_encode([1, 2, 3]), $result['eleventh']);
631648
// Check if unserialized
632649
$this->assertSame([1, 2, 3], $entity->eleventh);
633650
}
@@ -651,6 +668,8 @@ public function testCastAsJSONErrorDepth(): void
651668
}
652669
$current = $value;
653670
$entity->tenth = $array;
671+
672+
$entity->toDatabase();
654673
}
655674

656675
public function testCastAsJSONErrorUTF8(): void
@@ -661,6 +680,8 @@ public function testCastAsJSONErrorUTF8(): void
661680
$entity = $this->getCastEntity();
662681

663682
$entity->tenth = "\xB1\x31";
683+
684+
$entity->toDatabase();
664685
}
665686

666687
/**
@@ -675,7 +696,7 @@ public function testCastAsJSONSyntaxError(): void
675696
$entity = new Entity();
676697
$entity->casts['dummy'] = 'json[array]';
677698

678-
return $entity->castAs($value, 'dummy');
699+
return $entity->castAs($value, 'dummy', 'fromDatabase');
679700
}, null, Entity::class))('{ this is bad string');
680701
}
681702

@@ -692,7 +713,7 @@ public function testCastAsJSONAnotherErrorDepth(): void
692713
$entity = new Entity();
693714
$entity->casts['dummy'] = 'json[array]';
694715

695-
return $entity->castAs($value, 'dummy');
716+
return $entity->castAs($value, 'dummy', 'fromDatabase');
696717
}, null, Entity::class))($string);
697718
}
698719

@@ -709,7 +730,7 @@ public function testCastAsJSONControlCharCheck(): void
709730
$entity = new Entity();
710731
$entity->casts['dummy'] = 'json[array]';
711732

712-
return $entity->castAs($value, 'dummy');
733+
return $entity->castAs($value, 'dummy', 'fromDatabase');
713734
}, null, Entity::class))($string);
714735
}
715736

@@ -726,22 +747,24 @@ public function testCastAsJSONStateMismatch(): void
726747
$entity = new Entity();
727748
$entity->casts['dummy'] = 'json[array]';
728749

729-
return $entity->castAs($value, 'dummy');
750+
return $entity->castAs($value, 'dummy', 'fromDatabase');
730751
}, null, Entity::class))($string);
731752
}
732753

733754
public function testCastSetter(): void
734755
{
735-
$string = '321 String with numbers 123';
736-
$entity = $this->getCastEntity();
737-
$entity->first = $string;
756+
$entity = $this->getCastEntity();
738757

739758
$entity->cast(false);
759+
$string = '321 String with numbers 123';
760+
$entity->first = $string;
740761

741762
$this->assertIsString($entity->first);
742763
$this->assertSame($string, $entity->first);
743764

744765
$entity->cast(true);
766+
$string = '321 String with numbers 123';
767+
$entity->first = $string;
745768

746769
$this->assertIsInt($entity->first);
747770
$this->assertSame((int) $string, $entity->first);
@@ -1285,16 +1308,16 @@ protected function getCastNullableEntity()
12851308
return new class () extends Entity {
12861309
protected $attributes = [
12871310
'string_null' => null,
1288-
'string_empty' => null,
1311+
'string_empty' => '',
12891312
'integer_null' => null,
1290-
'integer_0' => null,
1313+
'integer_0' => 0,
12911314
'string_value_not_null' => 'value',
12921315
];
12931316
protected $_original = [
12941317
'string_null' => null,
1295-
'string_empty' => null,
1318+
'string_empty' => '',
12961319
'integer_null' => null,
1297-
'integer_0' => null,
1320+
'integer_0' => 0,
12981321
'string_value_not_null' => 'value',
12991322
];
13001323

0 commit comments

Comments
 (0)