get($model, 'employee_sites', null, []); $resultEmpty = $cast->get($model, 'employee_sites', '', []); // THEN expect($resultNull)->toBe([]) ->and($resultEmpty)->toBe([]); }); it('gets array of strings from json string', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); $json = json_encode(['PA', 'LY', 'NA']); // WHEN $sites = $cast->get($model, 'employee_sites', $json, []); // THEN expect($sites)->toBe(['PA', 'LY', 'NA']); }); it('gets array of strings from mixed array', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); $input = ['PA', new ShopperSiteCode('LY'), 123]; // WHEN $sites = $cast->get($model, 'employee_sites', $input, []); // THEN expect($sites)->toBe(['PA', 'LY', '123']); }); it('sets json empty array when value is null', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); // WHEN $value = $cast->set($model, 'employee_sites', null, []); // THEN expect($value)->toBe('[]'); }); it('sets json array from array of strings', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); // WHEN $value = $cast->set($model, 'employee_sites', ['PA', 'LY', 'NA'], []); // THEN expect($value)->toBe(json_encode(['PA', 'LY', 'NA'])); }); it('sets json array from array of Stringable objects', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); // WHEN $value = $cast->set($model, 'employee_sites', [new ShopperSiteCode('PA'), new ShopperSiteCode('LY')], []); // THEN expect($value)->toBe(json_encode(['PA', 'LY'])); }); it('throws exception when setting invalid type', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); // WHEN expect(fn () => $cast->set($model, 'employee_sites', 12345, [])) // THEN ->toThrow(\UnexpectedValueException::class, 'Employee sites must be an array, string or null.'); }); it('throws exception when setting array with non-convertible items', function (): void { // GIVEN $cast = new EmployeeSitesCast(); $model = \Mockery::mock(Model::class); // WHEN expect(fn () => $cast->set($model, 'employee_sites', [new \stdClass()], [])) // THEN ->toThrow(\UnexpectedValueException::class, 'Each employee site must be convertable to string.'); });