FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
FolderNavTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 Author: Steffen Weber
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\UI;
21 
25 use Mockery as M;
26 
27 function Traceback_uri()
28 {
29  return 'uri';
30 }
31 
32 class FolderNavTest extends \PHPUnit\Framework\TestCase
33 {
35  private $dbManager;
37  private $folderDao;
39  private $folderNav;
41  private $uri;
42 
43  protected function setUp()
44  {
45  $this->folderDao = M::mock(FolderDao::class);
46  $this->dbManager = M::mock(DbManager::class);
47  $this->folderNav = new FolderNav($this->dbManager,$this->folderDao);
48  $this->uri = Traceback_uri();
49  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
50  }
51 
52  protected function tearDown()
53  {
54  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
55  M::close();
56  }
57 
58  protected function getFormattedItem($row)
59  {
60  return Reflectory::invokeObjectsMethodnameWith($this->folderNav, 'getFormattedItem', array($row,$this->uri));
61  }
62 
63  protected function prepareShowFolderTree($parentFolderId='parentFolderId')
64  {
65  $this->folderDao->shouldReceive('getFolderTreeCte')->with($parentFolderId)
66  ->andReturn($parentFolderId.'Cte');
67  $this->dbManager->shouldReceive('prepare')->withArgs(array(anything(),startsWith($parentFolderId.'Cte')));
68  $this->dbManager->shouldReceive('execute')->withArgs(array(anything(),array($parentFolderId)))
69  ->andReturn($res=$parentFolderId.'Res');
70  $this->dbManager->shouldReceive('freeResult')->with($res);
71  return $res;
72  }
73 
74  public function testShowFolderTreeWithoutContent()
75  {
76  $res = $this->prepareShowFolderTree($parentFolderId='foo');
77  $this->dbManager->shouldReceive('fetchArray')->with($res)
78  ->andReturn($rowA=array('folder_pk'=>1, 'folder_name'=>'A', 'folder_desc'=>'', 'depth'=>0),false);
79  $out = $this->folderNav->showFolderTree($parentFolderId);
80  assertThat($out, equalTo('<ul id="tree"><li>'.$this->getFormattedItem($rowA).'</li></ul>'));
81  }
82 
83  public function testShowFolderTreeWithContent()
84  {
85  $res = $this->prepareShowFolderTree($parentFolderId='foo');
86  $this->dbManager->shouldReceive('fetchArray')->with($res)
87  ->andReturn($rowTop=array('folder_pk'=>1, 'folder_name'=>'Top', 'folder_desc'=>'', 'depth'=>0),
88  $rowA=array('folder_pk'=>2, 'folder_name'=>'B', 'folder_desc'=>'/A', 'depth'=>1),
89  $rowB=array('folder_pk'=>3, 'folder_name'=>'B', 'folder_desc'=>'/A/B', 'depth'=>2),
90  $rowC=array('folder_pk'=>4, 'folder_name'=>'C', 'folder_desc'=>'/C', 'depth'=>1),
91  false);
92  $out = $this->folderNav->showFolderTree($parentFolderId);
93  assertThat(str_replace("\n",'',$out), equalTo('<ul id="tree"><li>'.$this->getFormattedItem($rowTop).'<ul><li>'
94  .$this->getFormattedItem($rowA).'<ul><li>'
95  .$this->getFormattedItem($rowB).'</li></ul></li><li>'
96  .$this->getFormattedItem($rowC).'</li></ul></li></ul>'));
97  }
98 }
Traceback_uri()
Get the URI without query to this location.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28