FOSSology  4.5.1
Open Source License Compliance by Open Source Software
HashTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Harshit Gandhi <gandhiharshit716@gmail.com>
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
13 
15 use PHPUnit\Framework\TestCase;
16 
21 class HashTest extends TestCase
22 {
24  private $sha1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
25  private $md5 = "d41d8cd98f00b204e9800998ecf8427e";
26  private $sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
27  private $size = 1024;
28 
30 
36  public function testConstructor()
37  {
38  $hash = new Hash($this->sha1, $this->md5, $this->sha256, $this->size);
39  $this->assertInstanceOf(Hash::class, $hash);
40  }
41 
43 
48  public function testGetSha1()
49  {
50  $hash = new Hash($this->sha1, $this->md5, $this->sha256, $this->size);
51  $this->assertEquals($this->sha1, $hash->getSha1());
52  }
53 
58  public function testGetMd5()
59  {
60  $hash = new Hash($this->sha1, $this->md5, $this->sha256, $this->size);
61  $this->assertEquals($this->md5, $hash->getMd5());
62  }
63 
68  public function testGetSha256()
69  {
70  $hash = new Hash($this->sha1, $this->md5, $this->sha256, $this->size);
71  $this->assertEquals($this->sha256, $hash->getSha256());
72  }
73 
78  public function testGetSize()
79  {
80  $hash = new Hash($this->sha1, $this->md5, $this->sha256, $this->size);
81  $this->assertEquals($this->size, $hash->getSize());
82  }
83 
85 
90  public function testGetArrayWithFullData()
91  {
92  $hash = new Hash($this->sha1, $this->md5, $this->sha256, $this->size);
93 
94  $expectedArray = [
95  'sha1' => $this->sha1,
96  'md5' => $this->md5,
97  'sha256' => $this->sha256,
98  'size' => $this->size
99  ];
100 
101  $this->assertEquals($expectedArray, $hash->getArray());
102  }
103 
108  public function testGetArrayWithNullValues()
109  {
110  $hash = new Hash(null, null, null, null);
111 
112  $expectedArray = [
113  'sha1' => null,
114  'md5' => null,
115  'sha256' => null,
116  'size' => null
117  ];
118 
119  $this->assertEquals($expectedArray, $hash->getArray());
120  }
121 
123 
129  {
130  $inputArray = [
131  'sha1' => $this->sha1,
132  'md5' => $this->md5,
133  'sha256' => $this->sha256,
134  'size' => $this->size
135  ];
136 
137  $hash = Hash::createFromArray($inputArray);
138  $this->assertInstanceOf(Hash::class, $hash);
139  $this->assertEquals($this->sha1, $hash->getSha1());
140  $this->assertEquals($this->md5, $hash->getMd5());
141  $this->assertEquals($this->sha256, $hash->getSha256());
142  $this->assertEquals($this->size, $hash->getSize());
143  }
144 
150  {
151  $inputArray = [
152  'sha1' => $this->sha1,
153  'size' => $this->size
154  ];
155 
156  $hash = Hash::createFromArray($inputArray);
157  $this->assertInstanceOf(Hash::class, $hash);
158  $this->assertEquals($this->sha1, $hash->getSha1());
159  $this->assertNull($hash->getMd5());
160  $this->assertNull($hash->getSha256());
161  $this->assertEquals($this->size, $hash->getSize());
162  }
163 
169  {
170  $hash = Hash::createFromArray([]);
171  $this->assertInstanceOf(Hash::class, $hash);
172  $this->assertNull($hash->getSha1());
173  $this->assertNull($hash->getMd5());
174  $this->assertNull($hash->getSha256());
175  $this->assertNull($hash->getSize());
176  }
177 
183  {
184  $inputArray = [
185  'sha1' => $this->sha1,
186  'invalid_key' => 'value'
187  ];
188 
189  $hash = Hash::createFromArray($inputArray);
190  $this->assertNull($hash);
191  }
192 
198  {
199  $inputArray = [
200  'sha1' => $this->sha1,
201  'md5' => $this->md5,
202  'invalid_key' => 'value',
203  'size' => $this->size
204  ];
205 
206  $hash = Hash::createFromArray($inputArray);
207  $this->assertNull($hash);
208  }
209 }
Hash model holding information about file like checksums and size.
Definition: Hash.php:18
static createFromArray($inputArray)
Definition: Hash.php:125