mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 10:47:00 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			205 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			205 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * CategoryControllerTest.php
 | |
|  * Copyright (c) 2017 thegrumpydictator@gmail.com
 | |
|  *
 | |
|  * This file is part of Firefly III.
 | |
|  *
 | |
|  * Firefly III is free software: you can redistribute it and/or modify
 | |
|  * it under the terms of the GNU General Public License as published by
 | |
|  * the Free Software Foundation, either version 3 of the License, or
 | |
|  * (at your option) any later version.
 | |
|  *
 | |
|  * Firefly III is distributed in the hope that it will be useful,
 | |
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | |
|  * GNU General Public License for more details.
 | |
|  *
 | |
|  * You should have received a copy of the GNU General Public License
 | |
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
 | |
|  */
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace Tests\Feature\Controllers;
 | |
| 
 | |
| use Carbon\Carbon;
 | |
| use FireflyIII\Models\Category;
 | |
| use FireflyIII\Models\TransactionJournal;
 | |
| use FireflyIII\Repositories\Account\AccountRepositoryInterface;
 | |
| use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
 | |
| use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
 | |
| use Illuminate\Support\Collection;
 | |
| use Log;
 | |
| use Tests\TestCase;
 | |
| 
 | |
| /**
 | |
|  * Class CategoryControllerTest
 | |
|  *
 | |
|  * @SuppressWarnings(PHPMD.TooManyPublicMethods)
 | |
|  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 | |
|  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 | |
|  */
 | |
| class CategoryControllerTest extends TestCase
 | |
| {
 | |
|     /**
 | |
|      *
 | |
|      */
 | |
|     public function setUp(): void
 | |
|     {
 | |
|         parent::setUp();
 | |
|         Log::debug(sprintf('Now in %s.', \get_class($this)));
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      */
 | |
|     public function testCreate(): void
 | |
|     {
 | |
|         Log::debug('TestCreate()');
 | |
|         // mock stuff
 | |
|         $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $accountRepos  = $this->mock(AccountRepositoryInterface::class);
 | |
|         $journalRepos  = $this->mock(JournalRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
| 
 | |
|         $this->be($this->user());
 | |
|         $response = $this->get(route('categories.create'));
 | |
|         $response->assertStatus(200);
 | |
|         // has bread crumb
 | |
|         $response->assertSee('<ol class="breadcrumb">');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      */
 | |
|     public function testDelete(): void
 | |
|     {
 | |
|         Log::debug('Test Delete()');
 | |
|         // mock stuff
 | |
|         $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $accountRepos  = $this->mock(AccountRepositoryInterface::class);
 | |
|         $journalRepos  = $this->mock(JournalRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
| 
 | |
|         $this->be($this->user());
 | |
|         $response = $this->get(route('categories.delete', [1]));
 | |
|         $response->assertStatus(200);
 | |
|         // has bread crumb
 | |
|         $response->assertSee('<ol class="breadcrumb">');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      */
 | |
|     public function testDestroy(): void
 | |
|     {
 | |
|         Log::debug('Test destroy()');
 | |
|         // mock stuff
 | |
|         $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $accountRepos  = $this->mock(AccountRepositoryInterface::class);
 | |
|         $journalRepos  = $this->mock(JournalRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
| 
 | |
|         $categoryRepos->shouldReceive('destroy')->andReturn(true);
 | |
| 
 | |
|         $this->session(['categories.delete.uri' => 'http://localhost']);
 | |
|         $this->be($this->user());
 | |
|         $response = $this->post(route('categories.destroy', [1]));
 | |
|         $response->assertStatus(302);
 | |
|         $response->assertSessionHas('success');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      */
 | |
|     public function testEdit(): void
 | |
|     {
 | |
|         Log::debug('Test edit()');
 | |
|         // mock stuff
 | |
|         $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $accountRepos  = $this->mock(AccountRepositoryInterface::class);
 | |
|         $journalRepos  = $this->mock(JournalRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
| 
 | |
|         $this->be($this->user());
 | |
|         $response = $this->get(route('categories.edit', [1]));
 | |
|         $response->assertStatus(200);
 | |
|         // has bread crumb
 | |
|         $response->assertSee('<ol class="breadcrumb">');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      */
 | |
|     public function testIndex(): void
 | |
|     {
 | |
|         Log::debug('Test index()');
 | |
|         // mock stuff
 | |
|         $category      = factory(Category::class)->make();
 | |
|         $categoryRepos = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $accountRepos  = $this->mock(AccountRepositoryInterface::class);
 | |
|         $journalRepos  = $this->mock(JournalRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
|         $categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$category]))->once();
 | |
|         $categoryRepos->shouldReceive('lastUseDate')->andReturn(new Carbon)->once();
 | |
| 
 | |
|         $this->be($this->user());
 | |
|         $response = $this->get(route('categories.index'));
 | |
|         $response->assertStatus(200);
 | |
|         // has bread crumb
 | |
|         $response->assertSee('<ol class="breadcrumb">');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      * @covers \FireflyIII\Http\Requests\CategoryFormRequest
 | |
|      */
 | |
|     public function testStore(): void
 | |
|     {
 | |
|         Log::debug('Test store()');
 | |
|         $accountRepos = $this->mock(AccountRepositoryInterface::class);
 | |
|         $repository   = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $journalRepos = $this->mock(JournalRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
|         $repository->shouldReceive('findNull')->andReturn(new Category);
 | |
|         $repository->shouldReceive('store')->andReturn(new Category);
 | |
| 
 | |
|         $this->session(['categories.create.uri' => 'http://localhost']);
 | |
| 
 | |
|         $data = [
 | |
|             'name' => 'New Category ' . random_int(1000, 9999),
 | |
|         ];
 | |
|         $this->be($this->user());
 | |
|         $response = $this->post(route('categories.store'), $data);
 | |
|         $response->assertStatus(302);
 | |
|         $response->assertSessionHas('success');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @covers \FireflyIII\Http\Controllers\CategoryController
 | |
|      * @covers \FireflyIII\Http\Requests\CategoryFormRequest
 | |
|      */
 | |
|     public function testUpdate(): void
 | |
|     {
 | |
|         Log::debug('Test update()');
 | |
|         $category     = Category::first();
 | |
|         $repository   = $this->mock(CategoryRepositoryInterface::class);
 | |
|         $journalRepos = $this->mock(JournalRepositoryInterface::class);
 | |
|         $accountRepos = $this->mock(AccountRepositoryInterface::class);
 | |
|         $journalRepos->shouldReceive('firstNull')->once()->andReturn(TransactionJournal::first());
 | |
|         $repository->shouldReceive('update');
 | |
|         $repository->shouldReceive('findNull')->andReturn($category);
 | |
| 
 | |
|         $this->session(['categories.edit.uri' => 'http://localhost']);
 | |
| 
 | |
|         $data = [
 | |
|             'name'   => 'Updated Category ' . random_int(1000, 9999),
 | |
|             'active' => 1,
 | |
|         ];
 | |
|         $this->be($this->user());
 | |
|         $response = $this->post(route('categories.update', [1]), $data);
 | |
|         $response->assertStatus(302);
 | |
|         $response->assertSessionHas('success');
 | |
|     }
 | |
| }
 |