|
21 | 21 | use Illuminate\Events\Dispatcher; |
22 | 22 | use Illuminate\Filesystem\Filesystem; |
23 | 23 | use Illuminate\Support\Carbon; |
| 24 | +use InvalidArgumentException; |
24 | 25 | use Mockery as m; |
25 | 26 | use PHPUnit\Framework\Attributes\DataProvider; |
26 | 27 | use PHPUnit\Framework\TestCase; |
@@ -516,4 +517,148 @@ protected static function getTestDate() |
516 | 517 | { |
517 | 518 | return '2030-07-25 12:13:14 UTC'; |
518 | 519 | } |
| 520 | + |
| 521 | + public function testItGetsAsString() |
| 522 | + { |
| 523 | + $repo = $this->getRepository(); |
| 524 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('bar'); |
| 525 | + $this->assertSame('bar', $repo->string('foo')); |
| 526 | + } |
| 527 | + |
| 528 | + public function testItGetsAsStringWithDefault() |
| 529 | + { |
| 530 | + $repo = $this->getRepository(); |
| 531 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(null); |
| 532 | + $this->assertSame('default', $repo->string('foo', 'default')); |
| 533 | + } |
| 534 | + |
| 535 | + public function testItThrowsExceptionWhenGettingNonStringAsString() |
| 536 | + { |
| 537 | + $this->expectException(InvalidArgumentException::class); |
| 538 | + $this->expectExceptionMessage('Cache value for key [foo] must be a string, integer given.'); |
| 539 | + |
| 540 | + $repo = $this->getRepository(); |
| 541 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(123); |
| 542 | + $repo->string('foo'); |
| 543 | + } |
| 544 | + |
| 545 | + public function testItGetsAsInteger() |
| 546 | + { |
| 547 | + $repo = $this->getRepository(); |
| 548 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(123); |
| 549 | + $this->assertSame(123, $repo->integer('foo')); |
| 550 | + } |
| 551 | + |
| 552 | + public function testItGetsAsIntegerWithDefault() |
| 553 | + { |
| 554 | + $repo = $this->getRepository(); |
| 555 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(null); |
| 556 | + $this->assertSame(456, $repo->integer('foo', 456)); |
| 557 | + } |
| 558 | + |
| 559 | + public function testItGetsAsIntegerFromNumericString() |
| 560 | + { |
| 561 | + $repo = $this->getRepository(); |
| 562 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('123'); |
| 563 | + $this->assertSame(123, $repo->integer('foo')); |
| 564 | + } |
| 565 | + |
| 566 | + public function testItThrowsExceptionWhenGettingNonIntegerAsInteger() |
| 567 | + { |
| 568 | + $this->expectException(InvalidArgumentException::class); |
| 569 | + $this->expectExceptionMessage('Cache value for key [foo] must be an integer, string given.'); |
| 570 | + |
| 571 | + $repo = $this->getRepository(); |
| 572 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('bar'); |
| 573 | + $repo->integer('foo'); |
| 574 | + } |
| 575 | + |
| 576 | + public function testItThrowsExceptionWhenGettingFloatStringAsInteger() |
| 577 | + { |
| 578 | + $this->expectException(InvalidArgumentException::class); |
| 579 | + $this->expectExceptionMessage('Cache value for key [foo] must be an integer, string given.'); |
| 580 | + |
| 581 | + $repo = $this->getRepository(); |
| 582 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('1.5'); |
| 583 | + $repo->integer('foo'); |
| 584 | + } |
| 585 | + |
| 586 | + public function testItGetsAsFloat() |
| 587 | + { |
| 588 | + $repo = $this->getRepository(); |
| 589 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(1.5); |
| 590 | + $this->assertSame(1.5, $repo->float('foo')); |
| 591 | + } |
| 592 | + |
| 593 | + public function testItGetsAsFloatWithDefault() |
| 594 | + { |
| 595 | + $repo = $this->getRepository(); |
| 596 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(null); |
| 597 | + $this->assertSame(2.5, $repo->float('foo', 2.5)); |
| 598 | + } |
| 599 | + |
| 600 | + public function testItGetsAsFloatFromNumericString() |
| 601 | + { |
| 602 | + $repo = $this->getRepository(); |
| 603 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('1.5'); |
| 604 | + $this->assertSame(1.5, $repo->float('foo')); |
| 605 | + } |
| 606 | + |
| 607 | + public function testItThrowsExceptionWhenGettingNonFloatAsFloat() |
| 608 | + { |
| 609 | + $this->expectException(InvalidArgumentException::class); |
| 610 | + $this->expectExceptionMessage('Cache value for key [foo] must be a float, string given.'); |
| 611 | + |
| 612 | + $repo = $this->getRepository(); |
| 613 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('bar'); |
| 614 | + $repo->float('foo'); |
| 615 | + } |
| 616 | + |
| 617 | + public function testItGetsAsBoolean() |
| 618 | + { |
| 619 | + $repo = $this->getRepository(); |
| 620 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(true); |
| 621 | + $this->assertTrue($repo->boolean('foo')); |
| 622 | + } |
| 623 | + |
| 624 | + public function testItGetsAsBooleanWithDefault() |
| 625 | + { |
| 626 | + $repo = $this->getRepository(); |
| 627 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(null); |
| 628 | + $this->assertFalse($repo->boolean('foo', false)); |
| 629 | + } |
| 630 | + |
| 631 | + public function testItThrowsExceptionWhenGettingNonBooleanAsBoolean() |
| 632 | + { |
| 633 | + $this->expectException(InvalidArgumentException::class); |
| 634 | + $this->expectExceptionMessage('Cache value for key [foo] must be a boolean, string given.'); |
| 635 | + |
| 636 | + $repo = $this->getRepository(); |
| 637 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('bar'); |
| 638 | + $repo->boolean('foo'); |
| 639 | + } |
| 640 | + |
| 641 | + public function testItGetsAsArray() |
| 642 | + { |
| 643 | + $repo = $this->getRepository(); |
| 644 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(['bar', 'baz']); |
| 645 | + $this->assertSame(['bar', 'baz'], $repo->array('foo')); |
| 646 | + } |
| 647 | + |
| 648 | + public function testItGetsAsArrayWithDefault() |
| 649 | + { |
| 650 | + $repo = $this->getRepository(); |
| 651 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(null); |
| 652 | + $this->assertSame(['default'], $repo->array('foo', ['default'])); |
| 653 | + } |
| 654 | + |
| 655 | + public function testItThrowsExceptionWhenGettingNonArrayAsArray() |
| 656 | + { |
| 657 | + $this->expectException(InvalidArgumentException::class); |
| 658 | + $this->expectExceptionMessage('Cache value for key [foo] must be an array, string given.'); |
| 659 | + |
| 660 | + $repo = $this->getRepository(); |
| 661 | + $repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('bar'); |
| 662 | + $repo->array('foo'); |
| 663 | + } |
519 | 664 | } |
0 commit comments