|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of https://github.com/josantonius/php-json repository. |
| 7 | + * |
| 8 | + * (c) Josantonius <hello@josantonius.dev> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Josantonius\Json\Tests; |
| 15 | + |
| 16 | +use Josantonius\Json\Exception\GetFileException; |
| 17 | +use Josantonius\Json\Exception\UnavailableMethodException; |
| 18 | +use Josantonius\Json\Json; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class PushMethodTest extends TestCase |
| 22 | +{ |
| 23 | + private string $filepath = __DIR__ . '/filename.json'; |
| 24 | + |
| 25 | + private string $url = 'https://raw.githubusercontent.com/josantonius/php-json/main/composer.json'; |
| 26 | + |
| 27 | + public function setUp(): void |
| 28 | + { |
| 29 | + parent::setup(); |
| 30 | + } |
| 31 | + |
| 32 | + public function tearDown(): void |
| 33 | + { |
| 34 | + if (file_exists($this->filepath)) { |
| 35 | + unlink($this->filepath); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function testShouldPushArrayOnJsonFile(): void |
| 40 | + { |
| 41 | + $jsonFile = new Json($this->filepath); |
| 42 | + |
| 43 | + $jsonFile->set([['foo' => 'bar']]); |
| 44 | + |
| 45 | + $jsonFile->push(['bar' => 'foo']); |
| 46 | + |
| 47 | + $this->assertEquals( |
| 48 | + [['foo' => 'bar'], ['bar' => 'foo']], |
| 49 | + json_decode(file_get_contents($this->filepath), true) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testShouldPushObjectOnJsonFile(): void |
| 54 | + { |
| 55 | + $jsonFile = new Json($this->filepath); |
| 56 | + |
| 57 | + $jsonFile->set([['foo' => 'bar']]); |
| 58 | + |
| 59 | + $jsonFile->push((object) ['bar' => 'foo']); |
| 60 | + |
| 61 | + $this->assertEquals( |
| 62 | + [['foo' => 'bar'], ['bar' => 'foo']], |
| 63 | + json_decode(file_get_contents($this->filepath), true) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public function testShouldThrowExceptionIfPushMethodIsUsedWithRemoteFile(): void |
| 68 | + { |
| 69 | + $jsonFile = new Json($this->url); |
| 70 | + |
| 71 | + $this->expectException(UnavailableMethodException::class); |
| 72 | + |
| 73 | + $jsonFile->push(['bar' => 'foo']); |
| 74 | + } |
| 75 | + |
| 76 | + public function testShouldThrowExceptionIfFileCannotBeObtained(): void |
| 77 | + { |
| 78 | + $jsonFile = new Json($this->filepath); |
| 79 | + |
| 80 | + unlink($this->filepath); |
| 81 | + |
| 82 | + $this->expectException(GetFileException::class); |
| 83 | + |
| 84 | + $jsonFile->push((object) ['bar' => 'foo']); |
| 85 | + } |
| 86 | + |
| 87 | + public function testShouldThrowExceptionIfRemoteFileCannotBeObtained(): void |
| 88 | + { |
| 89 | + $jsonFile = new Json($this->url . 'wrong'); |
| 90 | + |
| 91 | + $this->expectException(GetFileException::class); |
| 92 | + |
| 93 | + $jsonFile->push((object) ['bar' => 'foo']); |
| 94 | + } |
| 95 | +} |
0 commit comments