Files

67 lines
2.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Admin\ActionInput;
final class ActionInputTest extends TestCase {
private function post( array $o = array() ): array {
return $o + array( 'title' => 'Eat a bean', 'description' => '', 'amount' => '5.00', 'mode' => 'every', 'goal_id' => '0', 'sort' => '' );
}
private function v( array $o = array(), int $min = 100 ): array {
return ActionInput::validate( $this->post( $o ), $min, 'USD', array( 3, 4 ), 40 );
}
public function test_valid_input(): void {
$r = $this->v( array( 'title' => ' <b>Eat</b> a bean ', 'description' => 'Yum', 'goal_id' => '3' ) );
$this->assertNull( $r['error'] );
$this->assertSame( array( 'goal_id' => 3, 'title' => 'Eat a bean', 'description' => 'Yum', 'amount_minor' => 500, 'mode' => 'every', 'sort' => 40 ), $r['data'] );
}
public function test_title_required_and_max_120(): void {
$this->assertNull( $this->v( array( 'title' => ' ' ) )['data'] );
$this->assertNull( $this->v( array( 'title' => str_repeat( 'a', 121 ) ) )['data'] );
$this->assertNotNull( $this->v( array( 'title' => str_repeat( 'a', 120 ) ) )['data'] );
}
public function test_description_max_255(): void {
$this->assertNull( $this->v( array( 'description' => str_repeat( 'a', 256 ) ) )['data'] );
$this->assertNotNull( $this->v( array( 'description' => str_repeat( 'a', 255 ) ) )['data'] );
}
public function test_amount_must_reach_the_minimum(): void {
$low = $this->v( array( 'amount' => '0.99' ) );
$this->assertNull( $low['data'] );
$this->assertStringContainsString( 'minimum', (string) $low['error'] );
$this->assertNotNull( $this->v( array( 'amount' => '1.00' ) )['data'] );
$this->assertNull( $this->v( array( 'amount' => '4.99' ), 500 )['data'] );
$this->assertNotNull( $this->v( array( 'amount' => '5' ), 500 )['data'] );
}
public function test_bad_amounts(): void {
foreach ( array( '', '0', '-5', 'abc', '5.123' ) as $bad ) {
$this->assertNull( $this->v( array( 'amount' => $bad ) )['data'], "amount '$bad'" );
}
}
public function test_mode_whitelist(): void {
$this->assertNotNull( $this->v( array( 'mode' => 'exact' ) )['data'] );
$this->assertNull( $this->v( array( 'mode' => 'always' ) )['data'] );
$this->assertNull( $this->v( array( 'mode' => null ) )['data'] );
}
public function test_goal_must_exist_or_be_zero(): void {
$this->assertNotNull( $this->v( array( 'goal_id' => '4' ) )['data'] );
$this->assertNull( $this->v( array( 'goal_id' => '99' ) )['data'] );
$this->assertNull( $this->v( array( 'goal_id' => '-1' ) )['data'] );
}
public function test_sort(): void {
$this->assertSame( 7, $this->v( array( 'sort' => '7' ) )['data']['sort'] );
$this->assertNull( $this->v( array( 'sort' => 'x' ) )['data'] );
}
}