FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseViewProxyTest.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Proxy;
20 
22 use Mockery as M;
23 
24 class LicenseViewProxyTest extends \PHPUnit\Framework\TestCase
25 {
26  private $dbManagerMock;
27 
28  protected function setUp()
29  {
30  global $container;
31  $container = M::mock('ContainerBuilder');
32  $this->dbManagerMock = M::mock(DbManager::class);
33  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManagerMock);
34  $this->almostAllColumns = 'rf_pk,rf_shortname,rf_text,rf_url,rf_add_date,rf_copyleft,rf_fullname,rf_notes,marydone,rf_active,rf_text_updatable,rf_md5,rf_detector_type,rf_source';
35  }
36 
37  protected function tearDown()
38  {
39  M::close();
40  }
41 
42  public function testQueryOnlyLicenseRef()
43  {
44  $licenseViewProxy = new LicenseViewProxy(0);
45 
46  $reflection = new \ReflectionClass(get_class($licenseViewProxy));
47  $method = $reflection->getMethod('queryOnlyLicenseRef');
48  $method->setAccessible(true);
49 
50  $options1 = array('columns'=>array('rf_pk','rf_shortname'));
51  $query1 = $method->invoke($licenseViewProxy,$options1);
52  assertThat($query1, is("SELECT rf_pk,rf_shortname FROM ONLY license_ref"));
53 
54  $options2 = array('extraCondition'=>'rf_pk<100');
55  $query2 = $method->invoke($licenseViewProxy,$options2);
56  assertThat($query2, is("SELECT $this->almostAllColumns,0 AS group_fk FROM ONLY license_ref WHERE rf_pk<100"));
57  }
58 
59  public function testQueryLicenseCandidate()
60  {
61  $groupId = 123;
62  $licenseViewProxy = new LicenseViewProxy($groupId);
63 
64  $reflection = new \ReflectionClass(get_class($licenseViewProxy));
65  $method = $reflection->getMethod('queryLicenseCandidate');
66  $method->setAccessible(true);
67 
68  $options1 = array('columns'=>array('rf_pk','rf_shortname'));
69  $query1 = $method->invoke($licenseViewProxy,$options1);
70  assertThat($query1, is("SELECT rf_pk,rf_shortname FROM license_candidate WHERE group_fk=$groupId"));
71 
72  $options2 = array('extraCondition'=>'rf_pk<100');
73  $query2 = $method->invoke($licenseViewProxy,$options2);
74  assertThat($query2, is("SELECT $this->almostAllColumns,group_fk FROM license_candidate WHERE group_fk=$groupId AND rf_pk<100"));
75 
76  $prefix = '#';
77  $options3 = array(LicenseViewProxy::CANDIDATE_PREFIX=>$prefix,'columns'=>array('rf_shortname'));
78  $query3 = $method->invoke($licenseViewProxy,$options3);
79  assertThat($query3, is("SELECT '". pg_escape_string($prefix). "'||rf_shortname AS rf_shortname FROM license_candidate WHERE group_fk=$groupId"));
80  }
81 
82  public function testConstruct()
83  {
84  $licenseViewProxy0 = new LicenseViewProxy(0);
85  $query0 = $licenseViewProxy0->getDbViewQuery();
86  $expected0 = "SELECT $this->almostAllColumns,0 AS group_fk FROM ONLY license_ref";
87  assertThat($query0,is($expected0));
88 
89  $licenseViewProxy123 = new LicenseViewProxy(123,array('diff'=>true));
90  $query123 = $licenseViewProxy123->getDbViewQuery();
91  $expected123 = "SELECT $this->almostAllColumns,group_fk FROM license_candidate WHERE group_fk=123";
92  assertThat($query123,is($expected123));
93 
94  $licenseViewProxy0123 = new LicenseViewProxy(123);
95  $query0123 = $licenseViewProxy0123->getDbViewQuery();
96  $expected0123 = "$expected123 UNION $expected0";
97  assertThat($query0123,is($expected0123));
98  }
99 }