name()), 0, 20); $this->admin = new User(); $this->admin->name = $testName . '_admin_user'; $this->admin->email = $testName . '_admin_' . uniqid() . '@example.com'; $this->admin->password = bcrypt('password'); $this->admin->type = RoleTypes::ADMIN; $this->admin->status = 1; $this->admin->save(); // Create a test segment $this->segment = new Segment(); $this->segment->name = substr($testName . '_segment', 0, 45); $this->segment->company_module_id = 1; $this->segment->save(); } /** * Test updating an existing segment price constant. */ public function test_it_can_update_segment_price_constant() { // A R R A N G E $reference = 'update_segment_price_constant_ref'; $value = 100.50; $constant = new SegmentConstant(); $constant->segment_id = $this->segment->id; $constant->reference = $reference; $constant->value = [50.00]; $constant->save(); $payload = [ 'reference' => $reference, 'value' => $value ]; // A C T $response = $this->actingAs($this->admin, 'api') ->putJson("/api/v1/segment/price/update/{$this->segment->id}", $payload); // A S S E R T $response->assertStatus(200); $response->assertJsonStructure([ 'title', 'message', 'payload' => [ 'data' => [ 'id', 'reference', 'value' ] ] ]); $constant = SegmentConstant::query() ->where('segment_id', $this->segment->id) ->where('reference', $reference) ->first(); $this->assertNotNull($constant); $this->assertEquals([$value], $constant->value); } /** * Test creating a constant if it doesn't exist and then updating it. */ public function test_it_creates_constant_if_not_exists_then_updates_it() { // A R R A N G E $reference = 'create_segment_price_constant_ref'; $value = 75.25; $payload = [ 'reference' => $reference, 'value' => $value ]; // A C T $response = $this->actingAs($this->admin, 'api') ->putJson("/api/v1/segment/price/update/{$this->segment->id}", $payload); // A S S E R T $response->assertStatus(200); $constant = SegmentConstant::query() ->where('segment_id', $this->segment->id) ->where('reference', $reference) ->first(); $this->assertNotNull($constant); $this->assertEquals([$value], $constant->value); } }