seed(TerritoryReferenceSeeder::class); $territory = app(InvestmentGeography::class)->publicReferences()->where('location_type', 'province')->value('id'); $this->assertNotEmpty($territory); $definition = ['fields' => [ 'branch' => ['label' => 'Synthetic branch', 'type' => 'select', 'required' => true, 'options' => ['yes' => 'Yes', 'no' => 'No']], 'territory_id' => ['label' => 'Location', 'type' => 'territory', 'required' => false, 'when' => ['field' => 'branch', 'equals' => 'yes']], 'location_source' => ['label' => 'Source', 'type' => 'text', 'required' => false, 'when' => ['field' => 'branch', 'equals' => 'yes']], ]]; $desk = app(VirtualDesk::class); $payload = ['branch' => 'no', 'territory_id' => $territory]; $this->assertSame($payload, $desk->validateFields($payload, $definition, true)); $payload['branch'] = 'yes'; try { $desk->validateFields($payload, $definition, true); $this->fail('An applicable selected location must retain its source requirement.'); } catch (ValidationException $error) { $this->assertArrayHasKey('payload.location_source', $error->errors()); } $payload['location_source'] = 'Synthetic QA use of the packaged reference'; $this->assertSame($payload, $desk->validateFields($payload, $definition, true)); $this->assertSame(['branch' => 'yes'], $desk->validateFields(['branch' => 'yes'], $definition, true)); } }