Skip to content

Commit

Permalink
Merge pull request johnfercher#132 from johnfercher/release/08-01-2020
Browse files Browse the repository at this point in the history
release/08-01-2020
  • Loading branch information
johnfercher authored Jan 8, 2020
2 parents 5386d9c + 3925a15 commit 09beb58
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 26 deletions.
4 changes: 2 additions & 2 deletions internal/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func NewSignature(pdf gofpdf.Pdf, math Math, text Text) *signature {
// AddSpaceFor create a space for a signature inside a cell
func (s *signature) AddSpaceFor(label string, textProp props.Text, qtdCols float64, marginTop float64, actualCol float64) {
widthPerCol := s.math.GetWidthPerCol(qtdCols)
left, _, right, _ := s.pdf.GetMargins()
left, _, _, _ := s.pdf.GetMargins()
space := 4.0

s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+right-space, marginTop+5.0)
s.pdf.Line((widthPerCol*actualCol)+left+space, marginTop+5.0, widthPerCol*(actualCol+1)+left-space, marginTop+5.0)
s.text.Add(label, textProp, marginTop, actualCol, qtdCols)
}
26 changes: 25 additions & 1 deletion internal/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewSignature(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%T", signature), "*internal.signature")
}

func TestSignature_AddSpaceFor(t *testing.T) {
func TestSignature_AddSpaceFor_DefaultMargins(t *testing.T) {
// Arrange
pdf := &mocks.Pdf{}
pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0)
Expand All @@ -40,3 +40,27 @@ func TestSignature_AddSpaceFor(t *testing.T) {
text.AssertNumberOfCalls(t, "Add", 1)
text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0)
}

func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) {
// Arrange
pdf := &mocks.Pdf{}
pdf.On("GetMargins").Return(20.0, 10.0, 10.0, 10.0)
pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything)

math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(50.0)

text := &mocks.Text{}
text.On("Add", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)

signature := internal.NewSignature(pdf, math, text)

// Act
signature.AddSpaceFor("label", props.Text{Size: 10.0}, 5, 5, 2)

// Assert
pdf.AssertNumberOfCalls(t, "Line", 1)
pdf.AssertCalled(t, "Line", 124.0, 10.0, 166.0, 10.0)
text.AssertNumberOfCalls(t, "Add", 1)
text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0)
}
6 changes: 6 additions & 0 deletions pkg/pdf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,9 @@ func ExamplePdfMaroto_RegisterHeader() {

// Do more things or not and save...
}

// ExamplePdfMaroto_SetPageMargins demonstrates how to set custom page margins.
func ExamplePdfMaroto_SetPageMargins() {
m := pdf.NewMaroto(consts.Portrait, consts.A4)
m.SetPageMargins(10, 60, 10)
}
14 changes: 14 additions & 0 deletions pkg/pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Maroto interface {
GetPageSize() (float64, float64)
GetCurrentPage() int
GetCurrentOffset() float64
SetPageMargins(left, top, right float64)
GetPageMargins() (float64, float64, float64, float64)

// Outside Col/Row Components
TableList(header []string, contents [][]string, prop ...props.TableList)
Expand Down Expand Up @@ -144,6 +146,18 @@ func (s *PdfMaroto) GetCurrentOffset() float64 {
return s.offsetY
}

// SetPageMargins overrides default margins (10,10,10)
// the new page margin will affect all PDF pages
func (s *PdfMaroto) SetPageMargins(left, top, right float64) {
s.Pdf.SetMargins(left, top, right)
}

// GetPageMargins returns the set page margins. Comes in order of Left, Top, Right, Bottom
// Default page margins is left: 10, top: 10, right: 10
func (s *PdfMaroto) GetPageMargins() (left float64, top float64, right float64, bottom float64) {
return s.Pdf.GetMargins()
}

// Signature add a space for a signature inside a cell,
// the space will have a line and a text below
func (s *PdfMaroto) Signature(label string, prop ...props.Font) {
Expand Down
81 changes: 58 additions & 23 deletions pkg/pdf/pdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestPdfMaroto_Signature(t *testing.T) {
for _, c := range cases {
// Arrange
signature := c.signature()
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
tableList := baseTableList()
m := newMarotoTest(pdf, math, nil, nil, signature, nil, nil, tableList)
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestPdfMaroto_Text(t *testing.T) {
for _, c := range cases {
// Arrange
text := baseTextTest()
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
tableList := baseTableList()
m := newMarotoTest(pdf, math, nil, text, nil, nil, nil, tableList)
Expand Down Expand Up @@ -589,7 +589,7 @@ func TestPdfMaroto_FileImage(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
image := c.image()
tableList := baseTableList()
Expand Down Expand Up @@ -759,7 +759,7 @@ func TestPdfMaroto_Base64Image(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
image := c.image()
tableList := baseTableList()
Expand Down Expand Up @@ -915,7 +915,7 @@ func TestPdfMaroto_QrCode(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
code := c.code()
tableList := baseTableList()
Expand Down Expand Up @@ -999,7 +999,7 @@ func TestPdfMaroto_Barcode(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
code := c.code()
tableList := baseTableList()
Expand Down Expand Up @@ -1116,7 +1116,7 @@ func TestPdfMaroto_Row(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
tableList := baseTableList()

Expand All @@ -1142,7 +1142,7 @@ func TestPdfMaroto_Line(t *testing.T) {
{
"One line",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
return pdf
},
Expand All @@ -1160,7 +1160,7 @@ func TestPdfMaroto_Line(t *testing.T) {
{
"Two lines",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("Line", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
return pdf
},
Expand Down Expand Up @@ -1259,7 +1259,7 @@ func TestPdfMaroto_ColSpace(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
tableList := baseTableList()

Expand Down Expand Up @@ -1337,7 +1337,7 @@ func TestPdfMaroto_ColSpaces(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
tableList := baseTableList()

Expand All @@ -1364,7 +1364,7 @@ func TestPdfMaroto_Output(t *testing.T) {
{
"When Output returns an error",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("Output", mock.Anything).Return(errors.New("AnyError"))
return pdf
},
Expand All @@ -1385,7 +1385,7 @@ func TestPdfMaroto_Output(t *testing.T) {
{
"When Output not returns an error",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("Output", mock.Anything).Return(nil)
return pdf
},
Expand All @@ -1406,7 +1406,7 @@ func TestPdfMaroto_Output(t *testing.T) {
{
"When Output has footer",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("Output", mock.Anything).Return(nil)
return pdf
},
Expand Down Expand Up @@ -1463,7 +1463,7 @@ func TestPdfMaroto_OutputFileAndClose(t *testing.T) {
{
"When OutputFileAndClose returns an error",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("OutputFileAndClose", mock.Anything).Return(errors.New("AnyError"))
return pdf
},
Expand All @@ -1481,7 +1481,7 @@ func TestPdfMaroto_OutputFileAndClose(t *testing.T) {
{
"When OutputFileAndClose not returns an error",
func() *mocks.Pdf {
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
pdf.On("OutputFileAndClose", mock.Anything).Return(nil)
return pdf
},
Expand Down Expand Up @@ -1531,13 +1531,14 @@ func newMarotoTest(fpdf *mocks.Pdf, math *mocks.Math, font *mocks.Font, text *mo
return m
}

func basePdfTest() *mocks.Pdf {
func basePdfTest(left, top, right float64) *mocks.Pdf {
pdf := &mocks.Pdf{}
pdf.On("GetPageSize").Return(100.0, 100.0)
pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0)
pdf.On("GetMargins").Return(left, top, right, 0.0)
pdf.On("CellFormat", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
pdf.On("Ln", mock.Anything)
pdf.On("GetFontSize").Return(1.0, 1.0)
pdf.On("SetMargins", mock.AnythingOfType("float64"), mock.AnythingOfType("float64"), mock.AnythingOfType("float64"))
return pdf
}

Expand Down Expand Up @@ -1639,7 +1640,7 @@ func TestPdfMaroto_RegisterFooter(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
text := baseTextTest()
font := baseFontTest()
Expand Down Expand Up @@ -1723,7 +1724,7 @@ func TestPdfMaroto_RegisterHeader(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
text := baseTextTest()
font := baseFontTest()
Expand Down Expand Up @@ -1791,7 +1792,7 @@ func TestPdfMaroto_GetCurrentPage(t *testing.T) {

for _, c := range cases {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
text := baseTextTest()
font := baseFontTest()
Expand All @@ -1810,7 +1811,7 @@ func TestPdfMaroto_GetCurrentPage(t *testing.T) {

func TestPdfMaroto_GetCurrentPage_WhenCreateOffsetIsZero(t *testing.T) {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
text := baseTextTest()
font := baseFontTest()
Expand All @@ -1827,7 +1828,7 @@ func TestPdfMaroto_GetCurrentPage_WhenCreateOffsetIsZero(t *testing.T) {

func TestPdfMaroto_GetCurrentPage_WhenIsNotZero(t *testing.T) {
// Arrange
pdf := basePdfTest()
pdf := basePdfTest(10, 10, 10)
math := baseMathTest()
text := baseTextTest()
font := baseFontTest()
Expand All @@ -1847,3 +1848,37 @@ func TestPdfMaroto_GetCurrentPage_WhenIsNotZero(t *testing.T) {
// Assert
assert.Equal(t, offset, float64(20))
}

func TestPdfMaroto_SetPageMargins(t *testing.T) {
cases := []struct {
name string
act func(m pdf.Maroto)
assert func(t *testing.T, left, top, right float64)
}{
{
"Set page margins should override default",
func(m pdf.Maroto) {
m.SetPageMargins(12.3, 19.3, 0.0)
},
func(t *testing.T, left, top, right float64) {
assert.Equal(t, 12.3, left)
assert.Equal(t, 19.3, top)
assert.Equal(t, 0.0, right)
},
},
}

for _, c := range cases {
// Arrange
pdf := basePdfTest(12.3, 19.3, 0)

m := newMarotoTest(pdf, nil, nil, nil, nil, nil, nil, nil)

// Act
c.act(m)

// Assert
left, top, right, _ := m.GetPageMargins()
c.assert(t, left, top, right)
}
}

0 comments on commit 09beb58

Please sign in to comment.