FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseViewProxyTest.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 LicenseViewProxyTest extends \PHPUnit\Framework\TestCase
14 {
15  private $dbManagerMock;
16 
17  protected function setUp() : void
18  {
19  global $container;
20  $container = M::mock('ContainerBuilder');
21  $this->dbManagerMock = M::mock(DbManager::class);
22  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManagerMock);
23  $this->almostAllColumns = 'rf_pk,rf_spdx_id,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';
24  }
25 
26  protected function tearDown() : void
27  {
28  M::close();
29  }
30 
31  public function testQueryOnlyLicenseRef()
32  {
33  $licenseViewProxy = new LicenseViewProxy(0);
34 
35  $reflection = new \ReflectionClass(get_class($licenseViewProxy));
36  $method = $reflection->getMethod('queryOnlyLicenseRef');
37  $method->setAccessible(true);
38 
39  $options1 = array('columns'=>array('rf_pk','rf_shortname'));
40  $query1 = $method->invoke($licenseViewProxy,$options1);
41  assertThat($query1, is("SELECT rf_pk,rf_shortname FROM ONLY license_ref"));
42 
43  $options2 = array('extraCondition'=>'rf_pk<100');
44  $query2 = $method->invoke($licenseViewProxy,$options2);
45  assertThat($query2, is("SELECT $this->almostAllColumns,0 AS group_fk FROM ONLY license_ref WHERE rf_pk<100"));
46  }
47 
48  public function testQueryLicenseCandidate()
49  {
50  $groupId = 123;
51  $licenseViewProxy = new LicenseViewProxy($groupId);
52 
53  $reflection = new \ReflectionClass(get_class($licenseViewProxy));
54  $method = $reflection->getMethod('queryLicenseCandidate');
55  $method->setAccessible(true);
56 
57  $options1 = array('columns'=>array('rf_pk','rf_shortname'));
58  $query1 = $method->invoke($licenseViewProxy,$options1);
59  assertThat($query1, is("SELECT rf_pk,rf_shortname FROM license_candidate WHERE group_fk=$groupId"));
60 
61  $options2 = array('extraCondition'=>'rf_pk<100');
62  $query2 = $method->invoke($licenseViewProxy,$options2);
63  assertThat($query2, is("SELECT $this->almostAllColumns,group_fk FROM license_candidate WHERE group_fk=$groupId AND rf_pk<100"));
64 
65  $prefix = '#';
66  $options3 = array(LicenseViewProxy::CANDIDATE_PREFIX=>$prefix,'columns'=>array('rf_shortname'));
67  $query3 = $method->invoke($licenseViewProxy,$options3);
68  assertThat($query3, is("SELECT '". pg_escape_string($prefix). "'||rf_shortname AS rf_shortname FROM license_candidate WHERE group_fk=$groupId"));
69  }
70 
71  public function testConstruct()
72  {
73  $licenseViewProxy0 = new LicenseViewProxy(0);
74  $query0 = $licenseViewProxy0->getDbViewQuery();
75  $expected0 = "SELECT $this->almostAllColumns,0 AS group_fk FROM ONLY license_ref";
76  assertThat($query0,is($expected0));
77 
78  $licenseViewProxy123 = new LicenseViewProxy(123,array('diff'=>true));
79  $query123 = $licenseViewProxy123->getDbViewQuery();
80  $expected123 = "SELECT $this->almostAllColumns,group_fk FROM license_candidate WHERE group_fk=123";
81  assertThat($query123,is($expected123));
82 
83  $licenseViewProxy0123 = new LicenseViewProxy(123);
84  $query0123 = $licenseViewProxy0123->getDbViewQuery();
85  $expected0123 = "$expected123 UNION $expected0";
86  assertThat($query0123,is($expected0123));
87  }
88 }