Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
14 / 14 |
EnumExpectation | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
14 / 14 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
jsonSerialize | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getType | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
expect | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
getOptions | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setOptions | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
<?php | |
/** | |
* Copyright (c) Tony Bogdanov <tonybogdanov@gmail.com> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace DataExpectation; | |
use DataExpectation\Exceptions\UnexpectedDataException; | |
use DataExpectation\Traits\IndentTrait; | |
/** | |
* Class EnumExpectation | |
* | |
* @package DataExpectation | |
* @author Tony Bogdanov <tonybogdanov@gmail.com> | |
*/ | |
class EnumExpectation extends AbstractExpectation { | |
use IndentTrait; | |
/** | |
* @var array | |
*/ | |
protected $options; | |
/** | |
* EnumExpectation constructor. | |
* | |
* @param array $options | |
*/ | |
public function __construct( array $options ) { | |
$this->setOptions( array_values( $options ) ); | |
} | |
/** | |
* @return array | |
*/ | |
public function jsonSerialize(): array { | |
return array_replace( parent::jsonSerialize(), [ | |
'expectationArguments' => [ $this->options ], | |
] ); | |
} | |
/** | |
* @return string | |
*/ | |
public function getType(): string { | |
$result = "enum (\n"; | |
foreach ( $this->getOptions() as $option ) { | |
$result .= $this->indent( json_encode( $option ) . ";\n" ); | |
} | |
return $result . ')'; | |
} | |
/** | |
* @param $data | |
* @param string|null $path | |
* | |
* @return $this | |
* @throws UnexpectedDataException | |
*/ | |
public function expect( $data, string $path = null ) { | |
if ( ! in_array( $data, $this->getOptions(), true ) ) { | |
throw new UnexpectedDataException( $data, $this, $path ); | |
} | |
return $this; | |
} | |
/** | |
* @return array | |
*/ | |
public function getOptions(): array { | |
return $this->options; | |
} | |
/** | |
* @param array $options | |
* | |
* @return $this | |
*/ | |
public function setOptions( array $options ) { | |
$this->options = $options; | |
return $this; | |
} | |
} |