FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadTreeViewProxyTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Proxy;
9 
11 use Mockery as M;
12 
13 class UploadTreeViewProxyTest extends \PHPUnit\Framework\TestCase
14 {
15  private $viewSuffix = "<suffix>";
16 
18  private $itemTreeBounds;
19 
20  protected function setUp() : void
21  {
22  $this->itemTreeBounds = M::mock(ItemTreeBounds::class);
23  }
24 
25  public function testDefaultViewName()
26  {
27  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
28 
29  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds);
30 
31  assertThat($uploadTreeView->getDbViewName(), is("UploadTreeView"));
32  }
33 
34  public function testViewName()
35  {
36  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
37 
38  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds, array(), $this->viewSuffix);
39 
40  assertThat($uploadTreeView->getDbViewName(), is("UploadTreeView.<suffix>"));
41  }
42 
43  public function testWithoutCondition()
44  {
45  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
46 
47  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds);
48 
49  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM foo"));
50  }
51 
52  public function testWithoutConditionAndDefaultTable()
53  {
54  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("uploadtree_a");
55  $this->itemTreeBounds->shouldReceive("getUploadId")->once()->withNoArgs()->andReturn(23);
56 
57  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds);
58 
59  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM uploadtree_a WHERE upload_fk = 23"));
60  }
61 
62  public function testWithoutConditionAndMasterTable()
63  {
64  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("uploadtree");
65  $this->itemTreeBounds->shouldReceive("getUploadId")->once()->withNoArgs()->andReturn(23);
66 
67  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds);
68 
69  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM uploadtree WHERE upload_fk = 23"));
70  }
71 
72  public function testWithUploadCondition()
73  {
74  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
75  $this->itemTreeBounds->shouldReceive("getUploadId")->once()->withNoArgs()->andReturn(76);
76 
77  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds, array(UploadTreeViewProxy::CONDITION_UPLOAD));
78 
79  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM foo WHERE upload_fk = 76"));
80  }
81 
82  public function testWithDoubleUploadCondition()
83  {
84  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
85  $this->itemTreeBounds->shouldReceive("getUploadId")->once()->withNoArgs()->andReturn(76);
86 
87  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds, array(UploadTreeViewProxy::CONDITION_UPLOAD, UploadTreeViewProxy::CONDITION_UPLOAD));
88 
89  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM foo WHERE upload_fk = 76"));
90  }
91 
92  public function testWithRangeCondition()
93  {
94  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
95  $this->itemTreeBounds->shouldReceive("getLeft")->once()->withNoArgs()->andReturn(25);
96  $this->itemTreeBounds->shouldReceive("getRight")->once()->withNoArgs()->andReturn(50);
97 
98  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds, array(UploadTreeViewProxy::CONDITION_RANGE));
99 
100  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM foo WHERE lft BETWEEN 25 AND 50"));
101  }
102 
103  public function testWithPlainFilesCondition()
104  {
105  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
106 
107  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds, array(UploadTreeViewProxy::CONDITION_PLAIN_FILES));
108 
109  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM foo WHERE ((ufile_mode & (3<<28))=0) AND pfile_fk != 0"));
110  }
111 
112  public function testWithMultipleConditions()
113  {
114  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
115  $this->itemTreeBounds->shouldReceive("getUploadId")->once()->withNoArgs()->andReturn(5);
116  $this->itemTreeBounds->shouldReceive("getLeft")->once()->withNoArgs()->andReturn(22);
117  $this->itemTreeBounds->shouldReceive("getRight")->once()->withNoArgs()->andReturn(43);
118 
119  $uploadTreeView = new UploadTreeViewProxy($this->itemTreeBounds, array(UploadTreeViewProxy::CONDITION_UPLOAD, UploadTreeViewProxy::CONDITION_PLAIN_FILES, UploadTreeViewProxy::CONDITION_RANGE));
120 
121  assertThat($uploadTreeView->getDbViewQuery(), is("SELECT * FROM foo WHERE upload_fk = 5 AND ((ufile_mode & (3<<28))=0) AND pfile_fk != 0 AND lft BETWEEN 22 AND 43"));
122  }
123 
124  public function testExcpetionWithUnknownConstraint()
125  {
126  $this->expectException(\InvalidArgumentException::class);
127  $this->expectExceptionMessage("constraint bar is not defined");
128  $this->itemTreeBounds->shouldReceive("getUploadTreeTableName")->once()->withNoArgs()->andReturn("foo");
129 
130  new UploadTreeViewProxy($this->itemTreeBounds, array('bar'));
131  }
132 }