Skip to content

Commit ab0b9b0

Browse files
Add additional tests for Rule::array validation scenarios (#54844)
* Added tests for Rule::array with empty arrays, duplicate keys, and numeric values. * Added tests for array validation with objects, nullable values, and empty arrays * Add test for getCount with valid excludeId condition
1 parent df80a44 commit ab0b9b0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

tests/Validation/ValidationArrayRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
1818

1919
$this->assertSame('array', (string) $rule);
2020

21+
$rule = Rule::array([]);
22+
$this->assertSame('array', (string) $rule);
23+
2124
$rule = Rule::array('key_1', 'key_2', 'key_3');
2225

2326
$this->assertSame('array:key_1,key_2,key_3', (string) $rule);
@@ -37,6 +40,12 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
3740
$rule = Rule::array([ArrayKeysBacked::key_1, ArrayKeysBacked::key_2, ArrayKeysBacked::key_3]);
3841

3942
$this->assertSame('array:key_1,key_2,key_3', (string) $rule);
43+
44+
$rule = Rule::array(['key_1', 'key_1']);
45+
$this->assertSame('array:key_1,key_1', (string) $rule);
46+
47+
$rule = Rule::array([1, 2, 3]);
48+
$this->assertSame('array:1,2,3', (string) $rule);
4049
}
4150

4251
public function testArrayValidation()
@@ -46,6 +55,18 @@ public function testArrayValidation()
4655
$v = new Validator($trans, ['foo' => 'not an array'], ['foo' => Rule::array()]);
4756
$this->assertTrue($v->fails());
4857

58+
$v = new Validator($trans, ['foo' => (object) ['key_1' => 'bar']], ['foo' => Rule::array()]);
59+
$this->assertTrue($v->fails());
60+
61+
$v = new Validator($trans, ['foo' => null], ['foo' => ['nullable', Rule::array()]]);
62+
$this->assertTrue($v->passes());
63+
64+
$v = new Validator($trans, ['foo' => []], ['foo' => Rule::array()]);
65+
$this->assertTrue($v->passes());
66+
67+
$v = new Validator($trans, ['foo' => ['key_1' => []]], ['foo' => Rule::array(['key_1'])]);
68+
$this->assertTrue($v->passes());
69+
4970
$v = new Validator($trans, ['foo' => ['bar']], ['foo' => (string) Rule::array()]);
5071
$this->assertTrue($v->passes());
5172

tests/Validation/ValidationDatabasePresenceVerifierTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,18 @@ public function testBasicCountWithClosures()
6060

6161
$this->assertEquals(100, $verifier->getCount('table', 'column', 'value', null, null, $extra));
6262
}
63+
64+
public function testGetCountWithValidExcludeId()
65+
{
66+
$verifier = new DatabasePresenceVerifier($db = m::mock(ConnectionResolverInterface::class));
67+
$verifier->setConnection('connection');
68+
$db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock(stdClass::class));
69+
$conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock(stdClass::class));
70+
$builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
71+
$builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
72+
$builder->shouldReceive('where')->with('id', '<>', 123)->andReturn($builder);
73+
$builder->shouldReceive('count')->once()->andReturn(100);
74+
75+
$this->assertEquals(100, $verifier->getCount('table', 'column', 'value', 123, 'id', []));
76+
}
6377
}

0 commit comments

Comments
 (0)