Skip to content

Commit d0da072

Browse files
committed
Add snap and snapKey methods to Draw class
- implementation - test - readme - example
1 parent 7ab5f1d commit d0da072

9 files changed

+115
-29
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ vendor
55
coverage
66
.idea
77
coverage.clover
8+
.phpunit.result.cache

Diff for: .phpunit.result.cache

-1
This file was deleted.

Diff for: README.md

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ composer require hi-folks/rando-php
4343

4444
## Usage
4545

46+
In order to import the right class:
47+
```php
48+
use HiFolks\RandoPhp\Randomize;
49+
```
50+
4651
### Generate Char
4752

4853
Sometimes you want to obtain a random char, for example, a numeric char:
@@ -192,6 +197,18 @@ $randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->noDupl
192197
### Draw random stuff
193198
If you have a list of values and you want to extract/select/draw one or more elements,
194199
you could use Draw class instead of Randomize.
200+
For using Draw class you need to import:
201+
```php
202+
use HiFolks\RandoPhp\Draw;
203+
```
204+
205+
#### Suggest which programming language you could use in your next project (random)
206+
```php
207+
use HiFolks\RandoPhp\Draw;
208+
209+
$randomLanguage = Draw::sample(["PHP", "Python", "Golang", "Javascript"])->snap();
210+
var_dump($randomLanguage);
211+
```
195212

196213
#### Suggest which JS framework you could use in your next project (random)
197214
```php

Diff for: examples/basic.php

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
$randomJs = Draw::sample($array)->count(2)->preserveKeys()->extract();
5252
var_dump($randomJs);
5353

54+
echo "--- SUGGEST ME THE PROGRAMMING LANGUAGE TO USE ".PHP_EOL;
55+
$randomLanguage = Draw::sample(["PHP", "Python", "Golang", "Javascript"])->snap();
56+
var_dump($randomLanguage);
57+
5458

5559
echo "--- GENERATE STRING, 10 alphanumeric chars length".PHP_EOL;
5660
$randomChars = Randomize::sequence()->chars()->alphanumeric()->count(10)->asString()->generate();
@@ -63,3 +67,4 @@
6367
echo "--- GENERATE STRING, 10 alphabetical chars length".PHP_EOL;
6468
$randomChars = Randomize::sequence()->chars()->alpha()->count(10)->asString()->generate();
6569
var_dump($randomChars);
70+

Diff for: phpunit.xml.dist

+19-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" />
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
<report>
8+
<clover outputFile="build/logs/clover.xml"/>
9+
<html outputDirectory="build/coverage"/>
10+
<text outputFile="build/coverage.txt"/>
11+
</report>
12+
</coverage>
13+
<testsuites>
14+
<testsuite name="Test Suite">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<logging>
19+
<junit outputFile="build/report.junit.xml"/>
20+
</logging>
2921
</phpunit>

Diff for: phpunit.xml.dist.bak

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" />
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

Diff for: src/Models/Sample.php

+12
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ public function implode(bool $implode = true): self
146146
return $this;
147147
}
148148

149+
public function snap()
150+
{
151+
$key = $this->count(1)->preserveKeys()->extractKeys();
152+
return $this->array[$key];
153+
}
154+
public function snapKey()
155+
{
156+
$key = $this->count(1)->preserveKeys()->extractKeys();
157+
return $key;
158+
}
159+
160+
149161
/**
150162
* Extract and returns a sample random array, from the original array
151163
*

Diff for: tests/DrawSampleTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,33 @@ public function random_extract_preservekeys()
7373
$this->assertSame($count, count($sample), "Check extract count is correct");
7474
}
7575

76+
/** @test */
77+
public function random_snap()
78+
{
79+
$array=["testa" => "Testa", "croce" => "Croce"];
80+
81+
$sample = Draw::sample($array)->snap();
82+
$this->assertIsString($sample, "Element is a string");
83+
$this->assertContains($sample,["Testa", "Croce"], "Element is in Array");
84+
85+
$sample = Draw::sample($array)->snapKey();
86+
$this->assertIsString($sample, "Element is a string");
87+
$this->assertContains($sample,["testa", "croce"], "Element is in Array");
88+
89+
$array=[
90+
"first" => ["This", "is", "First"],
91+
"second" => ["This" , "is", "Second" ]
92+
];
93+
$sample = Draw::sample($array)->snap();
94+
$this->assertIsArray($sample, "Element is an array");
95+
96+
$sample = Draw::sample($array)->snapKey();
97+
$this->assertIsString($sample, "Element is a string");
98+
$this->assertContains($sample,["first", "second"], "Element is a valid key");
99+
100+
$array=[10,11,12,13];
101+
$sample = Draw::sample($array)->snap();
102+
$this->assertIsInt($sample, "Element is an Integer");
103+
}
104+
76105
}

Diff for: tests/RandomDateTimeTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public function random_datetime_range_format()
4141
$day = Randomize::datetime()->range('01-10-2020 00:00:00', '01-10-2020 12:00:00')->format("d")->generate();
4242
$this->assertSame("01", $day , "Extracting the right day");
4343
$year = Randomize::datetime()->range('01-01-2020', 'now')->format("Y")->generate();
44-
$this->assertSame("2020", $year , "Extracting the right year");
44+
$this->assertIsString($year, "Generated Y is a string");
45+
$this->assertSame(strlen($year), 4, "Generated Y is 4 chars");
46+
4547

4648
}
4749

0 commit comments

Comments
 (0)